mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-21 01:36:03 +08:00
OnValidate added for nodes
This commit is contained in:
parent
638e784ca9
commit
d88cf5b53d
@ -1,11 +1,7 @@
|
|||||||
using System.Collections;
|
[CustomNodeEditor(typeof(MathNode), "Math")]
|
||||||
using System.Collections.Generic;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
[CustomNodeEditor(typeof(MathNode), "Math")]
|
|
||||||
public class AddNodeEditor : NodeEditor {
|
public class AddNodeEditor : NodeEditor {
|
||||||
|
|
||||||
public override void OnNodeGUI() {
|
public override void OnNodeGUI() {
|
||||||
GUILayout.Label("YEAH CUSTOM");
|
base.OnNodeGUI();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,8 +3,9 @@
|
|||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
public class MathNode : Node {
|
public class MathNode : Node {
|
||||||
|
|
||||||
public int someValue;
|
public enum ValueType { Float, Int }
|
||||||
public enum MathType { Add, Subtract, Multiply, Divide}
|
public enum MathType { Add, Subtract, Multiply, Divide}
|
||||||
|
public ValueType valueType = ValueType.Float;
|
||||||
public MathType mathType = MathType.Add;
|
public MathType mathType = MathType.Add;
|
||||||
|
|
||||||
protected override void Init() {
|
protected override void Init() {
|
||||||
@ -14,4 +15,12 @@ public class MathNode : Node {
|
|||||||
outputs = new NodePort[1];
|
outputs = new NodePort[1];
|
||||||
outputs[0] = CreateNodeOutput("Result", typeof(float));
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -176,8 +176,19 @@ public partial class NodeEditorWindow {
|
|||||||
GUILayout.EndHorizontal();
|
GUILayout.EndHorizontal();
|
||||||
|
|
||||||
NodeEditor nodeEditor = GetNodeEditor(node.GetType());
|
NodeEditor nodeEditor = GetNodeEditor(node.GetType());
|
||||||
|
|
||||||
nodeEditor.target = node;
|
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();
|
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();
|
GUILayout.EndVertical();
|
||||||
|
|
||||||
if (e.type == EventType.Repaint) node.position.size = GUILayoutUtility.GetLastRect().size;
|
if (e.type == EventType.Repaint) node.position.size = GUILayoutUtility.GetLastRect().size;
|
||||||
|
|||||||
@ -25,7 +25,7 @@ public abstract class Node {
|
|||||||
Init();
|
Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract protected void Init();
|
protected abstract void Init();
|
||||||
|
|
||||||
public int GetInputId(NodePort input) {
|
public int GetInputId(NodePort input) {
|
||||||
for (int i = 0; i < inputs.Length; i++) {
|
for (int i = 0; i < inputs.Length; i++) {
|
||||||
@ -63,7 +63,6 @@ public abstract class Node {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void ClearConnections() {
|
public void ClearConnections() {
|
||||||
for (int i = 0; i < inputs.Length; i++) {
|
for (int i = 0; i < inputs.Length; i++) {
|
||||||
inputs[i].ClearConnections();
|
inputs[i].ClearConnections();
|
||||||
@ -72,4 +71,8 @@ public abstract class Node {
|
|||||||
outputs[i].ClearConnections();
|
outputs[i].ClearConnections();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode() {
|
||||||
|
return JsonUtility.ToJson(this).GetHashCode();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,14 +19,13 @@ public class NodePort : ISerializationCallbackReceiver{
|
|||||||
public bool IsInput { get { return direction == IO.Input; } }
|
public bool IsInput { get { return direction == IO.Input; } }
|
||||||
public bool IsOutput { get { return direction == IO.Output; } }
|
public bool IsOutput { get { return direction == IO.Output; } }
|
||||||
|
|
||||||
public Type type { get { return _type; } }
|
|
||||||
public Node node { get; private set; }
|
public Node node { get; private set; }
|
||||||
public string name { get { return _name; } set { _name = value; } }
|
public string name { get { return _name; } set { _name = value; } }
|
||||||
public bool enabled { get { return _enabled; } set { _enabled = value; } }
|
public bool enabled { get { return _enabled; } set { _enabled = value; } }
|
||||||
|
|
||||||
[NonSerialized] private List<NodePort> connections = new List<NodePort>();
|
[NonSerialized] private List<NodePort> connections = new List<NodePort>();
|
||||||
|
|
||||||
[SerializeField] private Type _type;
|
[SerializeField] public Type type;
|
||||||
[SerializeField] private string _name;
|
[SerializeField] private string _name;
|
||||||
[SerializeField] private bool _enabled = true;
|
[SerializeField] private bool _enabled = true;
|
||||||
[SerializeField] private IO _direction;
|
[SerializeField] private IO _direction;
|
||||||
@ -35,7 +34,7 @@ public class NodePort : ISerializationCallbackReceiver{
|
|||||||
|
|
||||||
public NodePort(string name, Type type, Node node, IO direction) {
|
public NodePort(string name, Type type, Node node, IO direction) {
|
||||||
_name = name;
|
_name = name;
|
||||||
_type = type;
|
this.type = type;
|
||||||
this.node = node;
|
this.node = node;
|
||||||
_direction = direction;
|
_direction = direction;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user