using System; using UnityEngine; namespace AlicizaX.Runtime { /// /// 基础组件。 /// [DisallowMultipleComponent] [AddComponentMenu("Game Framework/Base")] [UnityEngine.Scripting.Preserve] public sealed class BaseComponent : GameFrameworkComponent { private const int DefaultDpi = 96; private float m_GameSpeedBeforePause = 1f; [SerializeField] private int m_FrameRate = 30; [SerializeField] private float m_GameSpeed = 1f; [SerializeField] private bool m_RunInBackground = true; [SerializeField] private bool m_NeverSleep = true; /// /// 获取或设置游戏帧率。 /// public int FrameRate { get { return m_FrameRate; } set { Application.targetFrameRate = m_FrameRate = value; } } /// /// 获取或设置游戏速度。 /// public float GameSpeed { get { return m_GameSpeed; } set { Time.timeScale = m_GameSpeed = value >= 0f ? value : 0f; } } /// /// 获取游戏是否暂停。 /// public bool IsGamePaused { get { return m_GameSpeed <= 0f; } } /// /// 获取是否正常游戏速度。 /// public bool IsNormalGameSpeed { get { return m_GameSpeed == 1f; } } /// /// 获取或设置是否允许后台运行。 /// public bool RunInBackground { get { return m_RunInBackground; } set { Application.runInBackground = m_RunInBackground = value; } } /// /// 获取或设置是否禁止休眠。 /// public bool NeverSleep { get { return m_NeverSleep; } set { m_NeverSleep = value; Screen.sleepTimeout = value ? SleepTimeout.NeverSleep : SleepTimeout.SystemSetting; } } /// /// 游戏框架组件初始化。 /// protected override void Awake() { IsAutoRegister = false; base.Awake(); GameModuleMonoProxy _monoProxyProxy = new GameObject("GameModuleMonoProxy").AddComponent(); _monoProxyProxy.gameObject.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector; DontDestroyOnLoad(this); Utility.Unity.MakeEntity(transform); Log.Info("Game Version: {0}, Unity Version: {1}", AppVersion.GameVersion, Application.unityVersion); Utility.Converter.ScreenDpi = Screen.dpi; if (Utility.Converter.ScreenDpi <= 0) { Utility.Converter.ScreenDpi = DefaultDpi; } Application.targetFrameRate = m_FrameRate; Time.timeScale = m_GameSpeed; Application.runInBackground = m_RunInBackground; Screen.sleepTimeout = m_NeverSleep ? SleepTimeout.NeverSleep : SleepTimeout.SystemSetting; } private void OnApplicationQuit() { StopAllCoroutines(); Shutdown(); } /// /// 暂停游戏。 /// public void PauseGame() { if (IsGamePaused) { return; } m_GameSpeedBeforePause = GameSpeed; GameSpeed = 0f; } /// /// 恢复游戏。 /// public void ResumeGame() { if (!IsGamePaused) { return; } GameSpeed = m_GameSpeedBeforePause; } /// /// 重置为正常游戏速度。 /// public void ResetNormalGameSpeed() { if (IsNormalGameSpeed) { return; } GameSpeed = 1f; } internal void Shutdown() { Destroy(gameObject); Utility.Unity.Shutdown(); ReferencePool.ClearAll(); Utility.Marshal.FreeCachedHGlobal(); } } }