public static NodeCanvas LoadNodeCanvas(string path, bool createWorkingCopy) { if (!File.Exists(path)) { throw new UnityException("Cannot Load NodeCanvas: File '" + path + "' deos not exist!"); } NodeCanvas nodeCanvas = ResourceManager.LoadResource <NodeCanvas>(path); if (nodeCanvas == null) { throw new UnityException("Cannot Load NodeCanvas: The file at the specified path '" + path + "' is no valid save file as it does not contain a NodeCanvas!"); } #if UNITY_EDITOR if (!Application.isPlaying && (nodeCanvas.editorStates == null || nodeCanvas.editorStates.Length == 0)) { nodeCanvas.editorStates = ResourceManager.LoadResources <NodeEditorState>(path); } #endif ProcessCanvas(ref nodeCanvas, createWorkingCopy); #if UNITY_EDITOR UnityEditor.AssetDatabase.Refresh(); #endif NodeEditorCallbacks.IssueOnLoadCanvas(nodeCanvas); return(nodeCanvas); }
public static Node Create(string nodeID, Vector2 position, NodeOutput connectingOutput) { Node node = NodeTypes.getDefaultNode(nodeID); if (node == null) { throw new UnityException("Cannot create Node with id " + nodeID + " as no such Node type is registered!"); } node = node.Create(position); node.InitBase(); if (connectingOutput != null) { foreach (NodeInput input in node.Inputs) { if (input.TryApplyConnection(connectingOutput)) { break; } } } NodeEditorCallbacks.IssueOnAddNode(node); return(node); }
public void Delete() { if (!NodeEditor.curNodeCanvas.nodes.Contains(this)) { throw new UnityException("The Node " + name + " does not exist on the Canvas " + NodeEditor.curNodeCanvas.name + "!"); } NodeEditorCallbacks.IssueOnDeleteNode(this); NodeEditor.curNodeCanvas.nodes.Remove(this); for (int outCnt = 0; outCnt < Outputs.Count; outCnt++) { NodeOutput output = Outputs[outCnt]; while (output.connections.Count != 0) { output.connections[0].RemoveConnection(); } DestroyImmediate(output, true); } for (int inCnt = 0; inCnt < Inputs.Count; inCnt++) { NodeInput input = Inputs[inCnt]; if (input.connection != null) { input.connection.connections.Remove(input); } DestroyImmediate(input, true); } for (int knobCnt = 0; knobCnt < nodeKnobs.Count; knobCnt++) { if (nodeKnobs[knobCnt] != null) { DestroyImmediate(nodeKnobs[knobCnt], true); } } DestroyImmediate(this, true); }
public static void SaveNodeCanvas(string path, NodeCanvas nodeCanvas, bool createWorkingCopy) { #if !UNITY_EDITOR throw new System.NotImplementedException(); #else if (string.IsNullOrEmpty(path)) { throw new UnityException("Cannot save NodeCanvas: No spath specified to save the NodeCanvas " + (nodeCanvas != null ? nodeCanvas.name : "") + " to!"); } if (nodeCanvas == null) { throw new UnityException("Cannot save NodeCanvas: The specified NodeCanvas that should be saved to path " + path + " is null!"); } if (nodeCanvas.livesInScene) { Debug.LogWarning("Attempting to save scene canvas " + nodeCanvas.name + " to an asset, scene object references will be broken!" + (!createWorkingCopy ? " No workingCopy is going to be created, so your scene save is broken, too!" : "")); } #if UNITY_EDITOR if (!createWorkingCopy && UnityEditor.AssetDatabase.Contains(nodeCanvas) && UnityEditor.AssetDatabase.GetAssetPath(nodeCanvas) != path) { Debug.LogError("Trying to create a duplicate save file for '" + nodeCanvas.name + "'! Forcing to create a working copy!"); createWorkingCopy = true; } #endif #if UNITY_EDITOR nodeCanvas.BeforeSavingCanvas(); ProcessCanvas(ref nodeCanvas, createWorkingCopy); nodeCanvas.livesInScene = false; UnityEditor.AssetDatabase.CreateAsset(nodeCanvas, path); AddSubAssets(nodeCanvas.editorStates, nodeCanvas); foreach (Node node in nodeCanvas.nodes) { AddSubAsset(node, nodeCanvas); AddSubAssets(node.GetScriptableObjects(), node); foreach (NodeKnob knob in node.nodeKnobs) { AddSubAsset(knob, node); AddSubAssets(knob.GetScriptableObjects(), knob); } } UnityEditor.AssetDatabase.SaveAssets(); UnityEditor.AssetDatabase.Refresh(); #else // TODO: Node Editor: Need to implement ingame-saving (Resources, AsssetBundles, ... won't work) #endif NodeEditorCallbacks.IssueOnSaveCanvas(nodeCanvas); #endif }
public void RemoveConnection() { if (connection == null) { return; } NodeEditorCallbacks.IssueOnRemoveConnection(this); connection.connections.Remove(this); connection = null; NodeEditor.RecalculateFrom(body); }
protected static void ReassignOutputType(ref NodeOutput output, Type newOutputType) { Node body = output.body; string outputName = output.name; // Store all valid connections that are not affected by the type change IEnumerable <NodeInput> validConnections = output.connections.Where((NodeInput connection) => connection.typeData.Type.IsAssignableFrom(newOutputType)); // Delete the output of the old type output.Delete(); // Create Output with new type NodeEditorCallbacks.IssueOnAddNodeKnob(NodeOutput.Create(body, outputName, newOutputType.AssemblyQualifiedName)); output = body.Outputs[body.Outputs.Count - 1]; // Restore the valid connections foreach (NodeInput input in validConnections) { input.ApplyConnection(output); } }
private static void HandleNodeDragging(NodeEditorInputInfo inputInfo) { NodeEditorState state = inputInfo.editorState; if (state.dragNode) { if (state.selectedNode != null && GUIUtility.hotControl == 0) { state.dragOffset = inputInfo.inputPos - state.dragStart; state.selectedNode.rect.position = state.dragPos + state.dragOffset * state.zoom; NodeEditorCallbacks.IssueOnMoveNode(state.selectedNode); NodeEditor.RepaintClients(); } else { state.dragNode = false; } } }
private static void setupBaseFramework() { ResourceManager.SetDefaultResourcePath(editorPath + "EditorResources/"); ConnectionTypes.FetchTypes(); NodeTypes.FetchNodes(); NodeCanvasManager.GetAllCanvasTypes(); NodeEditorCallbacks.SetupReceivers(); NodeEditorCallbacks.IssueOnEditorStartUp(); NodeEditorInputSystem.SetupInput(); #if UNITY_EDITOR UnityEditor.EditorApplication.update -= Update; UnityEditor.EditorApplication.update += Update; #endif initiatedBase = true; }
protected static void ReassignInputType(ref NodeInput input, Type newInputType) { Node body = input.body; string inputName = input.name; // Store the valid connection if it's not affected by the type change NodeOutput validConnection = null; if (input.connection != null && newInputType.IsAssignableFrom(input.connection.typeData.Type)) { validConnection = input.connection; } // Delete the input of the old type input.Delete(); // Create Output with new type NodeEditorCallbacks.IssueOnAddNodeKnob(NodeInput.Create(body, inputName, newInputType.AssemblyQualifiedName)); input = body.Inputs[body.Inputs.Count - 1]; // Restore the valid connections if (validConnection != null) { input.ApplyConnection(validConnection); } }