mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 17:26:02 +08:00
Added DisplayValue and GetValue
This commit is contained in:
parent
cd1864fce3
commit
147424ca73
Binary file not shown.
5
Example/Nodes/DisplayValue.cs
Normal file
5
Example/Nodes/DisplayValue.cs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class DisplayValue : Node {
|
||||||
|
[Input] public float value;
|
||||||
|
}
|
||||||
12
Example/Nodes/DisplayValue.cs.meta
Normal file
12
Example/Nodes/DisplayValue.cs.meta
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 98f6f901f0da53142b79277ea3f42518
|
||||||
|
timeCreated: 1507499149
|
||||||
|
licenseType: Free
|
||||||
|
MonoImporter:
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
32
Example/Nodes/Editor/DisplayValueEditor.cs
Normal file
32
Example/Nodes/Editor/DisplayValueEditor.cs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEditor;
|
||||||
|
|
||||||
|
[CustomNodeEditor(typeof(DisplayValue), "Display Value")]
|
||||||
|
public class DisplayValueEditor : NodeEditor {
|
||||||
|
|
||||||
|
public override void OnNodeGUI(out Dictionary<NodePort, Vector2> portPositions) {
|
||||||
|
base.OnNodeGUI(out portPositions);
|
||||||
|
EditorGUILayout.LabelField("Value: " + GetResult());
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Example/Nodes/Editor/DisplayValueEditor.cs.meta
Normal file
12
Example/Nodes/Editor/DisplayValueEditor.cs.meta
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 429e0671aa9024e449642837aeadc9c2
|
||||||
|
timeCreated: 1507499229
|
||||||
|
licenseType: Free
|
||||||
|
MonoImporter:
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
public class MathNode : Node {
|
public class MathNode : Node {
|
||||||
[Input] public float a;
|
public float a;
|
||||||
[Input] public float b;
|
public float b;
|
||||||
[Output] public float result;
|
[Output] public float result;
|
||||||
public enum MathType { Add, Subtract, Multiply, Divide}
|
public enum MathType { Add, Subtract, Multiply, Divide}
|
||||||
public MathType mathType = MathType.Add;
|
public MathType mathType = MathType.Add;
|
||||||
@ -12,6 +12,20 @@ public class MathNode : Node {
|
|||||||
name = "Math";
|
name = "Math";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override object GetValue(NodePort port) {
|
||||||
|
switch(port.fieldName) {
|
||||||
|
case "result":
|
||||||
|
switch(mathType) {
|
||||||
|
case MathType.Add: return a + b;
|
||||||
|
case MathType.Subtract: return a - b;
|
||||||
|
case MathType.Multiply: return a * b;
|
||||||
|
case MathType.Divide: return a / b;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return 0f;
|
||||||
|
}
|
||||||
|
|
||||||
public override void OnCreateConnection(NodePort from, NodePort to) {
|
public override void OnCreateConnection(NodePort from, NodePort to) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -58,7 +58,6 @@ public class NodeEditor {
|
|||||||
if (NodeEditorUtilities.HasAttrib<Node.InputAttribute>(fieldAttribs) || NodeEditorUtilities.HasAttrib<Node.OutputAttribute>(fieldAttribs)) continue;
|
if (NodeEditorUtilities.HasAttrib<Node.InputAttribute>(fieldAttribs) || NodeEditorUtilities.HasAttrib<Node.OutputAttribute>(fieldAttribs)) continue;
|
||||||
DrawFieldInfoDrawerGUI(fields[i]);
|
DrawFieldInfoDrawerGUI(fields[i]);
|
||||||
}
|
}
|
||||||
EditorGUILayout.Space();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Draw node port GUI using automatic layouting. Returns port handle position. </summary>
|
/// <summary> Draw node port GUI using automatic layouting. Returns port handle position. </summary>
|
||||||
|
|||||||
@ -167,6 +167,8 @@ public partial class NodeEditorWindow {
|
|||||||
//Draw node contents
|
//Draw node contents
|
||||||
Dictionary<NodePort, Vector2> portHandlePoints;
|
Dictionary<NodePort, Vector2> portHandlePoints;
|
||||||
nodeEditor.OnNodeGUI(out portHandlePoints);
|
nodeEditor.OnNodeGUI(out portHandlePoints);
|
||||||
|
EditorGUILayout.Space();
|
||||||
|
|
||||||
if (e.type == EventType.Repaint) {
|
if (e.type == EventType.Repaint) {
|
||||||
foreach (var kvp in portHandlePoints) {
|
foreach (var kvp in portHandlePoints) {
|
||||||
Vector2 portHandlePos = kvp.Value;
|
Vector2 portHandlePos = kvp.Value;
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using System.Linq;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
/// <summary> Base class for all nodes </summary>
|
/// <summary> Base class for all nodes </summary>
|
||||||
@ -13,16 +14,18 @@ public abstract class Node : ScriptableObject {
|
|||||||
/// <summary> Input <see cref="NodePort"/>s. It is recommended not to modify these at hand. Instead, see <see cref="InputAttribute"/> </summary>
|
/// <summary> Input <see cref="NodePort"/>s. It is recommended not to modify these at hand. Instead, see <see cref="InputAttribute"/> </summary>
|
||||||
[SerializeField] public List<NodePort> inputs = new List<NodePort>();
|
[SerializeField] public List<NodePort> inputs = new List<NodePort>();
|
||||||
/// <summary> Output <see cref="NodePort"/>s. It is recommended not to modify these at hand. Instead, see <see cref="InputAttribute"/> </summary>
|
/// <summary> Output <see cref="NodePort"/>s. It is recommended not to modify these at hand. Instead, see <see cref="InputAttribute"/> </summary>
|
||||||
[SerializeField] public NodePort[] outputs = new NodePort[0];
|
[SerializeField] public List<NodePort> outputs = new List<NodePort>();
|
||||||
|
|
||||||
public int InputCount { get { return inputs.Count; } }
|
public int InputCount { get { return inputs.Count; } }
|
||||||
public int OutputCount { get { return outputs.Length; } }
|
public int OutputCount { get { return outputs.Count; } }
|
||||||
|
|
||||||
protected Node() {
|
protected Node() {
|
||||||
CachePorts(); //Cache the ports at creation time so we don't have to use reflection at runtime
|
CachePorts(); //Cache the ports at creation time so we don't have to use reflection at runtime
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void OnEnable() {
|
protected void OnEnable() {
|
||||||
|
VerifyConnections();
|
||||||
|
CachePorts();
|
||||||
Init();
|
Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,6 +52,7 @@ public abstract class Node : ScriptableObject {
|
|||||||
for (int i = 0; i < OutputCount; i++) {
|
for (int i = 0; i < OutputCount; i++) {
|
||||||
if (outputs[i].fieldName == fieldName) return outputs[i];
|
if (outputs[i].fieldName == fieldName) return outputs[i];
|
||||||
}
|
}
|
||||||
|
Debug.LogWarning("No outputs with fieldName '" + fieldName+"'");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,6 +61,12 @@ public abstract class Node : ScriptableObject {
|
|||||||
for (int i = 0; i < InputCount; i++) {
|
for (int i = 0; i < InputCount; i++) {
|
||||||
if (inputs[i].fieldName == fieldName) return inputs[i];
|
if (inputs[i].fieldName == fieldName) return inputs[i];
|
||||||
}
|
}
|
||||||
|
Debug.LogWarning("No inputs with fieldName '" + fieldName+"'");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual object GetValue(NodePort port) {
|
||||||
|
Debug.LogWarning("No GetValue(NodePort port) override defined for " + GetType());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +85,7 @@ public abstract class Node : ScriptableObject {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
public int GetOutputId(NodePort output) {
|
public int GetOutputId(NodePort output) {
|
||||||
for (int i = 0; i < outputs.Length; i++) {
|
for (int i = 0; i < outputs.Count; i++) {
|
||||||
if (output == outputs[i]) return i;
|
if (output == outputs[i]) return i;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -86,7 +96,7 @@ public abstract class Node : ScriptableObject {
|
|||||||
for (int i = 0; i < inputs.Count; i++) {
|
for (int i = 0; i < inputs.Count; i++) {
|
||||||
inputs[i].ClearConnections();
|
inputs[i].ClearConnections();
|
||||||
}
|
}
|
||||||
for (int i = 0; i < outputs.Length; i++) {
|
for (int i = 0; i < outputs.Count; i++) {
|
||||||
outputs[i].ClearConnections();
|
outputs[i].ClearConnections();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -114,6 +124,7 @@ public abstract class Node : ScriptableObject {
|
|||||||
|
|
||||||
System.Reflection.FieldInfo[] fieldInfo = GetType().GetFields();
|
System.Reflection.FieldInfo[] fieldInfo = GetType().GetFields();
|
||||||
for (int i = 0; i < fieldInfo.Length; i++) {
|
for (int i = 0; i < fieldInfo.Length; i++) {
|
||||||
|
|
||||||
//Get InputAttribute and OutputAttribute
|
//Get InputAttribute and OutputAttribute
|
||||||
object[] attribs = fieldInfo[i].GetCustomAttributes(false);
|
object[] attribs = fieldInfo[i].GetCustomAttributes(false);
|
||||||
InputAttribute inputAttrib = null;
|
InputAttribute inputAttrib = null;
|
||||||
@ -122,12 +133,29 @@ public abstract class Node : ScriptableObject {
|
|||||||
if (attribs[k] is InputAttribute) inputAttrib = attribs[k] as InputAttribute;
|
if (attribs[k] is InputAttribute) inputAttrib = attribs[k] as InputAttribute;
|
||||||
else if (attribs[k] is OutputAttribute) outputAttrib = attribs[k] as OutputAttribute;
|
else if (attribs[k] is OutputAttribute) outputAttrib = attribs[k] as OutputAttribute;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inputAttrib != null && outputAttrib != null) Debug.LogError("Field " + fieldInfo + " cannot be both input and output.");
|
if (inputAttrib != null && outputAttrib != null) Debug.LogError("Field " + fieldInfo + " cannot be both input and output.");
|
||||||
else if (inputAttrib != null) inputPorts.Add(new NodePort(fieldInfo[i], this));
|
else if (inputAttrib != null) inputPorts.Add(new NodePort(fieldInfo[i], this));
|
||||||
else if (outputAttrib != null) outputPorts.Add(new NodePort(fieldInfo[i], this));
|
else if (outputAttrib != null) outputPorts.Add(new NodePort(fieldInfo[i], this));
|
||||||
}
|
}
|
||||||
|
|
||||||
inputs = inputPorts;
|
//Remove
|
||||||
outputs = outputPorts.ToArray();
|
for (int i = inputs.Count-1; i >= 0; i--) {
|
||||||
|
//If input nodeport does not exist, remove it
|
||||||
|
if (!inputPorts.Any(x => inputs[i].fieldName == x.fieldName)) inputs.RemoveAt(i);
|
||||||
|
}
|
||||||
|
for (int i = outputs.Count - 1; i >= 0; i--) {
|
||||||
|
//If output nodeport does not exist, remove it
|
||||||
|
if (!outputPorts.Any(x => outputs[i].fieldName == x.fieldName)) outputs.RemoveAt(i);
|
||||||
|
}
|
||||||
|
//Add
|
||||||
|
for (int i = 0; i < inputPorts.Count; i++) {
|
||||||
|
//If inputports contains a new port, add it
|
||||||
|
if (!inputs.Any(x => x.fieldName == inputPorts[i].fieldName)) inputs.Add(inputPorts[i]);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < outputPorts.Count; i++) {
|
||||||
|
//If inputports contains a new port, add it
|
||||||
|
if (!outputs.Any(x => x.fieldName == outputPorts[i].fieldName)) outputs.Add(outputPorts[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -55,6 +55,10 @@ public class NodePort {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public object GetValue() {
|
||||||
|
return node.GetValue(this);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary> Connect this <see cref="NodePort"/> to another </summary>
|
/// <summary> Connect this <see cref="NodePort"/> to another </summary>
|
||||||
/// <param name="port">The <see cref="NodePort"/> to connect to</param>
|
/// <param name="port">The <see cref="NodePort"/> to connect to</param>
|
||||||
public void Connect(NodePort port) {
|
public void Connect(NodePort port) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user