mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-21 01:36:03 +08:00
Added nodeGraph.Copy();
This commit is contained in:
parent
e21398cd19
commit
11a9db199a
@ -64,5 +64,26 @@ namespace XNode {
|
||||
public void Clear() {
|
||||
nodes.Clear();
|
||||
}
|
||||
|
||||
/// <summary> Create a new deep copy of this graph </summary>
|
||||
public XNode.NodeGraph Copy() {
|
||||
// Instantiate a new nodegraph instance
|
||||
NodeGraph graph = Instantiate(this);
|
||||
// Instantiate all nodes inside the graph
|
||||
for (int i = 0; i < nodes.Count; i++) {
|
||||
Node node = Instantiate(nodes[i]) as Node;
|
||||
node.graph = graph;
|
||||
graph.nodes[i] = node;
|
||||
}
|
||||
|
||||
// Redirect all connections
|
||||
for (int i = 0; i < graph.nodes.Count; i++) {
|
||||
foreach (NodePort port in graph.nodes[i].Ports) {
|
||||
port.Redirect(nodes, graph.nodes);
|
||||
}
|
||||
}
|
||||
|
||||
return graph;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -230,11 +230,20 @@ namespace XNode {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Swap connected nodes from the old list with nodes from the new list </summary>
|
||||
public void Redirect(List<Node> oldNodes, List<Node> newNodes) {
|
||||
foreach (PortConnection connection in connections) {
|
||||
int index = oldNodes.IndexOf(connection.node);
|
||||
if (index >= 0) connection.node = newNodes[index];
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
private class PortConnection {
|
||||
[SerializeField] public string fieldName;
|
||||
[SerializeField] public Node node;
|
||||
public NodePort Port { get { return port != null ? port : port = GetPort(); } }
|
||||
|
||||
[NonSerialized] private NodePort port;
|
||||
|
||||
public PortConnection(NodePort port) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user