NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/GameWrapper/Main.cs

184 lines
5.6 KiB
C#
Raw Normal View History

using PhxhSDK;
2024-02-21 16:24:29 +08:00
using PhxhSDK.AOT;
2025-05-19 17:28:24 +08:00
using PhxhSDK.AOT.Aspect;
2025-10-27 14:49:13 +08:00
using PhxhSDK.Reference;
2025-04-28 17:38:49 +08:00
using System;
using System.Reflection;
2026-04-07 19:33:49 +08:00
using Cysharp.Threading.Tasks;
using PhxhSDK.AOT.Net;
2023-08-22 19:08:45 +08:00
using UnityEngine;
namespace Gameplay
2023-08-22 19:08:45 +08:00
{
2023-10-19 15:00:19 +08:00
public class Main : SingletonMono<Main>
2023-08-22 19:08:45 +08:00
{
2025-04-28 17:38:49 +08:00
private const string GAME_TYPE_NAME = "MyGame, GamePlay";
2025-09-04 14:09:23 +08:00
private BaseGame _game;
2025-05-22 19:07:46 +08:00
private float _lastSecondUpdate;
private float _lastTenthSecondUpdate;
private float _lastTenSecondsUpdate;
2024-02-21 16:24:29 +08:00
public bool isInited
2023-08-22 19:08:45 +08:00
{
get;
private set;
2024-02-21 16:24:29 +08:00
}
private void _InitGame()
{
var type = Type.GetType(GAME_TYPE_NAME);
2025-04-28 17:38:49 +08:00
if (null == type)
{
Debug.LogError($"[Main: _InitGame] {GAME_TYPE_NAME} Type found");
2025-04-28 17:38:49 +08:00
return;
}
DebugUtil.Log($"[Main: _InitGame] {GAME_TYPE_NAME} Type found, start creating instance");
2025-04-28 17:38:49 +08:00
2025-09-04 14:09:23 +08:00
_game = Activator.CreateInstance(type) as BaseGame;
2023-08-22 19:08:45 +08:00
if (_game == null)
{
DebugUtil.LogError("[Main: _InitGame] PhxhSDK.Main: no game instance created");
return;
2023-08-22 19:08:45 +08:00
}
DebugUtil.Log($"[Main: _InitGame] {GAME_TYPE_NAME} instance created successfully: {_game.GetType().FullName}");
2025-04-28 17:38:49 +08:00
#if UNITY_EDITOR
SDKManager.GetSingleton().InitEarly();
#endif
2023-10-19 15:00:19 +08:00
SDKManager.GetSingleton().Init();
DebugUtil.Log("[Main: _InitGame] SDKManager:Init 完成");
2023-08-22 19:08:45 +08:00
_game.Init();
DebugUtil.Log("[Main: _InitGame] Game:Init 完成");
2024-02-21 16:24:29 +08:00
_game.Start();
DebugUtil.Log("[Main: _InitGame] Game:Start 完成");
2023-08-22 19:08:45 +08:00
}
private async UniTask _InitAsync()
2024-02-21 16:24:29 +08:00
{
DebugUtil.Log("[Main: _InitAsync] 开始异步初始化");
isInited = false;
2024-02-21 16:24:29 +08:00
#if USE_YOO
await YooAssetHelper.Init();
DebugUtil.Log("[Main: _InitAsync] 资源系统初始化完成");
2024-02-21 16:24:29 +08:00
#endif
isInited = true;
2024-02-21 16:24:29 +08:00
_InitGame();
DebugUtil.Log("[Main: _InitAsync] 游戏初始化完成");
2024-02-21 16:24:29 +08:00
}
2023-12-15 19:25:08 +08:00
2025-08-28 14:53:45 +08:00
private void Awake()
2023-08-22 19:08:45 +08:00
{
2025-05-19 17:28:24 +08:00
if (null == AspectManager.Instance)
gameObject.AddComponent<AspectManager>();
_InitSomething();
}
private void _InitSomething()
{
2025-08-28 14:53:45 +08:00
}
2025-05-19 17:28:24 +08:00
2026-04-07 19:33:49 +08:00
private async UniTask _DynamicEnableLog()
2025-08-28 14:53:45 +08:00
{
Debug.Log("[Main:_DynamicEnableLog] 开始动态设置日志");
2026-04-07 19:33:49 +08:00
if (InformationShower.Instance != null)
{
// 先默认关闭
InformationShower.Instance.enabled = false;
}
// 默认关闭
var shouldEnableDebug = false;
2026-04-07 19:33:49 +08:00
#if DEBUG || DEVELOPMENT_BUILD || UNITY_EDITOR
shouldEnableDebug = true;
2025-05-07 12:15:40 +08:00
#endif
Debug.Log("[Main:_DynamicEnableLog] 初始日志开关: " + shouldEnableDebug);
2026-04-07 19:33:49 +08:00
if (!shouldEnableDebug)
{
// 不开启的时候, 如果在内网环境则强行开启
if (await NetChecker.CheckIsInsideNet())
{
shouldEnableDebug = true;
Debug.Log("[Main:_DynamicEnableLog] 检测到内网环境, 强制开启日志");
2026-04-07 19:33:49 +08:00
}
}
Debug.Log("[Main:_DynamicEnableLog] 日志开关状态: " + shouldEnableDebug);
Debug.unityLogger.logEnabled = shouldEnableDebug;
DebugUtil.LogEnable = shouldEnableDebug;
Debug.Log("[Main:_DynamicEnableLog] 日志开关已设置: " + shouldEnableDebug);
2026-03-27 12:04:38 +08:00
2026-04-07 19:33:49 +08:00
// 同步控制FPS显示
2026-03-27 12:04:38 +08:00
if (InformationShower.Instance != null)
{
InformationShower.Instance.enabled = shouldEnableDebug;
Debug.Log("[Main:_DynamicEnableLog] FPS信息展示组件状态: " + shouldEnableDebug);
2026-03-27 12:04:38 +08:00
}
2026-04-07 19:33:49 +08:00
}
private void Start()
{
_DynamicEnableLog().Forget();
2025-05-22 19:07:46 +08:00
_lastSecondUpdate = Time.time;
_lastTenthSecondUpdate = Time.time;
_lastTenSecondsUpdate = Time.time;
_InitAsync().Forget();
2023-08-22 19:08:45 +08:00
}
private void Update()
{
2025-04-28 17:38:49 +08:00
if (!isInited)
return;
2025-05-22 19:07:46 +08:00
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;
}
2025-10-27 14:49:13 +08:00
ReferencePool.Update(Time.realtimeSinceStartup);
2023-08-22 19:08:45 +08:00
_game.Update(Time.deltaTime);
2024-11-13 16:57:46 +08:00
SDKManager.Instance.Update();
2023-08-22 19:08:45 +08:00
}
private void OnApplicationPause(bool pauseStatus)
{
2025-04-28 17:38:49 +08:00
if (!isInited)
return;
2023-08-22 19:08:45 +08:00
_game.OnApplicationPause(pauseStatus);
}
private void OnDestroy()
{
_game.Release();
2023-10-19 15:00:19 +08:00
SDKManager.Instance.Release();
2023-08-22 19:08:45 +08:00
}
}
2023-10-19 15:00:19 +08:00
}