using System; using System.Collections.Generic; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif namespace TEngine { /// /// 游戏模块。 /// public partial class GameModule : MonoBehaviour { private static readonly Dictionary _moduleMaps = new Dictionary(ModuleImpSystem.DesignModuleCount); private static GameObject _gameModuleRoot; #region 框架模块 /// /// 获取游戏基础模块。 /// public static RootModule Base { get => _base ??= Get(); private set => _base = value; } private static RootModule _base; /// /// 获取调试模块。 /// public static DebuggerModule Debugger { get => _debugger ??= Get(); private set => _debugger = value; } private static DebuggerModule _debugger; /// /// 获取有限状态机模块。 /// public static FsmModule Fsm => _fsm ??= Get(); private static FsmModule _fsm; /// /// 流程管理模块。 /// public static ProcedureModule Procedure => _procedure ??= Get(); private static ProcedureModule _procedure; /// /// 获取对象池模块。 /// public static ObjectPoolModule ObjectPool => _objectPool ??= Get(); private static ObjectPoolModule _objectPool; /// /// 获取资源模块。 /// public static ResourceModule Resource => _resource ??= Get(); private static ResourceModule _resource; /// /// 获取音频模块。 /// public static AudioModule Audio => _audio ??= Get(); private static AudioModule _audio; /// /// 获取配置模块。 /// public static SettingModule Setting => _setting ??= Get(); private static SettingModule _setting; /// /// 获取UI模块。 /// public static UIModule UI => _ui ??= Get(); private static UIModule _ui; /// /// 获取多语言模块。 /// public static LocalizationModule Localization => _localization ??= Get(); private static LocalizationModule _localization; /// /// 获取场景模块。 /// public static SceneModule Scene => _scene ??= Get(); private static SceneModule _scene; /// /// 获取计时器模块。 /// public static TimerModule Timer => _timer ??= Get(); private static TimerModule _timer; /// /// 资源组件拓展。 /// public static ResourceExtComponent ResourceExt => _resourceExt ??= Get(); private static ResourceExtComponent _resourceExt; #endregion /// /// 获取游戏框架模块类。 /// /// 游戏框架模块类。 /// 游戏框架模块实例。 public static T Get() where T : Module { Type type = typeof(T); if (_moduleMaps.TryGetValue(type, out var ret)) { return ret as T; } T module = ModuleSystem.GetModule(); Log.Assert(condition: module != null, $"{typeof(T)} is null"); _moduleMaps.Add(type, module); return module; } private void Start() { Log.Info("GameModule Active"); gameObject.name = $"[{nameof(GameModule)}]"; _gameModuleRoot = gameObject; DontDestroyOnLoad(gameObject); } public static void Shutdown(ShutdownType shutdownType) { Log.Info("GameModule Shutdown"); if (_gameModuleRoot != null) { Destroy(_gameModuleRoot); _gameModuleRoot = null; } _moduleMaps.Clear(); _base = null; _debugger = null; _fsm = null; _procedure = null; _objectPool = null; _resource = null; _audio = null; _setting = null; _ui = null; _localization = null; _scene = null; _timer = null; _resourceExt = null; } #region HandlePlayModeStateChanged private void OnEnable() { #if UNITY_EDITOR EditorApplication.playModeStateChanged += HandlePlayModeStateChanged; #endif } private void OnDisable() { #if UNITY_EDITOR EditorApplication.playModeStateChanged -= HandlePlayModeStateChanged; #endif } #if UNITY_EDITOR void HandlePlayModeStateChanged(PlayModeStateChange state) { if (state == PlayModeStateChange.ExitingPlayMode) { ModuleImpSystem.Shutdown(); ModuleSystem.Shutdown(ShutdownType.Quit); } } #endif #endregion } }