2022-03-10 16:55:12 +08:00
|
|
|
|
# 资源更新
|
|
|
|
|
|
2022-04-12 19:36:18 +08:00
|
|
|
|
**获取资源版本**
|
|
|
|
|
|
2022-10-08 16:25:36 +08:00
|
|
|
|
对于联机运行模式,在更新补丁清单之前,需要获取一个资源版本。
|
2022-04-12 19:36:18 +08:00
|
|
|
|
|
2022-10-08 16:25:36 +08:00
|
|
|
|
该资源版本可以通过YooAssets提供的接口来更新,也可以通过HTTP访问游戏服务器来获取。
|
2022-03-10 16:55:12 +08:00
|
|
|
|
|
2022-04-12 19:36:18 +08:00
|
|
|
|
````c#
|
|
|
|
|
private IEnumerator UpdateStaticVersion()
|
|
|
|
|
{
|
|
|
|
|
UpdateStaticVersionOperation operation = YooAssets.UpdateStaticVersionAsync();
|
|
|
|
|
yield return operation;
|
2022-03-10 16:55:12 +08:00
|
|
|
|
|
2022-04-12 19:36:18 +08:00
|
|
|
|
if (operation.Status == EOperationStatus.Succeed)
|
|
|
|
|
{
|
|
|
|
|
//更新成功
|
2022-10-08 16:25:36 +08:00
|
|
|
|
string packageCRC = operation.PackageCRC;
|
|
|
|
|
Debug.Log($"Update resource Version : {packageCRC}");
|
2022-04-12 19:36:18 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//更新失败
|
|
|
|
|
Debug.LogError(operation.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
**更新补丁清单**
|
2022-03-10 16:55:12 +08:00
|
|
|
|
|
2022-04-12 19:36:18 +08:00
|
|
|
|
对于联机运行模式,在获取到资源版本号之后,就可以更新资源清单了。
|
2022-03-10 16:55:12 +08:00
|
|
|
|
|
|
|
|
|
````c#
|
|
|
|
|
private IEnumerator UpdatePatchManifest()
|
|
|
|
|
{
|
2022-10-08 16:25:36 +08:00
|
|
|
|
UpdateManifestOperation operation = YooAssets.UpdateManifestAsync(packageCRC);
|
2022-03-10 16:55:12 +08:00
|
|
|
|
yield return operation;
|
|
|
|
|
|
|
|
|
|
if (operation.Status == EOperationStatus.Succeed)
|
|
|
|
|
{
|
|
|
|
|
//更新成功
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//更新失败
|
|
|
|
|
Debug.LogError(operation.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
**补丁包下载**
|
|
|
|
|
|
|
|
|
|
在补丁清单更新完毕后,就可以更新资源文件了。
|
|
|
|
|
|
|
|
|
|
根据产品需求,可以选择更新全部资源,或者只更新部分资源。
|
|
|
|
|
|
|
|
|
|
补丁包下载接口:
|
|
|
|
|
|
2022-05-04 00:12:06 +08:00
|
|
|
|
- YooAssets.CreatePatchDownloader(int downloadingMaxNumber, int failedTryAgain)
|
|
|
|
|
|
|
|
|
|
用于下载更新当前资源版本所有的资源包文件。
|
|
|
|
|
|
|
|
|
|
- YooAssets.CreatePatchDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain)
|
|
|
|
|
|
|
|
|
|
用于下载更新资源标签指定的资源包文件。
|
|
|
|
|
|
|
|
|
|
- YooAssets.CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain)
|
|
|
|
|
|
|
|
|
|
用于下载更新指定的资源列表依赖的资源包文件。
|
2022-03-10 16:55:12 +08:00
|
|
|
|
|
|
|
|
|
````c#
|
|
|
|
|
IEnumerator Download()
|
|
|
|
|
{
|
|
|
|
|
int downloadingMaxNum = 10;
|
|
|
|
|
int failedTryAgain = 3;
|
2022-05-04 00:12:06 +08:00
|
|
|
|
DownloaderOperation downloader = YooAssets.CreatePatchDownloader(downloadingMaxNum, failedTryAgain);
|
2022-03-10 16:55:12 +08:00
|
|
|
|
|
|
|
|
|
//没有需要下载的资源
|
|
|
|
|
if (downloader.TotalDownloadCount == 0)
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//需要下载的文件总数和总大小
|
|
|
|
|
int totalDownloadCount = downloader.TotalDownloadCount;
|
|
|
|
|
long totalDownloadBytes = downloader.TotalDownloadBytes;
|
|
|
|
|
|
|
|
|
|
//注册回调方法
|
2022-06-25 11:41:13 +08:00
|
|
|
|
downloader.OnDownloadErrorCallback = OnDownloadErrorFunction;
|
|
|
|
|
downloader.OnDownloadProgressCallback = OnDownloadProgressUpdateFunction;
|
|
|
|
|
downloader.OnDownloadOverCallback = OnDownloadOverFunction;
|
|
|
|
|
downloader.OnStartDownloadFileCallback = OnStartDownloadFileFunction;
|
2022-03-10 16:55:12 +08:00
|
|
|
|
|
|
|
|
|
//开启下载
|
|
|
|
|
downloader.BeginDownload();
|
|
|
|
|
yield return downloader;
|
|
|
|
|
|
|
|
|
|
//检测下载结果
|
|
|
|
|
if (downloader.Status == EOperationStatus.Succeed)
|
|
|
|
|
{
|
|
|
|
|
//下载成功
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//下载失败
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
|
2022-06-28 20:20:36 +08:00
|
|
|
|
**弱联网更新解决方案**
|
|
|
|
|
|
2022-06-29 10:02:57 +08:00
|
|
|
|
对于偏单机但是也有资源热更需求的项目。当玩家本地网络不稳定或无网络的时候,我们又不希望玩家卡在资源更新步骤而不能正常游戏。所以当玩家本地网络有问题的时候,我们可以跳过资源更新的步骤。
|
2022-06-28 20:20:36 +08:00
|
|
|
|
|
|
|
|
|
````c#
|
2022-06-29 10:02:57 +08:00
|
|
|
|
private IEnumerator UpdateStaticVersion()
|
2022-06-28 20:20:36 +08:00
|
|
|
|
{
|
2022-06-29 10:02:57 +08:00
|
|
|
|
UpdateStaticVersionOperation operation = YooAssets.UpdateStaticVersionAsync(10);
|
2022-06-28 20:20:36 +08:00
|
|
|
|
yield return operation;
|
2022-06-29 10:02:57 +08:00
|
|
|
|
if (operation.Status == EOperationStatus.Succeed)
|
2022-06-28 20:20:36 +08:00
|
|
|
|
{
|
2022-06-29 10:02:57 +08:00
|
|
|
|
// 如果获取远端资源版本成功,说明当前网络连接并无问题,可以走正常更新流程。
|
|
|
|
|
......
|
2022-10-08 16:25:36 +08:00
|
|
|
|
|
2022-06-29 10:02:57 +08:00
|
|
|
|
// 注意:在成功下载所有资源之后,我们需要记录当前最新的资源版本号
|
2022-10-08 16:25:36 +08:00
|
|
|
|
PlayerPrefs.SetString("STATIC_VERSION", packageCRC);
|
2022-06-28 20:20:36 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-06-29 10:02:57 +08:00
|
|
|
|
// 如果获取远端资源版本失败,我们走弱联网更新模式。
|
|
|
|
|
// 注意:如果从来没有保存过版本信息,则需要从内部读取StaticVersion.bytes文件的版本信息。
|
2022-10-08 16:25:36 +08:00
|
|
|
|
string packageCRC = PlayerPrefs.GetString("STATIC_VERSION", string.Empty);
|
|
|
|
|
if (packageCRC == string.Empty)
|
2022-06-29 10:02:57 +08:00
|
|
|
|
{
|
2022-10-08 16:25:36 +08:00
|
|
|
|
packageCRC = LoadStaticVersionFromStreamingAssets();
|
2022-06-29 10:02:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 在弱联网情况下更新补丁清单
|
2022-10-08 16:25:36 +08:00
|
|
|
|
UpdateManifestOperation operation2 = YooAssets.WeaklyUpdateManifestAsync(packageCRC);
|
2022-06-29 10:02:57 +08:00
|
|
|
|
yield return operation2;
|
|
|
|
|
if (operation2.Status == EOperationStatus.Succeed)
|
|
|
|
|
{
|
|
|
|
|
StartGame();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// 指定版本的资源内容本地并不完整,需要提示玩家更新。
|
|
|
|
|
ShowMessageBox("请检查本地网络,有新的游戏内容需要更新!");
|
|
|
|
|
}
|
2022-06-28 20:20:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
|