diff --git a/Scripts/NodeGraph.cs b/Scripts/NodeGraph.cs
index f71642b..c2092e0 100644
--- a/Scripts/NodeGraph.cs
+++ b/Scripts/NodeGraph.cs
@@ -64,5 +64,26 @@ namespace XNode {
public void Clear() {
nodes.Clear();
}
+
+ /// Create a new deep copy of this graph
+ 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;
+ }
}
}
\ No newline at end of file
diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs
index 3aba9ed..039ab44 100644
--- a/Scripts/NodePort.cs
+++ b/Scripts/NodePort.cs
@@ -230,11 +230,20 @@ namespace XNode {
}
}
+ /// Swap connected nodes from the old list with nodes from the new list
+ public void Redirect(List oldNodes, List 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) {