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

147 lines
4.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;
2024-07-16 14:00:58 +08:00
using YooAsset;
2023-10-19 15:00:19 +08:00
public class GameLauncher : MonoBehaviour
{
const string START_SCENE_NAME = "Assets/Scenes/StartScene.unity";
2024-07-16 14:00:58 +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-07-16 14:00:58 +08:00
#if USE_HCLR
2024-02-22 12:41:18 +08:00
private HybridCLRHelper _hybridCLRHelper;
2024-07-16 14:00:58 +08:00
#endif
2024-02-22 14:53:23 +08:00
private VersionUpdateHandle _versionUpdateHandle;
private ISimpleLoader _simpleLoader;
2024-07-16 14:00:58 +08:00
private IVersionUpdateUI versionUpdate;
/// <summary>
/// 加载进度
/// </summary>
private float loadProcess;
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
{
2024-07-16 14:00:58 +08:00
loadProcess = 0f;
2023-10-19 15:00:19 +08:00
#if UNITY_EDITOR
enableHotUpdate = false;
2024-02-21 16:24:29 +08:00
#endif
2024-07-16 14:00:58 +08:00
2024-02-21 16:24:29 +08:00
#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-07-16 14:00:58 +08:00
versionUpdate = new TempUIHandle();
_versionUpdateHandle = new VersionUpdateHandle(versionUpdate);
bool result = await _versionUpdateHandle.CheckUpdate();
2024-02-22 14:53:23 +08:00
if (!result)
{
Debug.LogError("更新失败!,无法进入游戏");
return;
}
2024-07-16 14:00:58 +08:00
2023-10-19 15:00:19 +08:00
if (enableHotUpdate)
2024-02-22 12:41:18 +08:00
{
2024-07-16 14:00:58 +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-07-16 14:19:27 +08:00
2024-02-22 13:24:26 +08:00
await _hybridCLRHelper.LoadAssemblies();
2024-02-22 12:41:18 +08:00
Debug.Log("加载程序集结束!");
2024-07-16 14:00:58 +08:00
#endif
2024-02-22 12:41:18 +08:00
}
2024-07-16 14:00:58 +08:00
2024-02-22 13:24:26 +08:00
await EnterGame();
2023-10-19 15:00:19 +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("开始加载游戏场景");
2024-07-16 14:00:58 +08:00
GameInit();
SceneHandle sceneHandle = await _simpleLoader.LoadSceneAsync(START_SCENE_NAME, UnityEngine.SceneManagement.LoadSceneMode.Additive);
foreach (GameObject go in sceneHandle.SceneObject.GetRootGameObjects())
{
if ("EventSystem".Equals(go.name))
{
Destroy(go);
break;
}
}
await LoadOver();
2024-02-22 14:53:23 +08:00
Debug.Log("加载游戏场景结束");
2023-10-19 15:00:19 +08:00
}
2024-07-16 14:00:58 +08:00
private async void GameInit()
{
versionUpdate.UpdateProgress(0.9f);
versionUpdate.UpdateProgressText("初始化游戏...");
2024-02-22 12:41:18 +08:00
2024-07-16 14:00:58 +08:00
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();
2024-07-16 14:19:27 +08:00
if (IVersionUpdateUI.IsLoadOver)
2024-07-16 14:00:58 +08:00
{
loadProcess = 1f;
versionUpdate.UpdateProgress(loadProcess);
versionUpdate.UpdateProgressText("完成");
await UniTask.Delay(200);
versionUpdate.CloseAll();
return;
2024-07-16 14:19:27 +08:00
}
2024-07-16 14:00:58 +08:00
}
}
2023-11-08 19:04:23 +08:00
}