diff --git a/Example/ExampleNodeGraph.asset b/Example/ExampleNodeGraph.asset index aa70b38..2215cf0 100644 --- a/Example/ExampleNodeGraph.asset +++ b/Example/ExampleNodeGraph.asset @@ -12,9 +12,11 @@ MonoBehaviour: m_Name: ExampleNodeGraph m_EditorClassIdentifier: nodes: - - {fileID: 114253037537709940} - - {fileID: 114976558807377558} ---- !u!114 &114253037537709940 + - {fileID: 114386305456729202} + - {fileID: 114862693020615806} + - {fileID: 114245132648392102} + - {fileID: 114602227203521556} +--- !u!114 &114245132648392102 MonoBehaviour: m_ObjectHideFlags: 0 m_PrefabParentObject: {fileID: 0} @@ -23,31 +25,33 @@ MonoBehaviour: m_Enabled: 1 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: 22e05e2a1f5ad7645850d52212143629, type: 3} - m_Name: Math + m_Name: MathNode m_EditorClassIdentifier: graph: {fileID: 11400000} - position: {x: 0, y: 0} + position: {x: -429.5, y: -110.5} inputs: - - node: {fileID: 114253037537709940} + - node: {fileID: 114245132648392102} _fieldName: a connections: [] _direction: 0 - - node: {fileID: 114253037537709940} + - node: {fileID: 114245132648392102} _fieldName: b connections: [] _direction: 0 outputs: - - node: {fileID: 114253037537709940} + - node: {fileID: 114245132648392102} _fieldName: result connections: - - fieldName: myFloat - node: {fileID: 114976558807377558} + - fieldName: a + node: {fileID: 114862693020615806} + - fieldName: value + node: {fileID: 114602227203521556} _direction: 1 - a: 5.1719556 - b: 2.0752559 + a: 4.771955 + b: 8.313191 result: 0 mathType: 0 ---- !u!114 &114976558807377558 +--- !u!114 &114386305456729202 MonoBehaviour: m_ObjectHideFlags: 0 m_PrefabParentObject: {fileID: 0} @@ -55,62 +59,74 @@ MonoBehaviour: m_GameObject: {fileID: 0} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 47bfd6281f41a10459bcb45bb4cd03f3, type: 3} - m_Name: TestNode + m_Script: {fileID: 11500000, guid: 98f6f901f0da53142b79277ea3f42518, type: 3} + m_Name: DisplayValue m_EditorClassIdentifier: graph: {fileID: 11400000} - position: {x: 271, y: -57} + position: {x: 23.5, y: -98.5} inputs: - - node: {fileID: 114976558807377558} - _fieldName: myString - connections: [] - _direction: 0 - - node: {fileID: 114976558807377558} - _fieldName: myVec2 - connections: [] - _direction: 0 - - node: {fileID: 114976558807377558} - _fieldName: myVec3 - connections: [] - _direction: 0 - - node: {fileID: 114976558807377558} - _fieldName: myFloat + - node: {fileID: 114386305456729202} + _fieldName: value connections: - fieldName: result - node: {fileID: 114253037537709940} + node: {fileID: 114862693020615806} _direction: 0 outputs: [] - myInt: 0 - myFloat: 0 - myDouble: 0 - myLong: 0 - myBool: 0 - myString: - myRect: - serializedVersion: 2 - x: 0 - y: 0 - width: 0 - height: 0 - myVec2: {x: 0, y: 0} - myVec3: {x: 0, y: 0, z: 0} - myVec4: {x: 0, y: 0, z: 0, w: 0} - myColor: {r: 0.25383312, g: 0.44852942, b: 0.628, a: 0.641} - myCurve: - serializedVersion: 2 - m_Curve: - - serializedVersion: 2 - time: 0 - value: 0 - inSlope: 0 - outSlope: 0 - tangentMode: 0 - - serializedVersion: 2 - time: 1 - value: 1 - inSlope: 2 - outSlope: 2 - tangentMode: 0 - m_PreInfinity: 2 - m_PostInfinity: 2 - m_RotationOrder: 4 + value: 4.01703 +--- !u!114 &114602227203521556 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 98f6f901f0da53142b79277ea3f42518, type: 3} + m_Name: DisplayValue + m_EditorClassIdentifier: + graph: {fileID: 11400000} + position: {x: -194.5, y: -140.5} + inputs: + - node: {fileID: 114602227203521556} + _fieldName: value + connections: + - fieldName: result + node: {fileID: 114245132648392102} + _direction: 0 + outputs: [] + value: 0 +--- !u!114 &114862693020615806 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 22e05e2a1f5ad7645850d52212143629, type: 3} + m_Name: MathNode + m_EditorClassIdentifier: + graph: {fileID: 11400000} + position: {x: -196.5, y: -45.5} + inputs: + - node: {fileID: 114862693020615806} + _fieldName: a + connections: + - fieldName: result + node: {fileID: 114245132648392102} + _direction: 0 + - node: {fileID: 114862693020615806} + _fieldName: b + connections: [] + _direction: 0 + outputs: + - node: {fileID: 114862693020615806} + _fieldName: result + connections: + - fieldName: value + node: {fileID: 114386305456729202} + _direction: 1 + a: 8.142139 + b: 3.53361 + result: 0 + mathType: 1 diff --git a/Example/Nodes/DisplayValue.cs b/Example/Nodes/DisplayValue.cs index d19dd21..1e376be 100644 --- a/Example/Nodes/DisplayValue.cs +++ b/Example/Nodes/DisplayValue.cs @@ -1,6 +1,9 @@ -using UnityEngine; - -public class DisplayValue : ExampleNodeBase { - [Input] public float value; +namespace ExampleNodes { + public class DisplayValue : Node { + [Input] public float value; + public float GetValue() { + return GetInputByFieldName("value", value); + } + } } diff --git a/Example/Nodes/Editor/DisplayValueEditor.cs b/Example/Nodes/Editor/DisplayValueEditor.cs index 7e7b395..c2a4366 100644 --- a/Example/Nodes/Editor/DisplayValueEditor.cs +++ b/Example/Nodes/Editor/DisplayValueEditor.cs @@ -3,16 +3,18 @@ using System.Collections.Generic; using UnityEngine; using UnityEditor; -[CustomNodeEditor(typeof(DisplayValue), "Display Value")] -public class DisplayValueEditor : NodeEditor { +namespace ExampleNodes { + [CustomNodeEditor(typeof(ExampleNodes.DisplayValue), "ExampleNodes/Display Value")] + public class DisplayValueEditor : NodeEditor { - protected override void OnBodyGUI(out Dictionary portPositions) { - base.OnBodyGUI(out portPositions); - EditorGUILayout.LabelField("Value: " + GetResult()); - } + protected override void OnBodyGUI(out Dictionary portPositions) { + base.OnBodyGUI(out portPositions); + EditorGUILayout.LabelField(GetResult().ToString()); + } - public float GetResult() { - ExampleNodeBase t = target as ExampleNodeBase; - return t.GetInputFloat("value"); + public float GetResult() { + DisplayValue t = target as DisplayValue; + return t.GetValue(); + } } } diff --git a/Example/Nodes/Editor/MathNodeEditor.cs b/Example/Nodes/Editor/MathNodeEditor.cs deleted file mode 100644 index 1dfb35d..0000000 --- a/Example/Nodes/Editor/MathNodeEditor.cs +++ /dev/null @@ -1,4 +0,0 @@ -[CustomNodeEditor(typeof(MathNode), "Math")] -public class AddNodeEditor : NodeEditor { - -} diff --git a/Example/Nodes/Editor/MathNodeEditor.cs.meta b/Example/Nodes/Editor/MathNodeEditor.cs.meta deleted file mode 100644 index 57c1490..0000000 --- a/Example/Nodes/Editor/MathNodeEditor.cs.meta +++ /dev/null @@ -1,12 +0,0 @@ -fileFormatVersion: 2 -guid: c4cc83d08877562419d86874dd3587e2 -timeCreated: 1505987983 -licenseType: Free -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Example/Nodes/ExampleNodeBase.cs b/Example/Nodes/ExampleNodeBase.cs deleted file mode 100644 index 705c6bc..0000000 --- a/Example/Nodes/ExampleNodeBase.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public abstract 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.GetOutputValue(); - 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 deleted file mode 100644 index 1cc1c43..0000000 --- a/Example/Nodes/ExampleNodeBase.cs.meta +++ /dev/null @@ -1,12 +0,0 @@ -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 374931d..65856f9 100644 --- a/Example/Nodes/MathNode.cs +++ b/Example/Nodes/MathNode.cs @@ -1,31 +1,32 @@ -using UnityEngine; +namespace ExampleNodes { + [System.Serializable] + public class MathNode : Node { + // Adding [Input] or [Output] is all you need to do to register a field as a valid port on your node + [Input] public float a; + [Input] public float b; + // The value of an output node field is not used for anything, but could be used for caching output results + [Output] public float result; -[System.Serializable] -public class MathNode : ExampleNodeBase { - [Input] public float a; - [Input] public float b; - [Output] public float result; - public enum MathType { Add, Subtract, Multiply, Divide} - public MathType mathType = MathType.Add; + // UNEC will display this as an editable field - just like the normal inspector would + public MathType mathType = MathType.Add; + public enum MathType { Add, Subtract, Multiply, Divide } - protected override void Init() { - name = "Math"; - } + // GetValue should be overridden to return a value for any specified output port + public override object GetValue(NodePort port) { - public override object GetValue(NodePort port) { - float a = GetInputFloat("a"); - float b = GetInputFloat("b"); - - switch(port.fieldName) { - case "result": - switch(mathType) { - case MathType.Add: return a + b; + // Get new a and b values from input connections. Fallback to field values if input is not connected + float a = GetInputByFieldName("a", this.a); + float b = GetInputByFieldName("b", this.b); + + // After you've gotten your input values, you can perform your calculations and return a value + if (port.fieldName == "result") + switch (mathType) { + case MathType.Add: default: return a + b; case MathType.Subtract: return a - b; case MathType.Multiply: return a * b; case MathType.Divide: return a / b; } - break; + else return 0f; } - return 0f; } -} +} \ No newline at end of file diff --git a/Example/Nodes/TestNode.cs b/Example/Nodes/TestNode.cs deleted file mode 100644 index dd5b44b..0000000 --- a/Example/Nodes/TestNode.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class TestNode : Node { - - public int myInt; - [Input] public float myFloat; - public double myDouble; - public long myLong; - public bool myBool; - [Input] public string myString; - public Rect myRect; - [Input] public Vector2 myVec2; - [Input] public Vector3 myVec3; - public Vector4 myVec4; - public Color myColor; - public AnimationCurve myCurve; -} diff --git a/Example/Nodes/TestNode.cs.meta b/Example/Nodes/TestNode.cs.meta deleted file mode 100644 index 93b01d2..0000000 --- a/Example/Nodes/TestNode.cs.meta +++ /dev/null @@ -1,12 +0,0 @@ -fileFormatVersion: 2 -guid: 47bfd6281f41a10459bcb45bb4cd03f3 -timeCreated: 1507990362 -licenseType: Free -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: