using PhxhSDK; using PhxhSDK.AOT; using PhxhSDK.AOT.Aspect; using PhxhSDK.Reference; using System; using System.Reflection; using Cysharp.Threading.Tasks; using PhxhSDK.AOT.Net; using UnityEngine; namespace Gameplay { public class Main : SingletonMono
{ private const string GAME_TYPE_NAME = "MyGame, GamePlay"; private BaseGame _game; private float _lastSecondUpdate; private float _lastTenthSecondUpdate; private float _lastTenSecondsUpdate; public bool isInited { get; private set; } private void _InitGame() { var type = Type.GetType(GAME_TYPE_NAME); if (null == type) { Debug.LogError($"[Main: _InitGame] {GAME_TYPE_NAME} Type found"); return; } DebugUtil.Log($"[Main: _InitGame] {GAME_TYPE_NAME} Type found, start creating instance"); _game = Activator.CreateInstance(type) as BaseGame; if (_game == null) { DebugUtil.LogError("[Main: _InitGame] PhxhSDK.Main: no game instance created"); return; } DebugUtil.Log($"[Main: _InitGame] {GAME_TYPE_NAME} instance created successfully: {_game.GetType().FullName}"); #if UNITY_EDITOR SDKManager.GetSingleton().InitEarly(); #endif SDKManager.GetSingleton().Init(); DebugUtil.Log("[Main: _InitGame] SDKManager:Init 完成"); _game.Init(); DebugUtil.Log("[Main: _InitGame] Game:Init 完成"); _game.Start(); DebugUtil.Log("[Main: _InitGame] Game:Start 完成"); } private async UniTask _InitAsync() { DebugUtil.Log("[Main: _InitAsync] 开始异步初始化"); isInited = false; #if USE_YOO await YooAssetHelper.Init(); DebugUtil.Log("[Main: _InitAsync] 资源系统初始化完成"); #endif isInited = true; _InitGame(); DebugUtil.Log("[Main: _InitAsync] 游戏初始化完成"); } private void Awake() { if (null == AspectManager.Instance) gameObject.AddComponent(); _InitSomething(); } private void _InitSomething() { } private async UniTask _DynamicEnableLog() { Debug.Log("[Main:_DynamicEnableLog] 开始动态设置日志"); if (InformationShower.Instance != null) { // 先默认关闭 InformationShower.Instance.enabled = false; } // 默认关闭 var shouldEnableDebug = false; #if DEBUG || DEVELOPMENT_BUILD || UNITY_EDITOR shouldEnableDebug = true; #endif Debug.Log("[Main:_DynamicEnableLog] 初始日志开关: " + shouldEnableDebug); if (!shouldEnableDebug) { // 不开启的时候, 如果在内网环境则强行开启 if (await NetChecker.CheckIsInsideNet()) { shouldEnableDebug = true; Debug.Log("[Main:_DynamicEnableLog] 检测到内网环境, 强制开启日志"); } } Debug.Log("[Main:_DynamicEnableLog] 日志开关状态: " + shouldEnableDebug); Debug.unityLogger.logEnabled = shouldEnableDebug; DebugUtil.LogEnable = shouldEnableDebug; Debug.Log("[Main:_DynamicEnableLog] 日志开关已设置: " + shouldEnableDebug); // 同步控制FPS显示 if (InformationShower.Instance != null) { InformationShower.Instance.enabled = shouldEnableDebug; Debug.Log("[Main:_DynamicEnableLog] FPS信息展示组件状态: " + shouldEnableDebug); } } private void Start() { _DynamicEnableLog().Forget(); _lastSecondUpdate = Time.time; _lastTenthSecondUpdate = Time.time; _lastTenSecondsUpdate = Time.time; _InitAsync().Forget(); } private void Update() { if (!isInited) return; float currentTime = Time.time; // 每秒更新 if (currentTime - _lastSecondUpdate >= 1.0f) { _game.UpdatePerSecond(currentTime - _lastSecondUpdate); _lastSecondUpdate = currentTime; } // 每0.1秒更新 if (currentTime - _lastTenthSecondUpdate >= 0.1f) { _game.UpdatePerTenthSecond(currentTime - _lastTenthSecondUpdate); _lastTenthSecondUpdate = currentTime; } // 每10秒更新 if (currentTime - _lastTenSecondsUpdate >= 10.0f) { _game.UpdatePerTenSeconds(currentTime - _lastTenSecondsUpdate); _lastTenSecondsUpdate = currentTime; } ReferencePool.Update(Time.realtimeSinceStartup); _game.Update(Time.deltaTime); SDKManager.Instance.Update(); } private void OnApplicationPause(bool pauseStatus) { if (!isInited) return; _game.OnApplicationPause(pauseStatus); } private void OnDestroy() { _game.Release(); SDKManager.Instance.Release(); } } }