MathNode.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233
  1. namespace XNode.Examples.MathNodes {
  2. [System.Serializable]
  3. public class MathNode : XNode.Node {
  4. // Adding [Input] or [Output] is all you need to do to register a field as a valid port on your node
  5. [Input] public float a;
  6. [Input] public float b;
  7. // The value of an output node field is not used for anything, but could be used for caching output results
  8. [Output] public float result;
  9. // Will be displayed as an editable field - just like the normal inspector
  10. public MathType mathType = MathType.Add;
  11. public enum MathType { Add, Subtract, Multiply, Divide }
  12. // GetValue should be overridden to return a value for any specified output port
  13. public override object GetValue(XNode.NodePort port) {
  14. // Get new a and b values from input connections. Fallback to field values if input is not connected
  15. float a = GetInputValue<float>("a", this.a);
  16. float b = GetInputValue<float>("b", this.b);
  17. // After you've gotten your input values, you can perform your calculations and return a value
  18. result = 0f;
  19. if (port.fieldName == "result")
  20. switch (mathType) {
  21. case MathType.Add: default: result = a + b; break;
  22. case MathType.Subtract: result = a - b; break;
  23. case MathType.Multiply: result = a * b; break;
  24. case MathType.Divide: result = a / b; break;
  25. }
  26. return result;
  27. }
  28. }
  29. }