132 lines
4.8 KiB
C#
132 lines
4.8 KiB
C#
using Cysharp.Threading.Tasks;
|
||
using System.Threading.Tasks;
|
||
using UnityEngine;
|
||
using YooAsset;
|
||
|
||
public class PreloadOperation
|
||
{
|
||
PatchOperationData data;
|
||
PatchOperation operation;
|
||
public PreloadOperation(EPlayMode playMode)
|
||
{
|
||
data = new PatchOperationData();
|
||
data.packageName = "Preload";
|
||
data.playMode = playMode;
|
||
data.useBuildinFileSystem = true;
|
||
data.downloadingMaxNum = 10;
|
||
data.failedTryAgain = 3;
|
||
|
||
operation = new PatchOperation(data);
|
||
}
|
||
public async UniTask Execute()
|
||
{
|
||
#if !UNITY_EDITOR
|
||
CheckIsOffline();
|
||
#endif
|
||
InitializationOperation initializationOperation = await operation.InitializePackage();
|
||
//初始化失败
|
||
if (initializationOperation.Status != EOperationStatus.Succeed)
|
||
{
|
||
MessageBox.Show()
|
||
.SetTitle(operation.data.packageName)
|
||
.SetContent($"{initializationOperation.Error}")
|
||
.AddButton("退出", (box) => { Application.Quit(); });
|
||
return;
|
||
}
|
||
Debug.Log($"初始化成功{operation.data.packageName}");
|
||
var version = await GetBestPackageVersion();
|
||
#if UNITY_EDITOR
|
||
var PackageVersionOperation = await operation.RequestPackageVersion();
|
||
version = PackageVersionOperation.PackageVersion;
|
||
#endif
|
||
//获取版本失败
|
||
if (version == null)
|
||
{
|
||
MessageBox.Show()
|
||
.SetTitle(operation.data.packageName)
|
||
.SetContent("获取版本失败")
|
||
.AddButton("退出", (box) => { Application.Quit(); });
|
||
return;
|
||
}
|
||
operation.packageVersion = version;
|
||
Debug.Log($"获取版本成功{operation.data.packageName}:{version}");
|
||
UpdatePackageManifestOperation updatePackageManifest = await operation.UpdatePackageManifest();
|
||
//获取资源清单失败
|
||
if (updatePackageManifest.Status != EOperationStatus.Succeed)
|
||
{
|
||
MessageBox.Show()
|
||
.SetTitle(operation.data.packageName)
|
||
.SetContent(updatePackageManifest.Error)
|
||
.AddButton("退出", (box) => { Application.Quit(); });
|
||
return;
|
||
}
|
||
await LoadAndShowPatchWindow();
|
||
_ = UpdatePreloadPackage();
|
||
|
||
}
|
||
void CheckIsOffline()
|
||
{
|
||
if (string.IsNullOrEmpty(operation.GetCachedPackageVersion()))
|
||
{
|
||
operation.data.playMode = EPlayMode.OfflinePlayMode;
|
||
}
|
||
}
|
||
async Task<string> GetBestPackageVersion()
|
||
{
|
||
string cachedVersion = operation.GetCachedPackageVersion();
|
||
if (!string.IsNullOrEmpty(cachedVersion))
|
||
{
|
||
return cachedVersion;
|
||
}
|
||
string buildinVersion = await operation.GetBuildinPackageVersion();
|
||
if (!string.IsNullOrEmpty(buildinVersion))
|
||
{
|
||
return buildinVersion;
|
||
}
|
||
return null;
|
||
}
|
||
private async UniTask LoadAndShowPatchWindow()
|
||
{
|
||
var assetHandle = operation.package.LoadAssetAsync<GameObject>("PatchWindow");
|
||
await assetHandle.ToUniTask();
|
||
if (assetHandle.Status == EOperationStatus.Succeed)
|
||
GameObject.Instantiate(assetHandle.AssetObject, GameManager.Inst.MainUICanvas.transform);
|
||
}
|
||
private async UniTask UpdatePreloadPackage()
|
||
{
|
||
var PackageVersionOperation = await operation.RequestPackageVersion();
|
||
if (PackageVersionOperation.Status != EOperationStatus.Succeed)
|
||
{
|
||
Debug.Log($"{operation.data.packageName}后台更新版本失败:{PackageVersionOperation.Error}");
|
||
return;
|
||
}
|
||
operation.packageVersion = PackageVersionOperation.PackageVersion;
|
||
var PackageManifestOperation = await operation.UpdatePackageManifest();
|
||
if (PackageManifestOperation.Status != EOperationStatus.Succeed)
|
||
{
|
||
Debug.Log($"{operation.data.packageName}后台更新资源清单失败:{PackageManifestOperation.Error}");
|
||
return;
|
||
}
|
||
var DownloaderOperation = operation.CreateDownloader();
|
||
if(DownloaderOperation.TotalDownloadCount == 0)
|
||
{
|
||
operation.SaveVersionToCache();
|
||
Debug.Log($"后台更新{operation.data.packageName}完成,版本号{operation.packageVersion}");
|
||
return;
|
||
}
|
||
if (!await operation.DownloadPackageFiles())
|
||
{
|
||
Debug.Log($"{operation.data.packageName}后台文件下载失败");
|
||
return;
|
||
}
|
||
var ClearCacheFilesOperation = await operation.ClearCacheBundle();
|
||
if (ClearCacheFilesOperation.Status != EOperationStatus.Succeed)
|
||
{
|
||
Debug.Log($"{operation.data.packageName}后台清除未使用缓存失败:{ClearCacheFilesOperation.Error}");
|
||
return;
|
||
}
|
||
operation.SaveVersionToCache();
|
||
Debug.Log($"后台更新{operation.data.packageName}完成,版本号{operation.packageVersion}");
|
||
}
|
||
}
|