/// <summary> /// Validates this canvas, checking for any broken nodes or references and cleans them. /// </summary> public void Validate() { NodeEditor.checkInit(false); // Check Groups CheckNodeCanvasList(ref groups, "groups"); // Check Nodes and their connection ports CheckNodeCanvasList(ref nodes, "nodes"); foreach (Node node in nodes) { ConnectionPortManager.UpdateConnectionPorts(node); foreach (ConnectionPort port in node.connectionPorts) { port.Validate(node); } } // Check EditorStates if (editorStates == null) { editorStates = new NodeEditorState[0]; } editorStates = editorStates.Where((NodeEditorState state) => state != null).ToArray(); foreach (NodeEditorState state in editorStates) { if (!nodes.Contains(state.selectedNode)) { state.selectedNode = null; } } // Validate CanvasType-specific stuff ValidateSelf(); }
/// <summary> /// Creates a node of the specified ID at pos on the specified canvas, optionally auto-connecting the specified output to a matching input /// silent disables any events, init specifies whether OnCreate should be called /// </summary> public static Node Create(string nodeID, Vector2 pos, NodeCanvas hostCanvas, ConnectionPort connectingPort = null, bool silent = false, bool init = true) { if (string.IsNullOrEmpty(nodeID) || hostCanvas == null) { throw new ArgumentException(); } if (!NodeCanvasManager.CheckCanvasCompability(nodeID, hostCanvas.GetType())) { throw new UnityException("Cannot create Node with ID '" + nodeID + "' as it is not compatible with the current canavs type (" + hostCanvas.GetType().ToString() + ")!"); } if (!hostCanvas.CanAddNode(nodeID)) { throw new UnityException("Cannot create Node with ID '" + nodeID + "' on the current canvas of type (" + hostCanvas.GetType().ToString() + ")!"); } // Create node from data NodeTypeData data = NodeTypes.getNodeData(nodeID); Node node = (Node)CreateInstance(data.type); if (node == null) { return(null); } // Init node state node.name = node.Title; node.autoSize = node.DefaultSize; node.position = pos; node.Canvas = hostCanvas; ConnectionPortManager.UpdateConnectionPorts(node); if (init) { node.OnCreate(); } if (connectingPort != null) { // Handle auto-connection and link the output to the first compatible input for (int i = 0; i < node.connectionPorts.Count; i++) { if (node.connectionPorts[i].TryApplyConnection(connectingPort, silent)) { break; } } } // Add node to host canvas hostCanvas.nodes.Add(node); if (!silent) { // Callbacks hostCanvas.OnNodeChange(connectingPort != null ? connectingPort.body : node); NodeEditorCallbacks.IssueOnAddNode(node); hostCanvas.Validate(); NodeEditor.RepaintClients(); } return(node); }
/// <summary> /// Validates this canvas, checking for any broken nodes or references and cleans them. /// </summary> public bool Validate(bool repair = true) { NodeEditor.checkInit(false); // Check Groups if (!CheckNodeCanvasList(ref groups, "groups", repair) && !repair) { return(false); } // Check Nodes if (!CheckNodeCanvasList(ref nodes, "nodes", repair) && !repair) { return(false); } // Check Connection ports foreach (Node node in nodes) { ConnectionPortManager.UpdateConnectionPorts(node); if (node.canvas != this && !repair) { return(false); } node.canvas = this; foreach (ConnectionPort port in node.connectionPorts) { if (!port.Validate(node, repair) && !repair) { return(false); } } } // Check EditorStates if (editorStates == null) { editorStates = new NodeEditorState[0]; } editorStates = editorStates.Where((NodeEditorState state) => state != null).ToArray(); foreach (NodeEditorState state in editorStates) { if (!nodes.Contains(state.selectedNode)) { state.selectedNode = null; } } // Validate CanvasType-specific stuff ValidateSelf(); return(true); }
public static void ReinstateNode(Node node, List <ConnectionPort> connectedPorts) { if (node == null) { // Not critical. Happens when undo records are triggered, but it's canvas has been unloaded // Debug.LogWarning("Lost reference to Undo SO 'node'!"); return; } if (connectedPorts == null) // Should not happen if node has not been lost { Debug.LogWarning("Lost reference to Undo SO 'connectedPorts'!"); } node.canvas.nodes.Remove(node); node.canvas.nodes.Add(node); ConnectionPortManager.UpdateConnectionPorts(node); if (connectedPorts != null) { int portIndex = 0; for (int i = 0; i < connectedPorts.Count; i++) { if (connectedPorts[i] == null) { // 'Decode' null-seperated list portIndex++; continue; } if (node.connectionPorts.Count <= portIndex) { Debug.LogWarning("Misaligned port count in reinstated node!"); break; } ConnectionPort port1 = node.connectionPorts[portIndex], port2 = connectedPorts[i]; // Workaround for undo records created in playmode that have being deleted (by Unity) after exiting if (port1.body.canvas.nodes.Contains(port1.body) && port2.body.canvas.nodes.Contains(port2.body)) { port1.TryApplyConnection(port2, true); } } if (node.connectionPorts.Count > portIndex) { Debug.LogWarning("Misaligned port count in reinstated node!"); } } }
/// <summary> /// Creates a copy of the specified node at pos on the specified canvas, optionally auto-connecting the specified output to a matching input /// silent disables any events /// </summary> public static Node CreateCopy(Node toCopy, Vector2 pos, NodeCanvas hostCanvas, ConnectionPort connectingPort = null, bool silent = false) { if (toCopy == null || hostCanvas == null) { throw new ArgumentException(); } if (!NodeCanvasManager.CheckCanvasCompability(toCopy.GetID, hostCanvas.GetType())) { throw new UnityException("Cannot create Node with ID '" + toCopy.GetID + "' as it is not compatible with the current canvas type (" + hostCanvas.GetType().ToString() + ")!"); } if (!hostCanvas.CanAddNode(toCopy.GetID)) { throw new UnityException("Cannot create Node with ID '" + toCopy.GetID + "' on the current canvas of type (" + hostCanvas.GetType().ToString() + ")!"); } Node node = ScriptableObject.Instantiate(toCopy); //Clone static connection ports foreach (ConnectionPortDeclaration portDecl in ConnectionPortManager.GetPortDeclarationEnumerator(node, true)) { ConnectionPort port = (ConnectionPort)portDecl.portField.GetValue(node); port = portDecl.portInfo.CreateNew(node); portDecl.portField.SetValue(node, port); } //Clone dynamic connection ports for (int i = 0; i < node.dynamicConnectionPorts.Count; ++i) { node.dynamicConnectionPorts[i] = ScriptableObject.Instantiate(node.dynamicConnectionPorts[i]); node.dynamicConnectionPorts[i].body = node; node.dynamicConnectionPorts[i].ClearConnections(); } ConnectionPortManager.UpdateRepresentativePortLists(node); //Clone child SOs System.Func <ScriptableObject, ScriptableObject> copySOs = (ScriptableObject so) => ScriptableObject.Instantiate(so);; node.CopyScriptableObjects(copySOs); if (node == null) { return(null); } // Init node state node.name = node.Title; node.autoSize = node.DefaultSize; node.position = pos; ConnectionPortManager.UpdateConnectionPorts(node); if (connectingPort != null) { // Handle auto-connection and link the output to the first compatible input for (int i = 0; i < node.connectionPorts.Count; i++) { if (node.connectionPorts[i].TryApplyConnection(connectingPort, silent)) { break; } } } // Add node to host canvas hostCanvas.nodes.Add(node); if (!silent) { // Callbacks hostCanvas.OnNodeChange(connectingPort != null ? connectingPort.body : node); NodeEditorCallbacks.IssueOnAddNode(node); hostCanvas.Validate(); NodeEditor.RepaintClients(); } return(node); }
/// <summary> /// Creates a node of the specified ID at pos on the specified canvas, optionally auto-connecting the specified output to a matching input /// silent disables any events, init specifies whether OnCreate should be called /// </summary> public static Node Create(string nodeID, Vector2 pos, NodeCanvas hostCanvas, ConnectionPort connectingPort = null, bool silent = false, bool init = true) { if (string.IsNullOrEmpty(nodeID) || hostCanvas == null) { throw new ArgumentException(); } if (!NodeCanvasManager.CheckCanvasCompability(nodeID, hostCanvas.GetType())) { throw new UnityException("Cannot create Node with ID '" + nodeID + "' as it is not compatible with the current canavs type (" + hostCanvas.GetType().ToString() + ")!"); } if (!hostCanvas.CanAddNode(nodeID)) { throw new UnityException("Cannot create Node with ID '" + nodeID + "' on the current canvas of type (" + hostCanvas.GetType().ToString() + ")!"); } // Create node from data NodeTypeData data = NodeTypes.getNodeData(nodeID); Node node = (Node)CreateInstance(data.type); if (node == null) { return(null); } // Init node state node.canvas = hostCanvas; node.name = node.Title; node.autoSize = node.DefaultSize; node.position = pos; ConnectionPortManager.UpdateConnectionPorts(node); if (init) { node.OnCreate(); } if (connectingPort != null) { // Handle auto-connection and link the output to the first compatible input for (int i = 0; i < node.connectionPorts.Count; i++) { if (node.connectionPorts[i].TryApplyConnection(connectingPort, true)) { break; } } } // Add node to host canvas hostCanvas.nodes.Add(node); if (!silent) { // Callbacks hostCanvas.OnNodeChange(connectingPort != null ? connectingPort.body : node); NodeEditorCallbacks.IssueOnAddNode(node); hostCanvas.Validate(); NodeEditor.RepaintClients(); } #if UNITY_EDITOR if (!silent) { List <ConnectionPort> connectedPorts = new List <ConnectionPort>(); foreach (ConnectionPort port in node.connectionPorts) { // 'Encode' connected ports in one list (double level cannot be serialized) foreach (ConnectionPort conn in port.connections) { connectedPorts.Add(conn); } connectedPorts.Add(null); } Node createdNode = node; UndoPro.UndoProManager.RecordOperation( () => NodeEditorUndoActions.ReinstateNode(createdNode, connectedPorts), () => NodeEditorUndoActions.RemoveNode(createdNode), "Create Node"); // Make sure the new node is in the memory dump NodeEditorUndoActions.CompleteSOMemoryDump(hostCanvas); } #endif return(node); }