mirror of
https://github.com/Siccity/xNode.git
synced 2026-02-06 15:24:55 +08:00
Improved broken connection cleanup. Example updates
This commit is contained in:
parent
899e5ae979
commit
bc5c060dee
BIN
Example/ExampleNodeGraph.asset
Normal file
BIN
Example/ExampleNodeGraph.asset
Normal file
Binary file not shown.
9
Example/ExampleNodeGraph.asset.meta
Normal file
9
Example/ExampleNodeGraph.asset.meta
Normal file
@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e8c47bc953732464a9bf3a76273d99ef
|
||||
timeCreated: 1507916591
|
||||
licenseType: Free
|
||||
NativeFormatImporter:
|
||||
mainObjectFileID: 11400000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -1,15 +0,0 @@
|
||||
using UnityEngine;
|
||||
|
||||
[System.Serializable]
|
||||
public class BaseNode : Node {
|
||||
|
||||
[Input] public string input;
|
||||
[Output] public string output;
|
||||
public bool concat;
|
||||
[TextArea]
|
||||
public string SomeString;
|
||||
[Header("New stuff")]
|
||||
public Color col;
|
||||
public AnimationCurve anim;
|
||||
public Vector3 vec;
|
||||
}
|
||||
13
Example/Nodes/ConstantValue.cs
Normal file
13
Example/Nodes/ConstantValue.cs
Normal file
@ -0,0 +1,13 @@
|
||||
[System.Serializable]
|
||||
public class ConstantValue : ExampleNodeBase {
|
||||
public float a;
|
||||
[Output] public float value;
|
||||
|
||||
protected override void Init() {
|
||||
name = "Constant Value";
|
||||
}
|
||||
|
||||
public override object GetValue(NodePort port) {
|
||||
return a;
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 23665941e8cd89a48b1272ac5fd6510c
|
||||
timeCreated: 1505462705
|
||||
guid: 707240ce8955a0240a7c0c4177d83bf5
|
||||
timeCreated: 1505937586
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
@ -1,5 +1,6 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class DisplayValue : Node {
|
||||
public class DisplayValue : ExampleNodeBase {
|
||||
[Input] public float value;
|
||||
|
||||
}
|
||||
|
||||
@ -12,20 +12,7 @@ public class DisplayValueEditor : NodeEditor {
|
||||
}
|
||||
|
||||
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;
|
||||
ExampleNodeBase t = target as ExampleNodeBase;
|
||||
return t.GetInputFloat("value");
|
||||
}
|
||||
}
|
||||
|
||||
22
Example/Nodes/ExampleNodeBase.cs
Normal file
22
Example/Nodes/ExampleNodeBase.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public 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.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/ExampleNodeBase.cs.meta
Normal file
12
Example/Nodes/ExampleNodeBase.cs.meta
Normal file
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 923bada49e668fd4a98b04fcb49999d7
|
||||
timeCreated: 1507917099
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -1,9 +1,9 @@
|
||||
using UnityEngine;
|
||||
|
||||
[System.Serializable]
|
||||
public class MathNode : Node {
|
||||
public float a;
|
||||
public float b;
|
||||
public class MathNode : ExampleNodeBase {
|
||||
[Input] public float c;
|
||||
[Input] public float b;
|
||||
[Output] public float result;
|
||||
public enum MathType { Add, Subtract, Multiply, Divide}
|
||||
public MathType mathType = MathType.Add;
|
||||
@ -13,6 +13,9 @@ public class MathNode : Node {
|
||||
}
|
||||
|
||||
public override object GetValue(NodePort port) {
|
||||
float a = GetInputFloat("c");
|
||||
float b = GetInputFloat("b");
|
||||
|
||||
switch(port.fieldName) {
|
||||
case "result":
|
||||
switch(mathType) {
|
||||
@ -25,8 +28,4 @@ public class MathNode : Node {
|
||||
}
|
||||
return 0f;
|
||||
}
|
||||
|
||||
public override void OnCreateConnection(NodePort from, NodePort to) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -128,7 +128,9 @@ public partial class NodeEditorWindow {
|
||||
if (!portConnectionPoints.ContainsKey(output)) continue;
|
||||
Vector2 from = _portConnectionPoints[output].center;
|
||||
for (int k = 0; k < output.ConnectionCount; k++) {
|
||||
|
||||
NodePort input = output.GetConnection(k);
|
||||
if (input == null) return; //If a script has been updated and the port doesn't exist, it is removed and null is returned. If this happens, return.
|
||||
Vector2 to = _portConnectionPoints[input].center;
|
||||
DrawConnection(from, to, NodeEditorUtilities.GetTypeColor(output.type));
|
||||
}
|
||||
|
||||
@ -77,7 +77,16 @@ public class NodePort {
|
||||
}
|
||||
|
||||
public NodePort GetConnection(int i) {
|
||||
//If the connection is broken for some reason, remove it.
|
||||
if (connections[i].node == null || string.IsNullOrEmpty(connections[i].fieldName)) {
|
||||
connections.RemoveAt(i);
|
||||
return null;
|
||||
}
|
||||
NodePort port = connections[i].node.GetPortByFieldName(connections[i].fieldName);
|
||||
if (port == null) {
|
||||
connections.RemoveAt(i);
|
||||
return null;
|
||||
}
|
||||
return port;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user