mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 17:26:02 +08:00
35 lines
1.5 KiB
C#
35 lines
1.5 KiB
C#
using XNode;
|
|
|
|
namespace BasicNodes {
|
|
[System.Serializable]
|
|
public class MathNode : Node {
|
|
// Adding [Input] or [Output] is all you need to do to register a field as a valid port on your node
|
|
[Input] public float a;
|
|
[Input] public float b;
|
|
// The value of an output node field is not used for anything, but could be used for caching output results
|
|
[Output] public float result;
|
|
|
|
// UNEC will display this as an editable field - just like the normal inspector would
|
|
public MathType mathType = MathType.Add;
|
|
public enum MathType { Add, Subtract, Multiply, Divide }
|
|
|
|
// GetValue should be overridden to return a value for any specified output port
|
|
public override object GetValue(NodePort port) {
|
|
|
|
// Get new a and b values from input connections. Fallback to field values if input is not connected
|
|
float a = GetInputValue<float>("a", this.a);
|
|
float b = GetInputValue<float>("b", this.b);
|
|
|
|
// After you've gotten your input values, you can perform your calculations and return a value
|
|
result = 0f;
|
|
if (port.fieldName == "result")
|
|
switch (mathType) {
|
|
case MathType.Add: default: result = a+b; break;
|
|
case MathType.Subtract: result = a - b; break;
|
|
case MathType.Multiply: result = a * b; break;
|
|
case MathType.Divide: result = a / b; break;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
} |