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

159 lines
4.2 KiB
C#

using Code.Scripts.HotUpdate.UI;
using Cysharp.Threading.Tasks;
using PhxhSDK.AOT;
using PhxhSDK.AOT.SimpleLoader;
using PhxhSDK.AOT.VersionUpdate;
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameLauncher : MonoBehaviour
{
const string START_SCENE_NAME = "Assets/Scenes/StartScene.unity";
//最大能容忍的下载卡住时长,一旦超过则重新开始下载
// private const float DOWNLOAD_STUCK_TIME_MAX = 10;
#if USE_HCLR
private HybridCLRHelper _hybridCLRHelper;
#endif
private VersionUpdateHandle _versionUpdateHandle;
private ISimpleLoader _simpleLoader;
private IVersionUpdateUI versionUpdate;
/// <summary>
/// 加载进度
/// </summary>
private float loadProcess;
public bool enableHotUpdate = true;
private void Start()
{
Launch();
}
private async void Launch()
{
loadProcess = 0f;
#if UNITY_EDITOR
enableHotUpdate = false;
#endif
#if USE_YOO
await YooAssetHelper.Init();
_simpleLoader = new SimpleLoader_YooAsset();
#else
_simpleLoader = new SimpleLoader_Addressables();
#endif
Debug.Log($"Launch Game! enableHotUpdate:{enableHotUpdate}");
versionUpdate = new TempUIHandle();
_versionUpdateHandle = new VersionUpdateHandle(versionUpdate);
bool result = await _versionUpdateHandle.CheckUpdate();
if (!result)
{
Debug.LogError("更新失败!,无法进入游戏");
return;
}
if (enableHotUpdate)
{
#if USE_HCLR
Debug.Log("需要热更新,开始加载程序集");
_hybridCLRHelper = new HybridCLRHelper();
_hybridCLRHelper.SetDllList(AOTGenericReferences.PatchedAOTAssemblyList);
_hybridCLRHelper.RegistLoader(_simpleLoader);
await _hybridCLRHelper.LoadAssemblies();
Debug.Log("加载程序集结束!");
#endif
}
await EnterGame();
}
private async UniTask EnterGame()
{
Debug.Log("开始加载游戏场景");
GameInit();
try
{
Scene scene = await _simpleLoader.LoadSceneAsync(START_SCENE_NAME, LoadSceneMode.Additive);
foreach (GameObject go in scene.GetRootGameObjects())
{
if ("EventSystem".Equals(go.name))
{
Destroy(go);
break;
}
}
}
catch (Exception e)
{
Debug.LogError(e);
}
finally
{
await LoadOver();
Debug.Log("加载游戏场景结束");
}
}
private async void GameInit()
{
versionUpdate.UpdateProgress(0.9f);
versionUpdate.UpdateProgressText("初始化游戏...");
await UniTask.Delay(100);
if (loadProcess < 1f)
versionUpdate.UpdateProgress(0.91f);
await UniTask.Delay(100);
if (loadProcess < 1f)
versionUpdate.UpdateProgress(0.92f);
await UniTask.Delay(100);
if (loadProcess < 1f)
versionUpdate.UpdateProgress(0.93f);
await UniTask.Delay(100);
if (loadProcess < 1f)
versionUpdate.UpdateProgress(0.94f);
await UniTask.Delay(100);
if (loadProcess < 1f)
versionUpdate.UpdateProgress(0.95f);
await UniTask.Delay(100);
if (loadProcess < 1f)
versionUpdate.UpdateProgress(0.96f);
await UniTask.Delay(100);
if (loadProcess < 1f)
versionUpdate.UpdateProgress(0.97f);
await UniTask.Delay(100);
if (loadProcess < 1f)
versionUpdate.UpdateProgress(0.98f);
}
private async UniTask LoadOver()
{
while (true)
{
await UniTask.Yield();
Debug.Log($"校验是否加载完:{IVersionUpdateUI.IsLoadOver}");
if (IVersionUpdateUI.IsLoadOver)
{
loadProcess = 1f;
versionUpdate.UpdateProgress(loadProcess);
versionUpdate.UpdateProgressText("完成");
await UniTask.Delay(200);
versionUpdate.CloseAll();
return;
}
}
}
}