Unity 框架

Main.cs 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using TMPro;
  5. using UnityEngine;
  6. [assembly: InternalsVisibleTo("TFramework.Editor")]
  7. namespace TFramework
  8. {
  9. [DefaultExecutionOrder(-10000)]
  10. [AddComponentMenu("TFramework/全局管理(Main)")]
  11. [DisallowMultipleComponent]
  12. public class Main : SingletonBehaviourBase<Main>
  13. {
  14. [SerializeField]
  15. internal List<BaseManager> m_ManagerList = new List<BaseManager>();
  16. public bool IsEnableDebug;
  17. /// <summary>
  18. /// 供非Mono脚本使用Update事件
  19. /// </summary>
  20. public TAction onUpdate;
  21. protected override void Awake()
  22. {
  23. base.Awake();
  24. }
  25. // Start is called before the first frame update
  26. void Start()
  27. {
  28. if (IsEnableDebug)
  29. GetMagr<UIManager>().OpenUI<DebugPanel>();
  30. }
  31. // Update is called once per frame
  32. void Update()
  33. {
  34. onUpdate?.Invoke();
  35. }
  36. private void OnValidate()
  37. {
  38. if(IsEnableDebug)
  39. {
  40. if(m_ManagerList.Count <= 0||m_ManagerList.Find(m=>m is UIManager)==null)
  41. {
  42. Log.Error("启用Debug工具失败,Debug工具依赖UIManager,请先注册UIManager");
  43. IsEnableDebug = false;
  44. }
  45. }
  46. }
  47. protected override void OnDestroy()
  48. {
  49. if (IsEnableDebug)
  50. GetMagr<UIManager>().CloseUI<DebugPanel>(true);
  51. base.OnDestroy();
  52. }
  53. /// <summary>
  54. /// 获取指定类型管理器
  55. /// </summary>
  56. /// <typeparam name="T">管理器类型</typeparam>
  57. /// <returns>管理器</returns>
  58. public static T GetMagr<T>()where T:MonoBehaviour
  59. {
  60. T magr = Instance?.m_ManagerList.Find(p => p.GetType() == typeof(T)) as T;
  61. if (magr == null)
  62. Log.Error($"管理器{typeof(T).Name}未注册");
  63. return magr;
  64. }
  65. /// <summary>
  66. /// 延时执行
  67. /// </summary>
  68. /// <param name="time">延时时间</param>
  69. /// <param name="action">执行事件</param>
  70. public Coroutine Delay(float time, TAction action) => StartCoroutine(DelayAction(time, action));
  71. /// <summary>
  72. /// 延时执行
  73. /// </summary>
  74. /// <param name="time"></param>
  75. /// <param name="action"></param>
  76. /// <returns></returns>
  77. private IEnumerator DelayAction(float time, TAction action)
  78. {
  79. yield return new WaitForSeconds(time);
  80. action?.Invoke();
  81. }
  82. }
  83. public delegate void TAction();
  84. public delegate void TAction<in T>(T arg0);
  85. public delegate void TAction<in T1, in T2>(T1 arg0, T2 arg1);
  86. public delegate void TAction<in T1, in T2, in T3>(T1 arg0, T2 arg1, T3 arg2);
  87. public delegate TRes TFunc<out TRes>();
  88. public delegate TRes TFunc<in T1, out TRes>(T1 arg0);
  89. public delegate TRes TFunc<in T1, in T2, out TRes>(T1 arg0, T2 arg1);
  90. public enum ResLoadMode
  91. {
  92. Resourece,
  93. AssetBundle
  94. }
  95. public enum UpdateMode
  96. {
  97. Editor,
  98. Loacl,
  99. Bulid
  100. }
  101. }