TimeRecoveryHelper.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5. public class TimeRecoveryHelper : MonoSingleton<TimeRecoveryHelper>
  6. {
  7. /// <summary>
  8. ///
  9. /// </summary>
  10. public Dictionary<string, GameObject> allTimerDic = new Dictionary<string, GameObject>();
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. /// <param name="functionName"></param>
  15. /// <param name="seconds"></param>
  16. public void StartTimer(string functionName, int seconds, Action<int> leftTs)
  17. {
  18. if (allTimerDic.ContainsKey(functionName))
  19. {
  20. GameObject xx = allTimerDic[functionName];
  21. TimeRecovery timer = xx.GetComponent<TimeRecovery>();
  22. timer.StartTimer(seconds, leftTs);
  23. }
  24. else
  25. {
  26. GameObject xx = new GameObject();
  27. xx.transform.SetParent(this.gameObject.transform);
  28. TimeRecovery timer = xx.GetComponent<TimeRecovery>();
  29. if (timer == null)
  30. {
  31. AssemblyHelper.Instance.BindScript("TimeRecovery", xx);
  32. timer = xx.GetComponent<TimeRecovery>();
  33. }
  34. timer.StartTimer(seconds, leftTs);
  35. allTimerDic.Add(functionName, xx);
  36. }
  37. }
  38. public void StopTimer(string functionName)
  39. {
  40. if (allTimerDic.ContainsKey(functionName))
  41. {
  42. GameObject xx = allTimerDic[functionName];
  43. TimeRecovery timer = xx.GetComponent<TimeRecovery>();
  44. timer.StopTimer();
  45. Destroy(xx);
  46. allTimerDic.Remove(functionName);
  47. }
  48. }
  49. }