生活随笔
收集整理的這篇文章主要介紹了
Unity 3D模型展示框架篇之资源打包、加载、热更(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本項目將整合之前Unity程序基礎小框架專欄在Unity 3D模型展示項目基礎上進行整合,并記錄了集成過程中對原腳本的調整過程。增加了Asset Bundle+ILRuntime熱更新技術流程。
本篇文章介紹如何對更新進行代碼檢測以及使用更新資源服務器進行資源熱更新。
創建登錄UI預制體LoginUI.prefab,主要功能按鈕如圖所示:
添加項目啟動腳本ProLaunch.cs,主要進行熱更資源檢測、下載更新操作。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.SceneManagement;
using UnityEngine.UI;public class ProLaunch : MonoBehaviour
{public Text UpdateText
;public Text DownText
;public Button btnCheckAndUpdate
;public Button btnUpdate
;public Button btnDown
;public Button btnLogin
;public Slider Slider
;private List<object> _updateKeys
= new List<object>();void Start(){btnCheckAndUpdate
.onClick
.AddListener(() =>{StartCoroutine(DoUpdateAddressadble());});btnUpdate
.onClick
.AddListener(() =>{UpdateCatalog();});btnDown
.onClick
.AddListener(() =>{DownLoad();});btnLogin
.onClick
.AddListener(() =>{SceneManager
.LoadScene(1);});}void Update(){ }public async void UpdateCatalog(){var init
= Addressables
.InitializeAsync();await init
.Task
;var handle
= Addressables
.CheckForCatalogUpdates(false);await handle
.Task
;Debug
.Log("check catalog status " + handle
.Status
);if (handle
.Status
== AsyncOperationStatus
.Succeeded
){List<string> catalogs
= handle
.Result
;if (catalogs
!= null && catalogs
.Count
> 0){foreach (var catalog
in catalogs
){Debug
.Log("catalog " + catalog
);}Debug
.Log("download catalog start ");UpdateText
.text
= UpdateText
.text
+ "\n下載更新catalog";var updateHandle
= Addressables
.UpdateCatalogs(catalogs
, false);await updateHandle
.Task
;foreach (var item
in updateHandle
.Result
){Debug
.Log("catalog result " + item
.LocatorId
);foreach (var key
in item
.Keys
){Debug
.Log("catalog key " + key
);}_updateKeys
.AddRange(item
.Keys
);}Debug
.Log("download catalog finish " + updateHandle
.Status
);UpdateText
.text
= UpdateText
.text
+ "\n更新catalog完成" + updateHandle
.Status
;}else{Debug
.Log("dont need update catalogs");UpdateText
.text
= "沒有需要更新的catalogs信息";}}Addressables
.Release(handle
);}public void DownLoad(){StartCoroutine(DownAssetImpl());}public IEnumerator DownAssetImpl(){var downloadsize
= Addressables
.GetDownloadSizeAsync(_updateKeys
);yield return downloadsize
;Debug
.Log("start download size :" + downloadsize
.Result
);UpdateText
.text
= UpdateText
.text
+ "\n更新文件大小" + downloadsize
.Result
;if (downloadsize
.Result
> 0){var download
= Addressables
.DownloadDependenciesAsync(_updateKeys
, Addressables
.MergeMode
.Union
);yield return download
;Debug
.Log("download result type " + download
.Result
.GetType());UpdateText
.text
= UpdateText
.text
+ "\n下載結果類型 " + download
.Result
.GetType();foreach (var item
in download
.Result
as List<UnityEngine.ResourceManagement.ResourceProviders.IAssetBundleResource>){var ab
= item
.GetAssetBundle();Debug
.Log("ab name " + ab
.name
);UpdateText
.text
= UpdateText
.text
+ "\n ab名稱 " + ab
.name
;foreach (var name
in ab
.GetAllAssetNames()){Debug
.Log("asset name " + name
);UpdateText
.text
= UpdateText
.text
+ "\n asset 名稱 " + name
;}}Addressables
.Release(download
);}Addressables
.Release(downloadsize
);}IEnumerator LoadScene(string senceName
){var handle
= Addressables
.LoadSceneAsync(senceName
);if (handle
.Status
== AsyncOperationStatus
.Failed
){Debug
.LogError("場景加載異常: " + handle
.OperationException
.ToString());yield break;}while (!handle
.IsDone
){float percentage
= handle
.PercentComplete
;Debug
.Log("進度: " + percentage
);yield return null;}Debug
.Log("場景加載完畢");}IEnumerator DoUpdateAddressadble(){AsyncOperationHandle<IResourceLocator> initHandle
= Addressables
.InitializeAsync();yield return initHandle
;var checkHandle
= Addressables
.CheckForCatalogUpdates(false);yield return checkHandle
;if (checkHandle
.Status
!= AsyncOperationStatus
.Succeeded
){OnError("CheckForCatalogUpdates Error\n" + checkHandle
.OperationException
.ToString());yield break;}if (checkHandle
.Result
.Count
> 0){var updateHandle
= Addressables
.UpdateCatalogs(checkHandle
.Result
, false);yield return updateHandle
;if (updateHandle
.Status
!= AsyncOperationStatus
.Succeeded
){OnError("UpdateCatalogs Error\n" + updateHandle
.OperationException
.ToString());yield break;}List<IResourceLocator> locators
= updateHandle
.Result
;foreach (var locator
in locators
){List<object> keys
= new List<object>();keys
.AddRange(locator
.Keys
);var sizeHandle
= Addressables
.GetDownloadSizeAsync(keys
);yield return sizeHandle
;if (sizeHandle
.Status
!= AsyncOperationStatus
.Succeeded
){OnError("GetDownloadSizeAsync Error\n" + sizeHandle
.OperationException
.ToString());yield break;}long totalDownloadSize
= sizeHandle
.Result
;UpdateText
.text
= UpdateText
.text
+ "\ndownload size : " + totalDownloadSize
;Debug
.Log("download size : " + totalDownloadSize
);if (totalDownloadSize
> 0){var downloadHandle
= Addressables
.DownloadDependenciesAsync(keys
, Addressables
.MergeMode
.Union
, false);while (!downloadHandle
.IsDone
){if (downloadHandle
.Status
== AsyncOperationStatus
.Failed
){OnError("DownloadDependenciesAsync Error\n" + downloadHandle
.OperationException
.ToString());yield break;}float percentage
= downloadHandle
.PercentComplete
;Debug
.Log($"已下載: {percentage}");DownText
.text
= $"已下載: {Mathf.Round(percentage * 100)}%";Slider
.value = percentage
;if (percentage
>= 0.9f){Slider
.value = 1; }yield return null;}yield return downloadHandle
;if (downloadHandle
.Status
== AsyncOperationStatus
.Succeeded
){Debug
.Log("下載完畢!");DownText
.text
= DownText
.text
+ " 下載完畢";}}}}else{UpdateText
.text
= UpdateText
.text
+ "\n沒有檢測到更新";}EnterPro();}void EnterPro(){UpdateText
.text
= UpdateText
.text
+ "\n進入游戲場景";Debug
.Log("進入游戲");}private void OnError(string msg
){UpdateText
.text
= UpdateText
.text
+ $"\n{msg}\n請重試! ";}}
注意使用代碼更新時,需要修改熱AddressableAssetSettings更新設置否則代碼將無法檢測攔截更新信息。
做完以上工作,進行熱更Groups的設置與修改。創建一個遠程更新組并命名為RemoteGroup,按照圖中所示進行熱更資源配置。
配置熱更資源服務器,web服務器為IIS,如何創建應用程序這里不做贅述,有不懂的小伙伴留言。這里注意將熱更新資源.bundle、.hash加入到應用程序的MIME中使得Unity項目可以訪問到,否則404無法訪問資源。
配置好IIS服務器后訪問地址 http://www.btlfxx.cn/unity_cmj/tempas,沒毛病!!!
Unity項目中配置遠程更新服務器
配置信息
配置完成后New Build 一下,重新生成一下.bin文件。
構建成功信息
打開本地構建熱更資源的文件夾,看到如下文件信息。這些就是需要Copy到服務器上的熱更文件,Copy服務器之后在進行如下配置。
確定項目熱更新的工作模式
運行Unity項目
點擊檢測并更新
點擊登錄后跳轉場景
下面做熱更操作,將Switch預制體中的模塊隱藏掉。重新構建資源熱更包,將熱更包Copy到服務器上。
運行項目,下面做一個能下載資源的熱更包。
將RemoteGroup組中的Switch中的主體添加一個3D物體,重新打包。可以發現3個更新文件,組remotegroup新生成一個資源包,只需要Copy這3個文件到服務器上。
演示效果
Unity AA熱更新
總結
以上是生活随笔為你收集整理的Unity 3D模型展示框架篇之资源打包、加载、热更(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。