NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/HotUpdate/GameLauncher.cs

77 lines
2.0 KiB
C#
Raw Normal View History

2024-02-22 14:53:23 +08:00
using Code.Scripts.HotUpdate.UI;
2024-02-22 13:24:26 +08:00
using Cysharp.Threading.Tasks;
2024-02-21 16:24:29 +08:00
using PhxhSDK.AOT;
2024-02-22 12:41:18 +08:00
using PhxhSDK.AOT.SimpleLoader;
2024-02-22 14:53:23 +08:00
using PhxhSDK.AOT.VersionUpdate;
2023-10-19 15:00:19 +08:00
using UnityEngine;
public class GameLauncher : MonoBehaviour
{
const string START_SCENE_NAME = "Assets/Scenes/StartScene.unity";
2024-02-22 14:53:23 +08:00
2024-01-30 19:42:52 +08:00
//最大能容忍的下载卡住时长,一旦超过则重新开始下载
2024-01-31 18:33:47 +08:00
// private const float DOWNLOAD_STUCK_TIME_MAX = 10;
2024-01-30 19:42:52 +08:00
2024-02-22 14:02:47 +08:00
#if USE_HCLR
2024-02-22 12:41:18 +08:00
private HybridCLRHelper _hybridCLRHelper;
2024-02-22 14:02:47 +08:00
#endif
2024-02-22 14:53:23 +08:00
private VersionUpdateHandle _versionUpdateHandle;
private ISimpleLoader _simpleLoader;
2023-10-19 15:00:19 +08:00
2024-02-22 14:53:23 +08:00
2023-10-19 15:00:19 +08:00
public bool enableHotUpdate = true;
private void Start()
{
2024-02-22 13:24:26 +08:00
Launch();
2023-10-19 15:00:19 +08:00
}
2024-02-22 14:53:23 +08:00
private async void Launch()
2023-10-19 15:00:19 +08:00
{
#if UNITY_EDITOR
enableHotUpdate = false;
2024-02-21 16:24:29 +08:00
#endif
#if USE_YOO
2024-02-22 13:24:26 +08:00
await YooAssetHelper.Init();
2024-02-22 14:53:23 +08:00
_simpleLoader = new SimpleLoader_YooAsset();
#else
_simpleLoader = new SimpleLoader_Addressables();
2023-10-19 15:00:19 +08:00
#endif
Debug.Log($"Launch Game! enableHotUpdate:{enableHotUpdate}");
2024-02-22 14:53:23 +08:00
_versionUpdateHandle = new VersionUpdateHandle(new TempUIHandle());
var result = await _versionUpdateHandle.CheckUpdate();
if (!result)
{
Debug.LogError("更新失败!,无法进入游戏");
return;
}
2024-02-22 12:41:18 +08:00
2023-10-19 15:00:19 +08:00
if (enableHotUpdate)
2024-02-22 12:41:18 +08:00
{
2024-02-22 14:02:47 +08:00
#if USE_HCLR
2024-02-22 12:41:18 +08:00
Debug.Log("需要热更新,开始加载程序集");
_hybridCLRHelper = new HybridCLRHelper();
_hybridCLRHelper.SetDllList(AOTGenericReferences.PatchedAOTAssemblyList);
2024-02-22 14:53:23 +08:00
_hybridCLRHelper.RegistLoader(_simpleLoader);
2024-02-22 12:41:18 +08:00
2024-02-22 13:24:26 +08:00
await _hybridCLRHelper.LoadAssemblies();
2024-02-22 12:41:18 +08:00
Debug.Log("加载程序集结束!");
2024-02-22 14:02:47 +08:00
#endif
2024-02-22 12:41:18 +08:00
}
2024-02-22 13:24:26 +08:00
await EnterGame();
2023-10-19 15:00:19 +08:00
}
2023-11-08 19:04:23 +08:00
2024-02-22 14:53:23 +08:00
private async UniTask EnterGame()
2023-10-19 15:00:19 +08:00
{
2024-02-22 14:53:23 +08:00
Debug.Log("开始加载游戏场景");
await _simpleLoader.LoadSceneAsync(START_SCENE_NAME);
Debug.Log("加载游戏场景结束");
2023-10-19 15:00:19 +08:00
}
2024-02-22 12:41:18 +08:00
2023-11-08 19:04:23 +08:00
}