From d88cf5b53d43482b39d103568c600d95b0b4bbd2 Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Fri, 22 Sep 2017 12:46:45 +0200 Subject: [PATCH] OnValidate added for nodes --- Examples/Nodes/Editor/MathNodeEditor.cs | 8 ++------ Examples/Nodes/MathNode.cs | 11 ++++++++++- Scripts/Editor/NodeEditorGUI.cs | 11 +++++++++++ Scripts/Node.cs | 7 +++++-- Scripts/NodePort.cs | 5 ++--- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Examples/Nodes/Editor/MathNodeEditor.cs b/Examples/Nodes/Editor/MathNodeEditor.cs index d244742..7dbc033 100644 --- a/Examples/Nodes/Editor/MathNodeEditor.cs +++ b/Examples/Nodes/Editor/MathNodeEditor.cs @@ -1,11 +1,7 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -[CustomNodeEditor(typeof(MathNode), "Math")] +[CustomNodeEditor(typeof(MathNode), "Math")] public class AddNodeEditor : NodeEditor { public override void OnNodeGUI() { - GUILayout.Label("YEAH CUSTOM"); + base.OnNodeGUI(); } } diff --git a/Examples/Nodes/MathNode.cs b/Examples/Nodes/MathNode.cs index adf593c..8529cbc 100644 --- a/Examples/Nodes/MathNode.cs +++ b/Examples/Nodes/MathNode.cs @@ -3,8 +3,9 @@ [System.Serializable] public class MathNode : Node { - public int someValue; + public enum ValueType { Float, Int } public enum MathType { Add, Subtract, Multiply, Divide} + public ValueType valueType = ValueType.Float; public MathType mathType = MathType.Add; protected override void Init() { @@ -14,4 +15,12 @@ public class MathNode : Node { outputs = new NodePort[1]; outputs[0] = CreateNodeOutput("Result", typeof(float)); } + + public void OnValidate() { + System.Type type = typeof(int); + if (valueType == ValueType.Float) type = typeof(float); + inputs[0].type = type; + inputs[1].type = type; + outputs[0].type = type; + } } diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 6e5ae5d..495ad00 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -176,8 +176,19 @@ public partial class NodeEditorWindow { GUILayout.EndHorizontal(); NodeEditor nodeEditor = GetNodeEditor(node.GetType()); + nodeEditor.target = node; + + //Node is hashed before and after node GUI to detect changes + int nodeHash = 0; + var onValidate = node.GetType().GetMethod("OnValidate"); + if (onValidate != null) nodeHash = node.GetHashCode(); + nodeEditor.OnNodeGUI(); + + //If a change in hash is detected, call OnValidate method. This is done through reflection because OnValidate is only relevant in editor, and thus, the code should not be included in build. + if (onValidate != null && nodeHash != node.GetHashCode()) onValidate.Invoke(node, null); + GUILayout.EndVertical(); if (e.type == EventType.Repaint) node.position.size = GUILayoutUtility.GetLastRect().size; diff --git a/Scripts/Node.cs b/Scripts/Node.cs index 2da1cf0..e2bb06a 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -25,7 +25,7 @@ public abstract class Node { Init(); } - abstract protected void Init(); + protected abstract void Init(); public int GetInputId(NodePort input) { for (int i = 0; i < inputs.Length; i++) { @@ -63,7 +63,6 @@ public abstract class Node { } } - public void ClearConnections() { for (int i = 0; i < inputs.Length; i++) { inputs[i].ClearConnections(); @@ -72,4 +71,8 @@ public abstract class Node { outputs[i].ClearConnections(); } } + + public override int GetHashCode() { + return JsonUtility.ToJson(this).GetHashCode(); + } } diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs index 1bc47ff..4964551 100644 --- a/Scripts/NodePort.cs +++ b/Scripts/NodePort.cs @@ -19,14 +19,13 @@ public class NodePort : ISerializationCallbackReceiver{ public bool IsInput { get { return direction == IO.Input; } } public bool IsOutput { get { return direction == IO.Output; } } - public Type type { get { return _type; } } public Node node { get; private set; } public string name { get { return _name; } set { _name = value; } } public bool enabled { get { return _enabled; } set { _enabled = value; } } [NonSerialized] private List connections = new List(); - [SerializeField] private Type _type; + [SerializeField] public Type type; [SerializeField] private string _name; [SerializeField] private bool _enabled = true; [SerializeField] private IO _direction; @@ -35,7 +34,7 @@ public class NodePort : ISerializationCallbackReceiver{ public NodePort(string name, Type type, Node node, IO direction) { _name = name; - _type = type; + this.type = type; this.node = node; _direction = direction; }