GlobalLogicManager.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. public class GlobalLogicManager
  6. {
  7. //全局逻辑
  8. static Dictionary<string, IApplicationGlobalLogic> s_GlobalStatus = new Dictionary<string, IApplicationGlobalLogic>();
  9. public static void Init()
  10. {
  11. ApplicationManager.s_OnApplicationUpdate += OnUpdate;
  12. }
  13. public static void InitLogic(string logicName)
  14. {
  15. if (s_GlobalStatus.ContainsKey(logicName))
  16. {
  17. throw new Exception(logicName + " is Inited!");
  18. }
  19. else
  20. {
  21. IApplicationGlobalLogic l_statusTmp = (IApplicationGlobalLogic)Activator.CreateInstance(Type.GetType(logicName));
  22. s_GlobalStatus.Add(logicName, l_statusTmp);
  23. s_logicList = new List<IApplicationGlobalLogic>(s_GlobalStatus.Values);
  24. try
  25. {
  26. l_statusTmp.Init();
  27. }
  28. catch(Exception e)
  29. {
  30. Debug.LogError(e.ToString());
  31. }
  32. }
  33. }
  34. public static T GetLogic<T>() where T :IApplicationGlobalLogic
  35. {
  36. string logicName = typeof(T).Name;
  37. if (s_GlobalStatus.ContainsKey(logicName))
  38. {
  39. IApplicationGlobalLogic logicTmp = s_GlobalStatus[logicName];
  40. return (T)logicTmp;
  41. }
  42. else
  43. {
  44. throw new Exception("not find " + logicName);
  45. }
  46. }
  47. static List<IApplicationGlobalLogic> s_logicList = new List<IApplicationGlobalLogic>();
  48. static void OnUpdate ()
  49. {
  50. for (int i = 0; i < s_logicList.Count; i++)
  51. {
  52. try
  53. {
  54. s_logicList[i].Update();
  55. }
  56. catch(Exception e)
  57. {
  58. Debug.LogError(e.ToString());
  59. }
  60. }
  61. }
  62. }