From 22e537be4abe4888ded9a91f7f621f9162aeae6c Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Wed, 17 Jan 2018 11:12:51 +0100 Subject: [PATCH] Added state machine example --- Example/ExampleNodeGraph.cs | 8 - Example/Nodes/DisplayValue.cs | 14 - Example/Nodes/Editor/DisplayValueEditor.cs | 15 - Example.meta => Examples.meta | 0 Examples/MathGraph.meta | 10 + Examples/MathGraph/Editor.meta | 10 + Examples/MathGraph/Editor/MathGraphEditor.cs | 20 ++ .../MathGraph/Editor/MathGraphEditor.cs.meta | 13 + Examples/MathGraph/MathGraph.cs | 8 + .../MathGraph/MathGraph.cs.meta | 0 .../MathGraph/New Math Graph.asset | 266 ++++++++++-------- .../MathGraph/New Math Graph.asset.meta | 4 +- {Example => Examples/MathGraph}/Nodes.meta | 0 Examples/MathGraph/Nodes/DisplayValue.cs | 20 ++ .../MathGraph}/Nodes/DisplayValue.cs.meta | 0 .../MathGraph}/Nodes/Editor.meta | 0 .../Nodes/Editor/DisplayValueEditor.cs | 27 ++ .../Nodes/Editor/DisplayValueEditor.cs.meta | 0 .../MathGraph}/Nodes/MathNode.cs | 4 +- .../MathGraph}/Nodes/MathNode.cs.meta | 0 .../MathGraph}/Nodes/Vector.cs | 3 +- .../MathGraph}/Nodes/Vector.cs.meta | 0 Examples/StateMachine.meta | 10 + Examples/StateMachine/Editor.meta | 10 + .../StateMachine/Editor/StateGraphEditor.cs | 21 ++ .../Editor/StateGraphEditor.cs.meta | 13 + Examples/StateMachine/New State Graph.asset | 128 +++++++++ .../StateMachine/New State Graph.asset.meta | 10 + Examples/StateMachine/Nodes.meta | 10 + Examples/StateMachine/Nodes/Editor.meta | 10 + .../Nodes/Editor/StateNodeEditor.cs | 30 ++ .../Nodes/Editor/StateNodeEditor.cs.meta | 13 + Examples/StateMachine/Nodes/StateNode.cs | 39 +++ Examples/StateMachine/Nodes/StateNode.cs.meta | 13 + Examples/StateMachine/README.md | 14 + Examples/StateMachine/StateGraph.cs | 16 ++ Examples/StateMachine/StateGraph.cs.meta | 13 + 37 files changed, 606 insertions(+), 166 deletions(-) delete mode 100644 Example/ExampleNodeGraph.cs delete mode 100644 Example/Nodes/DisplayValue.cs delete mode 100644 Example/Nodes/Editor/DisplayValueEditor.cs rename Example.meta => Examples.meta (100%) create mode 100644 Examples/MathGraph.meta create mode 100644 Examples/MathGraph/Editor.meta create mode 100644 Examples/MathGraph/Editor/MathGraphEditor.cs create mode 100644 Examples/MathGraph/Editor/MathGraphEditor.cs.meta create mode 100644 Examples/MathGraph/MathGraph.cs rename Example/ExampleNodeGraph.cs.meta => Examples/MathGraph/MathGraph.cs.meta (100%) rename Example/ExampleNodeGraph.asset => Examples/MathGraph/New Math Graph.asset (64%) rename Example/ExampleNodeGraph.asset.meta => Examples/MathGraph/New Math Graph.asset.meta (72%) rename {Example => Examples/MathGraph}/Nodes.meta (100%) create mode 100644 Examples/MathGraph/Nodes/DisplayValue.cs rename {Example => Examples/MathGraph}/Nodes/DisplayValue.cs.meta (100%) rename {Example => Examples/MathGraph}/Nodes/Editor.meta (100%) create mode 100644 Examples/MathGraph/Nodes/Editor/DisplayValueEditor.cs rename {Example => Examples/MathGraph}/Nodes/Editor/DisplayValueEditor.cs.meta (100%) rename {Example => Examples/MathGraph}/Nodes/MathNode.cs (97%) rename {Example => Examples/MathGraph}/Nodes/MathNode.cs.meta (100%) rename {Example => Examples/MathGraph}/Nodes/Vector.cs (91%) rename {Example => Examples/MathGraph}/Nodes/Vector.cs.meta (100%) create mode 100644 Examples/StateMachine.meta create mode 100644 Examples/StateMachine/Editor.meta create mode 100644 Examples/StateMachine/Editor/StateGraphEditor.cs create mode 100644 Examples/StateMachine/Editor/StateGraphEditor.cs.meta create mode 100644 Examples/StateMachine/New State Graph.asset create mode 100644 Examples/StateMachine/New State Graph.asset.meta create mode 100644 Examples/StateMachine/Nodes.meta create mode 100644 Examples/StateMachine/Nodes/Editor.meta create mode 100644 Examples/StateMachine/Nodes/Editor/StateNodeEditor.cs create mode 100644 Examples/StateMachine/Nodes/Editor/StateNodeEditor.cs.meta create mode 100644 Examples/StateMachine/Nodes/StateNode.cs create mode 100644 Examples/StateMachine/Nodes/StateNode.cs.meta create mode 100644 Examples/StateMachine/README.md create mode 100644 Examples/StateMachine/StateGraph.cs create mode 100644 Examples/StateMachine/StateGraph.cs.meta diff --git a/Example/ExampleNodeGraph.cs b/Example/ExampleNodeGraph.cs deleted file mode 100644 index c9a7fa1..0000000 --- a/Example/ExampleNodeGraph.cs +++ /dev/null @@ -1,8 +0,0 @@ -using UnityEngine; -using System; -using XNode; - -/// Defines an example nodegraph. -[Serializable, CreateAssetMenu(fileName = "ExampleNodeGraph", menuName = "Node Graph/Example")] -public class ExampleNodeGraph : XNode.NodeGraph { -} diff --git a/Example/Nodes/DisplayValue.cs b/Example/Nodes/DisplayValue.cs deleted file mode 100644 index 3f5a317..0000000 --- a/Example/Nodes/DisplayValue.cs +++ /dev/null @@ -1,14 +0,0 @@ -using XNode; - -namespace BasicNodes { - public class DisplayValue : XNode.Node { - protected override void Init() { - base.Init(); - if (!HasPort("input")) AddInstanceInput(typeof(object), ConnectionType.Override ,"input"); - } - - public override object GetValue(XNode.NodePort port) { - return GetInputValue("input"); - } - } -} diff --git a/Example/Nodes/Editor/DisplayValueEditor.cs b/Example/Nodes/Editor/DisplayValueEditor.cs deleted file mode 100644 index c7482c4..0000000 --- a/Example/Nodes/Editor/DisplayValueEditor.cs +++ /dev/null @@ -1,15 +0,0 @@ -using UnityEditor; -using XNodeEditor; - -namespace BasicNodes { - [CustomNodeEditor(typeof(DisplayValue))] - public class DisplayValueEditor : NodeEditor { - - public override void OnBodyGUI() { - base.OnBodyGUI(); - NodeEditorGUILayout.PortField(target.GetInputPort("input")); - object obj = target.GetValue(null); - if (obj != null) EditorGUILayout.LabelField(obj.ToString()); - } - } -} \ No newline at end of file diff --git a/Example.meta b/Examples.meta similarity index 100% rename from Example.meta rename to Examples.meta diff --git a/Examples/MathGraph.meta b/Examples/MathGraph.meta new file mode 100644 index 0000000..1910f12 --- /dev/null +++ b/Examples/MathGraph.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: 852631db1e0f047478da88918f49e4a3 +folderAsset: yes +timeCreated: 1516177447 +licenseType: Free +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/MathGraph/Editor.meta b/Examples/MathGraph/Editor.meta new file mode 100644 index 0000000..be3526d --- /dev/null +++ b/Examples/MathGraph/Editor.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: 642e362943557314392fc62290a8e242 +folderAsset: yes +timeCreated: 1516180279 +licenseType: Free +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/MathGraph/Editor/MathGraphEditor.cs b/Examples/MathGraph/Editor/MathGraphEditor.cs new file mode 100644 index 0000000..06cd431 --- /dev/null +++ b/Examples/MathGraph/Editor/MathGraphEditor.cs @@ -0,0 +1,20 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using XNode.Examples; + +namespace XNodeEditor.Examples { + [CustomNodeGraphEditor(typeof(MathGraph))] + public class MathGraphEditor : NodeGraphEditor { + + /// + /// Overriding GetNodePath lets you control if and how nodes are categorized. + /// In this example we are sorting out all node types that are not in the XNode.Examples namespace. + /// + public override string GetNodePath(System.Type type) { + if (type.Namespace == "XNode.Examples.MathNodes") { + return base.GetNodePath(type).Replace("X Node/Examples/Math Nodes/", ""); + } else return null; + } + } +} \ No newline at end of file diff --git a/Examples/MathGraph/Editor/MathGraphEditor.cs.meta b/Examples/MathGraph/Editor/MathGraphEditor.cs.meta new file mode 100644 index 0000000..89bd3df --- /dev/null +++ b/Examples/MathGraph/Editor/MathGraphEditor.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 3f6ed0407f1f4ad45bf697d7798ddf0d +timeCreated: 1516180270 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/MathGraph/MathGraph.cs b/Examples/MathGraph/MathGraph.cs new file mode 100644 index 0000000..420ce64 --- /dev/null +++ b/Examples/MathGraph/MathGraph.cs @@ -0,0 +1,8 @@ +using System; +using UnityEngine; + +namespace XNode.Examples { + /// Defines an example nodegraph that can be created as an asset in the Project window. + [Serializable, CreateAssetMenu(fileName = "New Math Graph", menuName = "xNode Examples/Math Graph")] + public class MathGraph : XNode.NodeGraph { } +} \ No newline at end of file diff --git a/Example/ExampleNodeGraph.cs.meta b/Examples/MathGraph/MathGraph.cs.meta similarity index 100% rename from Example/ExampleNodeGraph.cs.meta rename to Examples/MathGraph/MathGraph.cs.meta diff --git a/Example/ExampleNodeGraph.asset b/Examples/MathGraph/New Math Graph.asset similarity index 64% rename from Example/ExampleNodeGraph.asset rename to Examples/MathGraph/New Math Graph.asset index a50d83c..9b22fb5 100644 --- a/Example/ExampleNodeGraph.asset +++ b/Examples/MathGraph/New Math Graph.asset @@ -9,119 +9,14 @@ MonoBehaviour: m_Enabled: 1 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: a6399826e2c44b447b32a3ed06646162, type: 3} - m_Name: ExampleNodeGraph + m_Name: New Math Graph m_EditorClassIdentifier: nodes: - - {fileID: 114708853913061688} - - {fileID: 114511978881715272} - - {fileID: 114509033286994848} - - {fileID: 114245496101350052} ---- !u!114 &114245496101350052 -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: -168, y: 8} - ports: - keys: - - input - values: - - _fieldName: input - _node: {fileID: 114245496101350052} - _typeQualifiedName: System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, - PublicKeyToken=b77a5c561934e089 - connections: - - fieldName: result - node: {fileID: 114511978881715272} - _direction: 0 - _connectionType: 1 - _dynamic: 1 ---- !u!114 &114509033286994848 -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: 72, y: -72} - ports: - keys: - - input - values: - - _fieldName: input - _node: {fileID: 114509033286994848} - _typeQualifiedName: System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, - PublicKeyToken=b77a5c561934e089 - connections: - - fieldName: vector - node: {fileID: 114708853913061688} - _direction: 0 - _connectionType: 1 - _dynamic: 1 ---- !u!114 &114511978881715272 -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: 19e541bba2a188f4a84c6f3718ee6d55, type: 3} - m_Name: MathNode - m_EditorClassIdentifier: - graph: {fileID: 11400000} - position: {x: -472, y: -120} - ports: - keys: - - a - - b - - result - values: - - _fieldName: a - _node: {fileID: 114511978881715272} - _typeQualifiedName: System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, - PublicKeyToken=b77a5c561934e089 - connections: [] - _direction: 0 - _connectionType: 0 - _dynamic: 0 - - _fieldName: b - _node: {fileID: 114511978881715272} - _typeQualifiedName: System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, - PublicKeyToken=b77a5c561934e089 - connections: [] - _direction: 0 - _connectionType: 0 - _dynamic: 0 - - _fieldName: result - _node: {fileID: 114511978881715272} - _typeQualifiedName: System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, - PublicKeyToken=b77a5c561934e089 - connections: - - fieldName: x - node: {fileID: 114708853913061688} - - fieldName: input - node: {fileID: 114245496101350052} - _direction: 1 - _connectionType: 0 - _dynamic: 0 - a: 6.48 - b: 7.59 - result: 14.07 - mathType: 0 ---- !u!114 &114708853913061688 + - {fileID: 114652361136345764} + - {fileID: 114067149339165002} + - {fileID: 114441428018438906} + - {fileID: 114775482956350040} +--- !u!114 &114067149339165002 MonoBehaviour: m_ObjectHideFlags: 0 m_PrefabParentObject: {fileID: 0} @@ -133,7 +28,7 @@ MonoBehaviour: m_Name: Vector m_EditorClassIdentifier: graph: {fileID: 11400000} - position: {x: -168, y: -120} + position: {x: -136, y: -136} ports: keys: - x @@ -142,25 +37,27 @@ MonoBehaviour: - vector values: - _fieldName: x - _node: {fileID: 114708853913061688} + _node: {fileID: 114067149339165002} _typeQualifiedName: System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 connections: - fieldName: result - node: {fileID: 114511978881715272} + node: {fileID: 114652361136345764} _direction: 0 _connectionType: 0 _dynamic: 0 - _fieldName: y - _node: {fileID: 114708853913061688} + _node: {fileID: 114067149339165002} _typeQualifiedName: System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - connections: [] + connections: + - fieldName: result + node: {fileID: 114775482956350040} _direction: 0 _connectionType: 0 _dynamic: 0 - _fieldName: z - _node: {fileID: 114708853913061688} + _node: {fileID: 114067149339165002} _typeQualifiedName: System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 connections: [] @@ -168,16 +65,141 @@ MonoBehaviour: _connectionType: 0 _dynamic: 0 - _fieldName: vector - _node: {fileID: 114708853913061688} + _node: {fileID: 114067149339165002} _typeQualifiedName: UnityEngine.Vector3, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null connections: - fieldName: input - node: {fileID: 114509033286994848} + node: {fileID: 114441428018438906} _direction: 1 _connectionType: 0 _dynamic: 0 x: 0 - y: 2.6412349 - z: 14.33477 - vector: {x: 14.07, y: 2.6412349, z: 14.33477} + y: 0 + z: 0 + vector: {x: -0.22000003, y: 2.14, z: 0} +--- !u!114 &114441428018438906 +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: 120, y: -88} + ports: + keys: + - input + values: + - _fieldName: input + _node: {fileID: 114441428018438906} + _typeQualifiedName: XNode.Examples.MathNodes.DisplayValue+Anything, Assembly-CSharp, + Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + connections: + - fieldName: vector + node: {fileID: 114067149339165002} + _direction: 0 + _connectionType: 1 + _dynamic: 0 +--- !u!114 &114652361136345764 +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: 19e541bba2a188f4a84c6f3718ee6d55, type: 3} + m_Name: MathNode + m_EditorClassIdentifier: + graph: {fileID: 11400000} + position: {x: -392, y: -232} + ports: + keys: + - a + - b + - result + values: + - _fieldName: a + _node: {fileID: 114652361136345764} + _typeQualifiedName: System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + connections: [] + _direction: 0 + _connectionType: 0 + _dynamic: 0 + - _fieldName: b + _node: {fileID: 114652361136345764} + _typeQualifiedName: System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + connections: [] + _direction: 0 + _connectionType: 0 + _dynamic: 0 + - _fieldName: result + _node: {fileID: 114652361136345764} + _typeQualifiedName: System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + connections: + - fieldName: x + node: {fileID: 114067149339165002} + _direction: 1 + _connectionType: 0 + _dynamic: 0 + a: 0.32 + b: 0.54 + result: -0.22000003 + mathType: 1 +--- !u!114 &114775482956350040 +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: 19e541bba2a188f4a84c6f3718ee6d55, type: 3} + m_Name: Math Node + m_EditorClassIdentifier: + graph: {fileID: 11400000} + position: {x: -392, y: -72} + ports: + keys: + - a + - b + - result + values: + - _fieldName: a + _node: {fileID: 114775482956350040} + _typeQualifiedName: System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + connections: [] + _direction: 0 + _connectionType: 0 + _dynamic: 0 + - _fieldName: b + _node: {fileID: 114775482956350040} + _typeQualifiedName: System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + connections: [] + _direction: 0 + _connectionType: 0 + _dynamic: 0 + - _fieldName: result + _node: {fileID: 114775482956350040} + _typeQualifiedName: System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + connections: + - fieldName: y + node: {fileID: 114067149339165002} + _direction: 1 + _connectionType: 0 + _dynamic: 0 + a: 0.81 + b: 1.33 + result: 2.14 + mathType: 0 diff --git a/Example/ExampleNodeGraph.asset.meta b/Examples/MathGraph/New Math Graph.asset.meta similarity index 72% rename from Example/ExampleNodeGraph.asset.meta rename to Examples/MathGraph/New Math Graph.asset.meta index 0ef23bc..61fd97e 100644 --- a/Example/ExampleNodeGraph.asset.meta +++ b/Examples/MathGraph/New Math Graph.asset.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: cee47fa32ae90bb4f8f0bec6f186cb3b -timeCreated: 1509308927 +guid: 97b11561dfa2a2646a1217518e90411b +timeCreated: 1516179614 licenseType: Free NativeFormatImporter: externalObjects: {} diff --git a/Example/Nodes.meta b/Examples/MathGraph/Nodes.meta similarity index 100% rename from Example/Nodes.meta rename to Examples/MathGraph/Nodes.meta diff --git a/Examples/MathGraph/Nodes/DisplayValue.cs b/Examples/MathGraph/Nodes/DisplayValue.cs new file mode 100644 index 0000000..422c4c4 --- /dev/null +++ b/Examples/MathGraph/Nodes/DisplayValue.cs @@ -0,0 +1,20 @@ +namespace XNode.Examples.MathNodes { + + public class DisplayValue : XNode.Node { + + /// + /// Create an input port that only allows a single connection. + /// The backing value is not important, as we are only interested in the input value. + /// We are also acceptable of all input types, so any type will do, as long as it is serializable. + /// + [Input(ShowBackingValue.Never, ConnectionType.Override)] public Anything input; + + /// Get the value currently plugged in to this node + public object GetValue() { + return GetInputValue("input"); + } + + /// This class is defined for the sole purpose of being serializable + [System.Serializable] public class Anything {} + } +} diff --git a/Example/Nodes/DisplayValue.cs.meta b/Examples/MathGraph/Nodes/DisplayValue.cs.meta similarity index 100% rename from Example/Nodes/DisplayValue.cs.meta rename to Examples/MathGraph/Nodes/DisplayValue.cs.meta diff --git a/Example/Nodes/Editor.meta b/Examples/MathGraph/Nodes/Editor.meta similarity index 100% rename from Example/Nodes/Editor.meta rename to Examples/MathGraph/Nodes/Editor.meta diff --git a/Examples/MathGraph/Nodes/Editor/DisplayValueEditor.cs b/Examples/MathGraph/Nodes/Editor/DisplayValueEditor.cs new file mode 100644 index 0000000..706c5dc --- /dev/null +++ b/Examples/MathGraph/Nodes/Editor/DisplayValueEditor.cs @@ -0,0 +1,27 @@ +using UnityEditor; +using XNode.Examples.MathNodes; + +namespace XNodeEditor.Examples { + + /// + /// NodeEditor functions similarly to the Editor class, only it is xNode specific. + /// Custom node editors should have the CustomNodeEditor attribute that defines which node type it is an editor for. + /// + [CustomNodeEditor(typeof(DisplayValue))] + public class DisplayValueEditor : NodeEditor { + + /// Called whenever the xNode editor window is updated + public override void OnBodyGUI() { + + // Draw the default GUI first, so we don't have to do all of that manually. + base.OnBodyGUI(); + + // `target` points to the node, but it is of type `Node`, so cast it. + DisplayValue displayValueNode = target as DisplayValue; + + // Get the value from the node, and display it + object obj = displayValueNode.GetValue(); + if (obj != null) EditorGUILayout.LabelField(obj.ToString()); + } + } +} \ No newline at end of file diff --git a/Example/Nodes/Editor/DisplayValueEditor.cs.meta b/Examples/MathGraph/Nodes/Editor/DisplayValueEditor.cs.meta similarity index 100% rename from Example/Nodes/Editor/DisplayValueEditor.cs.meta rename to Examples/MathGraph/Nodes/Editor/DisplayValueEditor.cs.meta diff --git a/Example/Nodes/MathNode.cs b/Examples/MathGraph/Nodes/MathNode.cs similarity index 97% rename from Example/Nodes/MathNode.cs rename to Examples/MathGraph/Nodes/MathNode.cs index b9c1e83..606e038 100644 --- a/Example/Nodes/MathNode.cs +++ b/Examples/MathGraph/Nodes/MathNode.cs @@ -1,6 +1,4 @@ -using XNode; - -namespace BasicNodes { +namespace XNode.Examples.MathNodes { [System.Serializable] public class MathNode : XNode.Node { // Adding [Input] or [Output] is all you need to do to register a field as a valid port on your node diff --git a/Example/Nodes/MathNode.cs.meta b/Examples/MathGraph/Nodes/MathNode.cs.meta similarity index 100% rename from Example/Nodes/MathNode.cs.meta rename to Examples/MathGraph/Nodes/MathNode.cs.meta diff --git a/Example/Nodes/Vector.cs b/Examples/MathGraph/Nodes/Vector.cs similarity index 91% rename from Example/Nodes/Vector.cs rename to Examples/MathGraph/Nodes/Vector.cs index 7141d2e..1f43e6f 100644 --- a/Example/Nodes/Vector.cs +++ b/Examples/MathGraph/Nodes/Vector.cs @@ -1,7 +1,6 @@ using UnityEngine; -using XNode; -namespace BasicNodes { +namespace XNode.Examples.MathNodes { public class Vector : XNode.Node { [Input] public float x, y, z; [Output] public Vector3 vector; diff --git a/Example/Nodes/Vector.cs.meta b/Examples/MathGraph/Nodes/Vector.cs.meta similarity index 100% rename from Example/Nodes/Vector.cs.meta rename to Examples/MathGraph/Nodes/Vector.cs.meta diff --git a/Examples/StateMachine.meta b/Examples/StateMachine.meta new file mode 100644 index 0000000..2349392 --- /dev/null +++ b/Examples/StateMachine.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: e8aa31d972c52204dbdcf109681928c8 +folderAsset: yes +timeCreated: 1516177052 +licenseType: Free +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/StateMachine/Editor.meta b/Examples/StateMachine/Editor.meta new file mode 100644 index 0000000..598278a --- /dev/null +++ b/Examples/StateMachine/Editor.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: 9abb31871fcc4b840baedb640fb6815e +folderAsset: yes +timeCreated: 1516181202 +licenseType: Free +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/StateMachine/Editor/StateGraphEditor.cs b/Examples/StateMachine/Editor/StateGraphEditor.cs new file mode 100644 index 0000000..f1b9cab --- /dev/null +++ b/Examples/StateMachine/Editor/StateGraphEditor.cs @@ -0,0 +1,21 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using XNode.Examples.StateGraph; +using XNodeEditor; + +namespace XNodeEditor.Examples { + [CustomNodeGraphEditor(typeof(StateGraph))] + public class StateGraphEditor : NodeGraphEditor { + + /// + /// Overriding GetNodePath lets you control if and how nodes are categorized. + /// In this example we are sorting out all node types that are not in the XNode.Examples namespace. + /// + public override string GetNodePath(System.Type type) { + if (type.Namespace == "XNode.Examples.StateGraph") { + return base.GetNodePath(type).Replace("X Node/Examples/State Graph/", ""); + } else return null; + } + } +} \ No newline at end of file diff --git a/Examples/StateMachine/Editor/StateGraphEditor.cs.meta b/Examples/StateMachine/Editor/StateGraphEditor.cs.meta new file mode 100644 index 0000000..5a85ea0 --- /dev/null +++ b/Examples/StateMachine/Editor/StateGraphEditor.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: c46ac2790aeee7341998d217311ba8d3 +timeCreated: 1516181207 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/StateMachine/New State Graph.asset b/Examples/StateMachine/New State Graph.asset new file mode 100644 index 0000000..ebbd886 --- /dev/null +++ b/Examples/StateMachine/New State Graph.asset @@ -0,0 +1,128 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +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: dad9e75908d47ae419dba5bc800df549, type: 3} + m_Name: New State Graph + m_EditorClassIdentifier: + nodes: + - {fileID: 114319284393922042} + - {fileID: 114471802820302434} + - {fileID: 114232340257100334} + current: {fileID: 114232340257100334} +--- !u!114 &114232340257100334 +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: 32ef86e1b73c7d642acaa1b75f66bbbb, type: 3} + m_Name: State Node + m_EditorClassIdentifier: + graph: {fileID: 11400000} + position: {x: -248, y: -392} + ports: + keys: + - enter + - exit + values: + - _fieldName: enter + _node: {fileID: 114232340257100334} + _typeQualifiedName: XNode.Examples.StateGraph.StateNode+Empty, Assembly-CSharp, + Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + connections: [] + _direction: 0 + _connectionType: 0 + _dynamic: 0 + - _fieldName: exit + _node: {fileID: 114232340257100334} + _typeQualifiedName: XNode.Examples.StateGraph.StateNode+Empty, Assembly-CSharp, + Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + connections: + - fieldName: enter + node: {fileID: 114319284393922042} + _direction: 1 + _connectionType: 0 + _dynamic: 0 +--- !u!114 &114319284393922042 +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: 32ef86e1b73c7d642acaa1b75f66bbbb, type: 3} + m_Name: State Node + m_EditorClassIdentifier: + graph: {fileID: 11400000} + position: {x: -392, y: -168} + ports: + keys: + - enter + - exit + values: + - _fieldName: enter + _node: {fileID: 114319284393922042} + _typeQualifiedName: XNode.Examples.StateGraph.StateNode+Empty, Assembly-CSharp, + Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + connections: + - fieldName: exit + node: {fileID: 114232340257100334} + _direction: 0 + _connectionType: 0 + _dynamic: 0 + - _fieldName: exit + _node: {fileID: 114319284393922042} + _typeQualifiedName: XNode.Examples.StateGraph.StateNode+Empty, Assembly-CSharp, + Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + connections: + - fieldName: enter + node: {fileID: 114471802820302434} + _direction: 1 + _connectionType: 0 + _dynamic: 0 +--- !u!114 &114471802820302434 +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: 32ef86e1b73c7d642acaa1b75f66bbbb, type: 3} + m_Name: State Node + m_EditorClassIdentifier: + graph: {fileID: 11400000} + position: {x: -56, y: -168} + ports: + keys: + - enter + - exit + values: + - _fieldName: enter + _node: {fileID: 114471802820302434} + _typeQualifiedName: XNode.Examples.StateGraph.StateNode+Empty, Assembly-CSharp, + Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + connections: + - fieldName: exit + node: {fileID: 114319284393922042} + _direction: 0 + _connectionType: 0 + _dynamic: 0 + - _fieldName: exit + _node: {fileID: 114471802820302434} + _typeQualifiedName: XNode.Examples.StateGraph.StateNode+Empty, Assembly-CSharp, + Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + connections: [] + _direction: 1 + _connectionType: 0 + _dynamic: 0 diff --git a/Examples/StateMachine/New State Graph.asset.meta b/Examples/StateMachine/New State Graph.asset.meta new file mode 100644 index 0000000..5513aac --- /dev/null +++ b/Examples/StateMachine/New State Graph.asset.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: 18b73d7e492ac594886d6afd816395ec +timeCreated: 1516181166 +licenseType: Free +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/StateMachine/Nodes.meta b/Examples/StateMachine/Nodes.meta new file mode 100644 index 0000000..7854c23 --- /dev/null +++ b/Examples/StateMachine/Nodes.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: 1e4f25b00cb54f84c82e84af31c8e056 +folderAsset: yes +timeCreated: 1514505844 +licenseType: Free +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/StateMachine/Nodes/Editor.meta b/Examples/StateMachine/Nodes/Editor.meta new file mode 100644 index 0000000..cb4202f --- /dev/null +++ b/Examples/StateMachine/Nodes/Editor.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: 61176f710e9af264db5db0a81711f423 +folderAsset: yes +timeCreated: 1514506327 +licenseType: Free +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/StateMachine/Nodes/Editor/StateNodeEditor.cs b/Examples/StateMachine/Nodes/Editor/StateNodeEditor.cs new file mode 100644 index 0000000..f9d6b9e --- /dev/null +++ b/Examples/StateMachine/Nodes/Editor/StateNodeEditor.cs @@ -0,0 +1,30 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; +using XNode.Examples.StateGraph; + +namespace XNodeEditor.Examples { + [CustomNodeEditor(typeof(StateNode))] + public class StateNodeEditor : NodeEditor { + + public override void OnHeaderGUI() { + GUI.color = Color.white; + StateNode node = target as StateNode; + StateGraph graph = node.graph as StateGraph; + if (graph.current == node) GUI.color = Color.blue; + string title = target.name; + GUILayout.Label(title, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30)); + GUI.color = Color.white; + } + + public override void OnBodyGUI() { + base.OnBodyGUI(); + StateNode node = target as StateNode; + StateGraph graph = node.graph as StateGraph; + if (GUILayout.Button("MoveNext Node")) node.MoveNext(); + if (GUILayout.Button("Continue Graph")) graph.Continue(); + if (GUILayout.Button("Set as current")) graph.current = node; + } + } +} \ No newline at end of file diff --git a/Examples/StateMachine/Nodes/Editor/StateNodeEditor.cs.meta b/Examples/StateMachine/Nodes/Editor/StateNodeEditor.cs.meta new file mode 100644 index 0000000..e5414c9 --- /dev/null +++ b/Examples/StateMachine/Nodes/Editor/StateNodeEditor.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: f59d4dfd6e73e6b4d9d14256bc7a20f3 +timeCreated: 1514506333 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/StateMachine/Nodes/StateNode.cs b/Examples/StateMachine/Nodes/StateNode.cs new file mode 100644 index 0000000..2bb7dc0 --- /dev/null +++ b/Examples/StateMachine/Nodes/StateNode.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace XNode.Examples.StateGraph { + public class StateNode : Node { + + [Input] public Empty enter; + [Output] public Empty exit; + + public void MoveNext() { + StateGraph fmGraph = graph as StateGraph; + + if (fmGraph.current != this) { + Debug.LogWarning("Node isn't active"); + return; + } + + NodePort exitPort = GetOutputPort("exit"); + + if (!exitPort.IsConnected) { + Debug.LogWarning("Node isn't connected"); + return; + } + + StateNode node = exitPort.Connection.node as StateNode; + node.OnEnter(); + } + + public void OnEnter() { + StateGraph fmGraph = graph as StateGraph; + fmGraph.current = this; + } + + [Serializable] + public class Empty { } + } +} \ No newline at end of file diff --git a/Examples/StateMachine/Nodes/StateNode.cs.meta b/Examples/StateMachine/Nodes/StateNode.cs.meta new file mode 100644 index 0000000..0443275 --- /dev/null +++ b/Examples/StateMachine/Nodes/StateNode.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 32ef86e1b73c7d642acaa1b75f66bbbb +timeCreated: 1514505861 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/StateMachine/README.md b/Examples/StateMachine/README.md new file mode 100644 index 0000000..8e7aa3e --- /dev/null +++ b/Examples/StateMachine/README.md @@ -0,0 +1,14 @@ +[![Discord](https://img.shields.io/discord/361769369404964864.svg)](https://discord.gg/qgPrHv4) +[![GitHub issues](https://img.shields.io/github/issues/Siccity/xNode.svg)](https://github.com/Siccity/xNode/issues) +[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/Siccity/xNode/master/LICENSE.md) +[![GitHub Wiki](https://img.shields.io/badge/wiki-available-brightgreen.svg)](https://github.com/Siccity/xNode/wiki) + +[Go to Downloads](https://github.com/Siccity/xNode/releases) + +### xNode State Machine (Example) +This example project should give you an understanding of how to handle traversing through nodes, one by one. +The same principle can be applied to dialogue graphs, and other simple decision makers. + + +Join the [Discord](https://discord.gg/qgPrHv4 "Join Discord server") server to leave feedback or get support. +Feel free to also leave suggestions/requests in the [issues](https://github.com/Siccity/xNode/issues "Go to Issues") page. diff --git a/Examples/StateMachine/StateGraph.cs b/Examples/StateMachine/StateGraph.cs new file mode 100644 index 0000000..8a136c2 --- /dev/null +++ b/Examples/StateMachine/StateGraph.cs @@ -0,0 +1,16 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace XNode.Examples.StateGraph { + [CreateAssetMenu(fileName = "New State Graph", menuName = "xNode Examples/State Graph")] + public class StateGraph : NodeGraph { + + // The current "active" node + public StateNode current; + + public void Continue() { + current.MoveNext(); + } + } +} \ No newline at end of file diff --git a/Examples/StateMachine/StateGraph.cs.meta b/Examples/StateMachine/StateGraph.cs.meta new file mode 100644 index 0000000..8e83d93 --- /dev/null +++ b/Examples/StateMachine/StateGraph.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: dad9e75908d47ae419dba5bc800df549 +timeCreated: 1514505839 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: