Unity 框架

Main.cs 3.3KB

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