diff --git a/Example/ExampleNodeGraph.asset b/Example/ExampleNodeGraph.asset new file mode 100644 index 0000000..8e72bce Binary files /dev/null and b/Example/ExampleNodeGraph.asset differ diff --git a/Example/ExampleNodeGraph.asset.meta b/Example/ExampleNodeGraph.asset.meta new file mode 100644 index 0000000..6ec3d2f --- /dev/null +++ b/Example/ExampleNodeGraph.asset.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: e8c47bc953732464a9bf3a76273d99ef +timeCreated: 1507916591 +licenseType: Free +NativeFormatImporter: + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Example/Nodes/BaseNode.cs b/Example/Nodes/BaseNode.cs deleted file mode 100644 index 26d4a8d..0000000 --- a/Example/Nodes/BaseNode.cs +++ /dev/null @@ -1,15 +0,0 @@ -using UnityEngine; - -[System.Serializable] -public class BaseNode : Node { - - [Input] public string input; - [Output] public string output; - public bool concat; - [TextArea] - public string SomeString; - [Header("New stuff")] - public Color col; - public AnimationCurve anim; - public Vector3 vec; -} diff --git a/Example/Nodes/ConstantValue.cs b/Example/Nodes/ConstantValue.cs new file mode 100644 index 0000000..b0b99ce --- /dev/null +++ b/Example/Nodes/ConstantValue.cs @@ -0,0 +1,13 @@ +[System.Serializable] +public class ConstantValue : ExampleNodeBase { + public float a; + [Output] public float value; + + protected override void Init() { + name = "Constant Value"; + } + + public override object GetValue(NodePort port) { + return a; + } +} diff --git a/Example/Nodes/BaseNode.cs.meta b/Example/Nodes/ConstantValue.cs.meta similarity index 76% rename from Example/Nodes/BaseNode.cs.meta rename to Example/Nodes/ConstantValue.cs.meta index e525d86..e836841 100644 --- a/Example/Nodes/BaseNode.cs.meta +++ b/Example/Nodes/ConstantValue.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 23665941e8cd89a48b1272ac5fd6510c -timeCreated: 1505462705 +guid: 707240ce8955a0240a7c0c4177d83bf5 +timeCreated: 1505937586 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Example/Nodes/DisplayValue.cs b/Example/Nodes/DisplayValue.cs index 01a2b0f..d19dd21 100644 --- a/Example/Nodes/DisplayValue.cs +++ b/Example/Nodes/DisplayValue.cs @@ -1,5 +1,6 @@ using UnityEngine; -public class DisplayValue : Node { +public class DisplayValue : ExampleNodeBase { [Input] public float value; + } diff --git a/Example/Nodes/Editor/DisplayValueEditor.cs b/Example/Nodes/Editor/DisplayValueEditor.cs index 34276a8..0530c18 100644 --- a/Example/Nodes/Editor/DisplayValueEditor.cs +++ b/Example/Nodes/Editor/DisplayValueEditor.cs @@ -12,20 +12,7 @@ public class DisplayValueEditor : NodeEditor { } public float GetResult() { - float result = 0f; - NodePort port = target.GetInputByFieldName("value"); - if (port == null) return result; - int connectionCount = port.ConnectionCount; - for (int i = 0; i < connectionCount; i++) { - - NodePort connection = port.GetConnection(i); - if (connection == null) continue; - object obj = connection.GetValue(); - if (obj == null) continue; - - if (connection.type == typeof(int)) result += (int)obj; - else if (connection.type == typeof(float)) result += (float)obj; - } - return result; + ExampleNodeBase t = target as ExampleNodeBase; + return t.GetInputFloat("value"); } } diff --git a/Example/Nodes/ExampleNodeBase.cs b/Example/Nodes/ExampleNodeBase.cs new file mode 100644 index 0000000..c47cff0 --- /dev/null +++ b/Example/Nodes/ExampleNodeBase.cs @@ -0,0 +1,22 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class ExampleNodeBase : Node { + + public float GetInputFloat(string fieldName) { + float result = 0f; + NodePort port = GetInputByFieldName(fieldName); + if (port == null) return result; + int connectionCount = port.ConnectionCount; + for (int i = 0; i < connectionCount; i++) { + NodePort connection = port.GetConnection(i); + if (connection == null) continue; + object obj = connection.GetValue(); + if (obj == null) continue; + if (connection.type == typeof(int)) result += (int)obj; + else if (connection.type == typeof(float)) result += (float)obj; + } + return result; + } +} diff --git a/Example/Nodes/ExampleNodeBase.cs.meta b/Example/Nodes/ExampleNodeBase.cs.meta new file mode 100644 index 0000000..1cc1c43 --- /dev/null +++ b/Example/Nodes/ExampleNodeBase.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 923bada49e668fd4a98b04fcb49999d7 +timeCreated: 1507917099 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Example/Nodes/MathNode.cs b/Example/Nodes/MathNode.cs index 32aa322..475698d 100644 --- a/Example/Nodes/MathNode.cs +++ b/Example/Nodes/MathNode.cs @@ -1,9 +1,9 @@ using UnityEngine; [System.Serializable] -public class MathNode : Node { - public float a; - public float b; +public class MathNode : ExampleNodeBase { + [Input] public float c; + [Input] public float b; [Output] public float result; public enum MathType { Add, Subtract, Multiply, Divide} public MathType mathType = MathType.Add; @@ -13,6 +13,9 @@ public class MathNode : Node { } public override object GetValue(NodePort port) { + float a = GetInputFloat("c"); + float b = GetInputFloat("b"); + switch(port.fieldName) { case "result": switch(mathType) { @@ -25,8 +28,4 @@ public class MathNode : Node { } return 0f; } - - public override void OnCreateConnection(NodePort from, NodePort to) { - - } } diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index cf0c44e..d04f747 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -128,7 +128,9 @@ public partial class NodeEditorWindow { if (!portConnectionPoints.ContainsKey(output)) continue; Vector2 from = _portConnectionPoints[output].center; for (int k = 0; k < output.ConnectionCount; k++) { + NodePort input = output.GetConnection(k); + if (input == null) return; //If a script has been updated and the port doesn't exist, it is removed and null is returned. If this happens, return. Vector2 to = _portConnectionPoints[input].center; DrawConnection(from, to, NodeEditorUtilities.GetTypeColor(output.type)); } diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs index 808fabd..1980935 100644 --- a/Scripts/NodePort.cs +++ b/Scripts/NodePort.cs @@ -77,7 +77,16 @@ public class NodePort { } public NodePort GetConnection(int i) { + //If the connection is broken for some reason, remove it. + if (connections[i].node == null || string.IsNullOrEmpty(connections[i].fieldName)) { + connections.RemoveAt(i); + return null; + } NodePort port = connections[i].node.GetPortByFieldName(connections[i].fieldName); + if (port == null) { + connections.RemoveAt(i); + return null; + } return port; }