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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Collections;
  2. namespace Unity.VisualScripting
  3. {
  4. /// <summary>
  5. /// Loops as long as a given condition is true.
  6. /// </summary>
  7. [UnitTitle("While Loop")]
  8. [UnitCategory("Control")]
  9. [UnitOrder(11)]
  10. public class While : LoopUnit
  11. {
  12. /// <summary>
  13. /// The condition to check at each iteration to determine whether the loop should continue.
  14. /// </summary>
  15. [DoNotSerialize]
  16. [PortLabelHidden]
  17. public ValueInput condition { get; private set; }
  18. protected override void Definition()
  19. {
  20. base.Definition();
  21. condition = ValueInput<bool>(nameof(condition));
  22. Requirement(condition, enter);
  23. }
  24. private int Start(Flow flow)
  25. {
  26. return flow.EnterLoop();
  27. }
  28. private bool CanMoveNext(Flow flow)
  29. {
  30. return flow.GetValue<bool>(condition);
  31. }
  32. protected override ControlOutput Loop(Flow flow)
  33. {
  34. var loop = Start(flow);
  35. var stack = flow.PreserveStack();
  36. while (flow.LoopIsNotBroken(loop) && CanMoveNext(flow))
  37. {
  38. flow.Invoke(body);
  39. flow.RestoreStack(stack);
  40. }
  41. flow.DisposePreservedStack(stack);
  42. flow.ExitLoop(loop);
  43. return exit;
  44. }
  45. protected override IEnumerator LoopCoroutine(Flow flow)
  46. {
  47. var loop = Start(flow);
  48. var stack = flow.PreserveStack();
  49. while (flow.LoopIsNotBroken(loop) && CanMoveNext(flow))
  50. {
  51. yield return body;
  52. flow.RestoreStack(stack);
  53. }
  54. flow.DisposePreservedStack(stack);
  55. flow.ExitLoop(loop);
  56. yield return exit;
  57. }
  58. }
  59. }