public override IEnumerable<Node> OnPreProcess()
{
//Don't do anything if this node isn't valid.
if (selected < 0)
return new List<Node>();
//Load the sub-graph.
Graph g = TryLoadGraph();
if (g == null)
{
return new List<Node>();
}
//Clone the sub-graph's nodes.
List<ParamNode_Float> floatParams = new List<ParamNode_Float>();
List<Node> newNodes = new List<Node>(g.Nodes.Select(n => n.Clone()));
//Add the new nodes to the graph, with new UID's.
//Keep track of the float param nodes because they map to this node's inputs.
int baseUID = Owner.NextUID;
Owner.NextUID += newNodes.Count;
foreach (Node n in newNodes)
{
//Offset all the UID's so there's no conflicts with the rest of the graph.
n.UID = baseUID + n.UID;
Owner.NextUID = Mathf.Max(Owner.NextUID, n.UID + 1);
for (int i = 0; i < n.Inputs.Count; ++i)
if (!n.Inputs[i].IsAConstant)
n.Inputs[i] = new NodeInput(n.Inputs[i].NodeID + baseUID);
Owner.AddNode(n, false);
//If this node is another sub-graph, make sure there's no infinite loop.
if (convertParamsToInputs && n is ParamNode_Float)
{
floatParams.Add((ParamNode_Float)n);
}
}
//Error-checking.
if (floatParams.Count != Inputs.Count)
{
Debug.LogError("Expected " + Inputs.Count + " float params in the graph but found " +
floatParams.Count);
return new List<Node>();
}
//Sort the float params so they line up with this node's inputs.
{
List<ParamNode_Float> sortedFloatParams = new List<ParamNode_Float>();
for (int i = 0; i < InputNames.Count; ++i)
{
for (int j = 0; j < floatParams.Count; ++j)
{
if (floatParams[j].Param.Name == InputNames[i])
{
sortedFloatParams.Add(floatParams[j]);
break;
}
}
if (sortedFloatParams.Count <= i)
{
Debug.LogError("This sub-graph had an unnecessary input \"" +
InputNames[i] + "\"");
}
}
floatParams = sortedFloatParams;
}
//Replace all references to those float params with this node's inputs.
foreach (Node n in newNodes)
{
for (int i = 0; i < n.Inputs.Count; ++i)
{
for (int j = 0; j < floatParams.Count; ++j)
{
if (!n.Inputs[i].IsAConstant && n.Inputs[i].NodeID == floatParams[j].UID)
{
n.Inputs[i] = Inputs[j];
}
}
}
}
//Remove the float params.
foreach (Node n in floatParams)
Owner.RemoveNode(n);
//Replace any references to this node with references to the output of the sub-graph.
NodeInput gOut = (g.Output.IsAConstant ?
g.Output :
new NodeInput(g.Output.NodeID + baseUID));
if (!Owner.Output.IsAConstant && Owner.Output.NodeID == UID)
Owner.Output = gOut;
foreach (Node nd in Owner.Nodes)
for (int i = 0; i < nd.Inputs.Count; ++i)
if (!nd.Inputs[i].IsAConstant && nd.Inputs[i].NodeID == UID)
nd.Inputs[i] = gOut;
//Finally, remove this node.
Owner.RemoveNode(this);
return newNodes;
}