Sin descripción
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.

StateUnit.cs 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. namespace Unity.VisualScripting
  2. {
  3. [TypeIcon(typeof(StateGraph))]
  4. [UnitCategory("Nesting")]
  5. public sealed class StateUnit : NesterUnit<StateGraph, StateGraphAsset>
  6. {
  7. public StateUnit() : base() { }
  8. public StateUnit(StateGraphAsset macro) : base(macro) { }
  9. /// <summary>
  10. /// The entry point to start the state graph.
  11. /// </summary>
  12. [DoNotSerialize]
  13. public ControlInput start { get; private set; }
  14. /// <summary>
  15. /// The entry point to stop the state graph.
  16. /// </summary>
  17. [DoNotSerialize]
  18. public ControlInput stop { get; private set; }
  19. /// <summary>
  20. /// The action to execute after the state graph has been started.
  21. /// </summary>
  22. [DoNotSerialize]
  23. public ControlOutput started { get; private set; }
  24. /// <summary>
  25. /// The action to execute after the state graph has been stopped.
  26. /// </summary>
  27. [DoNotSerialize]
  28. public ControlOutput stopped { get; private set; }
  29. public static StateUnit WithStart()
  30. {
  31. var stateUnit = new StateUnit();
  32. stateUnit.nest.source = GraphSource.Embed;
  33. stateUnit.nest.embed = StateGraph.WithStart();
  34. return stateUnit;
  35. }
  36. protected override void Definition()
  37. {
  38. start = ControlInput(nameof(start), Start);
  39. stop = ControlInput(nameof(stop), Stop);
  40. started = ControlOutput(nameof(started));
  41. stopped = ControlOutput(nameof(stopped));
  42. Succession(start, started);
  43. Succession(stop, stopped);
  44. }
  45. private ControlOutput Start(Flow flow)
  46. {
  47. flow.stack.EnterParentElement(this);
  48. nest.graph.Start(flow);
  49. flow.stack.ExitParentElement();
  50. return started;
  51. }
  52. private ControlOutput Stop(Flow flow)
  53. {
  54. flow.stack.EnterParentElement(this);
  55. nest.graph.Stop(flow);
  56. flow.stack.ExitParentElement();
  57. return stopped;
  58. }
  59. public override StateGraph DefaultGraph()
  60. {
  61. return StateGraph.WithStart();
  62. }
  63. }
  64. }