暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SelectUnit.cs 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. namespace Unity.VisualScripting
  2. {
  3. /// <summary>
  4. /// Selects a value from a set by checking if a condition is true or false.
  5. /// </summary>
  6. [UnitCategory("Control")]
  7. [UnitTitle("Select")]
  8. [TypeIcon(typeof(ISelectUnit))]
  9. [UnitOrder(6)]
  10. public sealed class SelectUnit : Unit, ISelectUnit
  11. {
  12. /// <summary>
  13. /// The condition to check.
  14. /// </summary>
  15. [DoNotSerialize]
  16. [PortLabelHidden]
  17. public ValueInput condition { get; private set; }
  18. /// <summary>
  19. /// The value to return if the condition is true.
  20. /// </summary>
  21. [DoNotSerialize]
  22. [PortLabel("True")]
  23. public ValueInput ifTrue { get; private set; }
  24. /// <summary>
  25. /// The value to return if the condition is false.
  26. /// </summary>
  27. [DoNotSerialize]
  28. [PortLabel("False")]
  29. public ValueInput ifFalse { get; private set; }
  30. /// <summary>
  31. /// The returned value.
  32. /// </summary>
  33. [DoNotSerialize]
  34. [PortLabelHidden]
  35. public ValueOutput selection { get; private set; }
  36. protected override void Definition()
  37. {
  38. condition = ValueInput<bool>(nameof(condition));
  39. ifTrue = ValueInput<object>(nameof(ifTrue)).AllowsNull();
  40. ifFalse = ValueInput<object>(nameof(ifFalse)).AllowsNull();
  41. selection = ValueOutput(nameof(selection), Branch).Predictable();
  42. Requirement(condition, selection);
  43. Requirement(ifTrue, selection);
  44. Requirement(ifFalse, selection);
  45. }
  46. public object Branch(Flow flow)
  47. {
  48. return flow.GetValue(flow.GetValue<bool>(condition) ? ifTrue : ifFalse);
  49. }
  50. }
  51. }