DelayCall.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. public static void stopCoroutine(Coroutine coroutine)
  33. {
  34. Instance.StopCoroutine(coroutine);
  35. }
  36. private static IEnumerator EDelayCall(int delayFrame, Action action)
  37. {
  38. int count = 0;
  39. while (count++ < delayFrame)
  40. yield return null;
  41. action.SafeInvoke();
  42. }
  43. private static IEnumerator EDelayCall(float delayTime, Action action)
  44. {
  45. yield return new WaitForSeconds(delayTime);
  46. action.SafeInvoke();
  47. }
  48. }