暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Once.cs 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. namespace Unity.VisualScripting
  2. {
  3. /// <summary>
  4. /// Executes an action only once, and a different action afterwards.
  5. /// </summary>
  6. [UnitCategory("Control")]
  7. [UnitOrder(14)]
  8. public sealed class Once : Unit, IGraphElementWithData
  9. {
  10. public sealed class Data : IGraphElementData
  11. {
  12. public bool executed;
  13. }
  14. /// <summary>
  15. /// The entry point for the action.
  16. /// </summary>
  17. [DoNotSerialize]
  18. [PortLabelHidden]
  19. public ControlInput enter { get; private set; }
  20. /// <summary>
  21. /// Trigger to reset the once check.
  22. /// </summary>
  23. [DoNotSerialize]
  24. public ControlInput reset { get; private set; }
  25. /// <summary>
  26. /// The action to execute the first time the node is entered.
  27. /// </summary>
  28. [DoNotSerialize]
  29. public ControlOutput once { get; private set; }
  30. /// <summary>
  31. /// The action to execute subsequently.
  32. /// </summary>
  33. [DoNotSerialize]
  34. public ControlOutput after { get; private set; }
  35. protected override void Definition()
  36. {
  37. enter = ControlInput(nameof(enter), Enter);
  38. reset = ControlInput(nameof(reset), Reset);
  39. once = ControlOutput(nameof(once));
  40. after = ControlOutput(nameof(after));
  41. Succession(enter, once);
  42. Succession(enter, after);
  43. }
  44. public IGraphElementData CreateData()
  45. {
  46. return new Data();
  47. }
  48. public ControlOutput Enter(Flow flow)
  49. {
  50. var data = flow.stack.GetElementData<Data>(this);
  51. if (!data.executed)
  52. {
  53. data.executed = true;
  54. return once;
  55. }
  56. else
  57. {
  58. return after;
  59. }
  60. }
  61. public ControlOutput Reset(Flow flow)
  62. {
  63. flow.stack.GetElementData<Data>(this).executed = false;
  64. return null;
  65. }
  66. }
  67. }