Unity 框架

Main.cs 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. }
  29. // Update is called once per frame
  30. void Update()
  31. {
  32. onUpdate?.Invoke();
  33. }
  34. /// <summary>
  35. /// 获取指定类型管理器
  36. /// </summary>
  37. /// <typeparam name="T">管理器类型</typeparam>
  38. /// <returns>管理器</returns>
  39. public static T GetMagr<T>()where T:MonoBehaviour
  40. {
  41. T magr = Instance?.m_ManagerList.Find(p => p.GetType() == typeof(T)) as T;
  42. if (magr == null)
  43. Log.Error($"管理器{typeof(T).Name}未注册");
  44. return magr;
  45. }
  46. /// <summary>
  47. /// 延时执行
  48. /// </summary>
  49. /// <param name="time">延时时间</param>
  50. /// <param name="action">执行事件</param>
  51. public Coroutine Delay(float time, TAction action) => StartCoroutine(DelayAction(time, action));
  52. /// <summary>
  53. /// 延时执行
  54. /// </summary>
  55. /// <param name="time"></param>
  56. /// <param name="action"></param>
  57. /// <returns></returns>
  58. private IEnumerator DelayAction(float time, TAction action)
  59. {
  60. yield return new WaitForSeconds(time);
  61. action?.Invoke();
  62. }
  63. }
  64. public delegate void TAction();
  65. public delegate void TAction<in T>(T arg0);
  66. public delegate void TAction<in T1, in T2>(T1 arg0, T2 arg1);
  67. public delegate void TAction<in T1, in T2, in T3>(T1 arg0, T2 arg1, T3 arg2);
  68. public delegate TRes TFunc<out TRes>();
  69. public delegate TRes TFunc<in T1, out TRes>(T1 arg0);
  70. public delegate TRes TFunc<in T1, in T2, out TRes>(T1 arg0, T2 arg1);
  71. public enum ResLoadMode
  72. {
  73. Resourece,
  74. AssetBundle
  75. }
  76. public enum UpdateMode
  77. {
  78. Editor,
  79. Loacl,
  80. Bulid
  81. }
  82. }