Assets/xNode/Scripts/Editor/NodeEditorAction.cs
/// <summary> Duplicate selected nodes and select the duplicates </summary>
Duplicate selected nodes and select the duplicates
Duplicate selected nodes and select the duplicates
public void DuplicateSelectedNodes() {
            UnityEngine.Object[] newNodes = new UnityEngine.Object[Selection.objects.Length];
            Dictionary<XNode.Node, XNode.Node> substitutes = new Dictionary<XNode.Node, XNode.Node>();
            for (int i = 0; i < Selection.objects.Length; i++) {
                if (Selection.objects[i] is XNode.Node) {
                    XNode.Node srcNode = Selection.objects[i] as XNode.Node;
                    if (srcNode.graph != graph) continue; 
                    XNode.Node newNode = graphEditor.CopyNode(srcNode);
                    substitutes.Add(srcNode, newNode);
                    newNode.position = srcNode.position + new Vector2(30, 30);
                    newNodes[i] = newNode;
                }
            }
            for (int i = 0; i < Selection.objects.Length; i++) {
                if (Selection.objects[i] is XNode.Node) {
                    XNode.Node srcNode = Selection.objects[i] as XNode.Node;
                    if (srcNode.graph != graph) continue; 
                    foreach (XNode.NodePort port in srcNode.Ports) {
                        for (int c = 0; c < port.ConnectionCount; c++) {
                            XNode.NodePort inputPort = port.direction == XNode.NodePort.IO.Input ? port : port.GetConnection(c);
                            XNode.NodePort outputPort = port.direction == XNode.NodePort.IO.Output ? port : port.GetConnection(c);
                            XNode.Node newNodeIn, newNodeOut;
                            if (substitutes.TryGetValue(inputPort.node, out newNodeIn) && substitutes.TryGetValue(outputPort.node, out newNodeOut)) {
                                newNodeIn.UpdateStaticPorts();
                                newNodeOut.UpdateStaticPorts();
                                inputPort = newNodeIn.GetInputPort(inputPort.fieldName);
                                outputPort = newNodeOut.GetOutputPort(outputPort.fieldName);
                            }
                            if (!inputPort.IsConnectedTo(outputPort)) inputPort.Connect(outputPort);
                        }
                    }
                }
            }
            Selection.objects = newNodes;
        }
public void DuplicateSelectedNodes ( ) { UnityEngine . Object [ ] newNodes = new UnityEngine . Object [ Selection . objects . Length ] ; Dictionary < XNode . Node , XNode . Node > substitutes = new Dictionary < XNode . Node , XNode . Node > ( ) ; for ( int i = 0 ; i < Selection . objects . Length ; i ++ ) { if ( Selection . objects [ i ] is XNode . Node ) { XNode . Node srcNode = Selection . objects [ i ] as XNode . Node ; if ( srcNode . graph != graph ) continue ; XNode . Node newNode = graphEditor . CopyNode ( srcNode ) ; substitutes . Add ( srcNode , newNode ) ; newNode . position = srcNode . position + new Vector2 ( 30 , 30 ) ; newNodes [ i ] = newNode ; } } for ( int i = 0 ; i < Selection . objects . Length ; i ++ ) { if ( Selection . objects [ i ] is XNode . Node ) { XNode . Node srcNode = Selection . objects [ i ] as XNode . Node ; if ( srcNode . graph != graph ) continue ; foreach ( XNode . NodePort port in srcNode . Ports ) { for ( int c = 0 ; c < port . ConnectionCount ; c ++ ) { XNode . NodePort inputPort = port . direction == XNode . NodePort . IO . Input ? port : port . GetConnection ( c ) ; XNode . NodePort outputPort = port . direction == XNode . NodePort . IO . Output ? port : port . GetConnection ( c ) ; XNode . Node newNodeIn , newNodeOut ; if ( substitutes . TryGetValue ( inputPort . node , out newNodeIn ) && substitutes . TryGetValue ( outputPort . node , out newNodeOut ) ) { newNodeIn . UpdateStaticPorts ( ) ; newNodeOut . UpdateStaticPorts ( ) ; inputPort = newNodeIn . GetInputPort ( inputPort . fieldName ) ; outputPort = newNodeOut . GetOutputPort ( outputPort . fieldName ) ; } if ( ! inputPort . IsConnectedTo ( outputPort ) ) inputPort . Connect ( outputPort ) ; } } } } Selection . objects = newNodes ; }
Duplicate selected nodes and select the duplicates
Duplicate selected nodes and select the duplicates
// ignore nodes selected in another graph // Walk through the selected nodes again, recreate connections, using the new nodes // ignore nodes selected in another graph