WaitNode.cs 1.4 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")]
  8. public class WaitNode : WaitableNode
  9. {
  10. public override string name => "Wait";
  11. [SerializeField, Input(name = "Seconds")]
  12. public float waitTime = 1f;
  13. private static WaitMonoBehaviour waitMonoBehaviour;
  14. protected override void Process()
  15. {
  16. // We should check where this Process() called from. But i don't know if this is an elegant and performant way to do that.
  17. // If this function is called from other than the ConditionalNode, then there will be problems, errors, unforeseen consequences, tears.
  18. // var isCalledFromConditionalProcessor = new StackTrace().GetFrame(5).GetMethod().ReflectedType == typeof(ConditionalProcessor);
  19. // if(!isCalledFromConditionalProcessor) return;
  20. if(waitMonoBehaviour == null)
  21. {
  22. var go = new GameObject(name: "WaitGameObject");
  23. waitMonoBehaviour = go.AddComponent<WaitMonoBehaviour>();
  24. }
  25. waitMonoBehaviour.Process(waitTime, ProcessFinished);
  26. }
  27. }
  28. public class WaitMonoBehaviour : MonoBehaviour
  29. {
  30. public void Process(float time, Action callback)
  31. {
  32. StartCoroutine(_Process(time, callback));
  33. }
  34. private IEnumerator _Process(float time, Action callback)
  35. {
  36. yield return new WaitForSeconds(time);
  37. callback.Invoke();
  38. }
  39. }
  40. }