WaitFrameNode.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections;
  3. using GraphProcessor;
  4. using UnityEngine;
  5. namespace NodeGraphProcessor.Examples
  6. {
  7. [Serializable, NodeMenuItem("Functions/Wait Frames")]
  8. public class WaitFrameNode : WaitableNode
  9. {
  10. public override string name => "Wait Frames";
  11. [SerializeField, Input(name = "Frames")]
  12. public int frame = 1;
  13. private static WaitFrameMonoBehaviour waitFrameMonoBehaviour;
  14. protected override void Process()
  15. {
  16. if(waitFrameMonoBehaviour == null)
  17. {
  18. var go = new GameObject(name: "WaitFrameGameObject");
  19. waitFrameMonoBehaviour = go.AddComponent<WaitFrameMonoBehaviour>();
  20. }
  21. waitFrameMonoBehaviour.Process(frame, ProcessFinished);
  22. }
  23. }
  24. public class WaitFrameMonoBehaviour : MonoBehaviour
  25. {
  26. public void Process(int frame, Action callback)
  27. {
  28. StartCoroutine(_Process(frame, callback));
  29. }
  30. private IEnumerator _Process(int frame, Action callback)
  31. {
  32. for(int i = 0; i < frame; i++)
  33. {
  34. yield return new WaitForEndOfFrame();
  35. i++;
  36. }
  37. callback.Invoke();
  38. }
  39. }
  40. }