DelayCall.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public class DelayCall : MonoBehaviour
  6. {
  7. #region Config
  8. public static DelayCall Instance
  9. {
  10. get
  11. {
  12. if (instance == null)
  13. {
  14. GameObject go = new GameObject("DelayCall");
  15. instance = go.AddComponent<DelayCall>();
  16. DontDestroyOnLoad(go);
  17. }
  18. return instance;
  19. }
  20. set { instance = value; }
  21. }
  22. private static DelayCall instance;
  23. #endregion
  24. public static Coroutine Call(int delayFrame, Action action)
  25. {
  26. return Instance.StartCoroutine(EDelayCall(delayFrame, action));
  27. }
  28. public static Coroutine Call(float delayTime, Action action)
  29. {
  30. return Instance.StartCoroutine(EDelayCall(delayTime, action));
  31. }
  32. private static IEnumerator EDelayCall(int delayFrame, Action action)
  33. {
  34. int count = 0;
  35. while (count++ < delayFrame)
  36. yield return null;
  37. action.SafeInvoke();
  38. }
  39. private static IEnumerator EDelayCall(float delayTime, Action action)
  40. {
  41. yield return new WaitForSeconds(delayTime);
  42. action.SafeInvoke();
  43. }
  44. }