NodeActionLifetimeExample.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using UnityEngine;
  2. namespace QFramework.Example.ActionKit
  3. {
  4. public class ActionLifetimeAction : ActionKitAction
  5. {
  6. protected override void OnReset()
  7. {
  8. Debug.Log("重置了,配合 RepeatNode 或者 Finish() 后再被执行的时候触发");
  9. mExecuteTime = 0;
  10. }
  11. protected override void OnBegin()
  12. {
  13. Debug.Log("开始了");
  14. }
  15. /// <summary>
  16. /// 已经执行了的次数
  17. /// </summary>
  18. private int mExecuteTime = 0;
  19. /// <summary>
  20. /// finished
  21. /// </summary>
  22. protected override void OnExecute(float dt)
  23. {
  24. Debug.Log("执行了,每帧被调用一次 ");
  25. mExecuteTime++;
  26. if (mExecuteTime >= 5)
  27. {
  28. // 自动会触发 Finish
  29. Finished = true;
  30. }
  31. }
  32. protected override void OnEnd()
  33. {
  34. Debug.Log("结束了,调用 Finish() 时触发");
  35. }
  36. protected override void OnDispose()
  37. {
  38. Debug.Log("销毁了,调用 Dispose() 时触发");
  39. }
  40. }
  41. public class NodeActionLifetimeExample : MonoBehaviour
  42. {
  43. void Start()
  44. {
  45. var lifetimeAction = new ActionLifetimeAction();
  46. this.Repeat(1)
  47. .Append(lifetimeAction)
  48. .Begin()
  49. .DisposeWhen(() => lifetimeAction.Finished);
  50. }
  51. }
  52. }