1
0
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:
Thor Brigsted 2017-12-25 01:35:11 +01:00
parent e21398cd19
commit 11a9db199a
2 changed files with 30 additions and 0 deletions

View File

@ -64,5 +64,26 @@ namespace XNode {
public void Clear() { public void Clear() {
nodes.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;
}
} }
} }

View File

@ -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] [Serializable]
private class PortConnection { private class PortConnection {
[SerializeField] public string fieldName; [SerializeField] public string fieldName;
[SerializeField] public Node node; [SerializeField] public Node node;
public NodePort Port { get { return port != null ? port : port = GetPort(); } } public NodePort Port { get { return port != null ? port : port = GetPort(); } }
[NonSerialized] private NodePort port; [NonSerialized] private NodePort port;
public PortConnection(NodePort port) { public PortConnection(NodePort port) {