TimerManager.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>时间控制器</summary>
  4. public class TimerManager : MonoBehaviour
  5. {
  6. #region 单利
  7. /// <summary>单利静态引用</summary>
  8. private static TimerManager _instance;
  9. /// <summary>单利 返回 常驻内存的 PrefabManger 对象,只会实例化一次</summary>
  10. public static TimerManager Instance
  11. {
  12. get
  13. {
  14. if (_instance == null)
  15. {
  16. _instance = GameObject.FindObjectOfType(typeof(TimerManager)) as TimerManager;
  17. if (_instance == null)
  18. {
  19. GameObject go = new GameObject("TimerManager");
  20. go.transform.localPosition = Vector3.zero;
  21. go.transform.localScale = Vector3.one;
  22. DontDestroyOnLoad(go);
  23. _instance = go.AddComponent<TimerManager>();
  24. }
  25. }
  26. return _instance;
  27. }
  28. }
  29. #endregion
  30. public delegate void DelegateWaitCallBack(object paramObj);
  31. /// <summary>延时执行某个函数</summary>
  32. public void WaitCallBack(int waitTime, DelegateWaitCallBack waitCallBack)
  33. {
  34. this.StartCoroutine(StartWait(waitTime, waitCallBack, null));
  35. }
  36. public void WaitCallBack(int waitTime, DelegateWaitCallBack waitCallBack, object paramObj)
  37. {
  38. this.StartCoroutine(StartWait(waitTime, waitCallBack, paramObj));
  39. }
  40. /// <summary>延迟函数</summary>
  41. IEnumerator StartWait(int waitTiame, DelegateWaitCallBack waitCallBack, object paramObj)
  42. {
  43. yield return new WaitForSeconds((float)waitTiame / 1000f);
  44. waitCallBack(paramObj);
  45. waitCallBack = null;
  46. }
  47. }