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

125 lines
3.2 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;
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()
{
SDKManager.GetSingleton().InitEarly();
2025-04-28 17:38:49 +08:00
Type type = Type.GetType(GAME_TYPE_NAME);
if (null == type)
{
Debug.LogError($"{GAME_TYPE_NAME} Type found");
return;
}
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)
{
2023-10-21 11:55:23 +08:00
DebugUtil.LogError("PhxhSDK.Main: no game instance created");
2023-08-22 19:08:45 +08:00
}
2025-04-28 17:38:49 +08:00
2023-10-19 15:00:19 +08:00
SDKManager.GetSingleton().Init();
2023-08-22 19:08:45 +08:00
_game.Init();
2024-02-21 16:24:29 +08:00
_game.Start();
2023-08-22 19:08:45 +08:00
}
2024-02-21 16:24:29 +08:00
private async void _InitAsync()
{
isInited = false;
2024-02-21 16:24:29 +08:00
#if USE_YOO
await YooAssetHelper.Init();
#endif
isInited = true;
2024-02-21 16:24:29 +08:00
_InitGame();
}
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>();
2025-08-28 14:53:45 +08:00
}
2025-05-19 17:28:24 +08:00
2025-08-28 14:53:45 +08:00
private void Start()
{
2025-05-07 12:15:40 +08:00
#if DEBUG || DEVELOPMENT_BUILD
Debug.unityLogger.logEnabled = true;
#else
Debug.unityLogger.logEnabled = false;
#endif
2025-05-22 19:07:46 +08:00
_lastSecondUpdate = Time.time;
_lastTenthSecondUpdate = Time.time;
_lastTenSecondsUpdate = Time.time;
2024-02-21 16:24:29 +08:00
_InitAsync();
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
}