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

164 lines
4.2 KiB
C#

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<Main>
{
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()
{
Type type = Type.GetType(GAME_TYPE_NAME);
if (null == type)
{
Debug.LogError($"{GAME_TYPE_NAME} Type found");
return;
}
_game = Activator.CreateInstance(type) as BaseGame;
if (_game == null)
{
DebugUtil.LogError("PhxhSDK.Main: no game instance created");
}
SDKManager.GetSingleton().Init();
_game.Init();
_game.Start();
}
private async void _InitAsync()
{
isInited = false;
#if USE_YOO
await YooAssetHelper.Init();
#endif
isInited = true;
_InitGame();
}
private void Awake()
{
if (null == AspectManager.Instance)
gameObject.AddComponent<AspectManager>();
_InitSomething();
}
private void _InitSomething()
{
}
private async UniTask _DynamicEnableLog()
{
if (InformationShower.Instance != null)
{
// 先默认关闭
InformationShower.Instance.enabled = false;
}
// 默认关闭
var shouldEnableDebug = false;
#if DEBUG || DEVELOPMENT_BUILD || UNITY_EDITOR
shouldEnableDebug = true;
#endif
if (!shouldEnableDebug)
{
// 不开启的时候, 如果在内网环境则强行开启
if (await NetChecker.CheckIsInsideNet())
{
shouldEnableDebug = true;
}
}
Debug.Log("[Debug] Debug log enabled: " + shouldEnableDebug);
Debug.unityLogger.logEnabled = shouldEnableDebug;
DebugUtil.LogEnable = shouldEnableDebug;
// 同步控制FPS显示
if (InformationShower.Instance != null)
{
InformationShower.Instance.enabled = shouldEnableDebug;
}
}
private void Start()
{
_DynamicEnableLog();
_lastSecondUpdate = Time.time;
_lastTenthSecondUpdate = Time.time;
_lastTenSecondsUpdate = Time.time;
_InitAsync();
}
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();
}
}
}