Unity Addressable熱更系統嘗試(一)


受啟發: Addressable 增量包

Addressable Setting中勾選

 

 制作資源

 

 設置定好資源的熱更

 

 下面是檢測更新代碼和加載資源

 1 [SerializeField]
 2     private string _entryName = "Assets/Prefab/GameObject.prefab";
 3 
 4     public Text UpdateLb;
 5     // Start is called before the first frame update
 6     void Start()
 7     {
 8         //var _ = StartAsync();
 9 
10         StartCoroutine(DoUpdateAddressadble());
11     }
12 
13     IEnumerator DoUpdateAddressadble()
14     {
15         AsyncOperationHandle<IResourceLocator> initHandle = Addressables.InitializeAsync();
16         yield return initHandle;
17 
18         var checkHandle = Addressables.CheckForCatalogUpdates(false);
19         yield return checkHandle;
20         if (checkHandle.Result.Count > 0)
21         {
22             var updateHandle = Addressables.UpdateCatalogs(checkHandle.Result, false);
23 
24             yield return updateHandle;
25 
26             List<IResourceLocator> locators = updateHandle.Result;
27             foreach (var locator in locators)
28             {
29                 List<object> keys = new List<object>();
30                 keys.AddRange(locator.Keys);
31                 var sizeHandle = Addressables.GetDownloadSizeAsync(keys);
32                 yield return sizeHandle;
33                 long totalDownloadSize = sizeHandle.Result;
34                 UpdateLb.text = UpdateLb.text + "\ndownload size : " + totalDownloadSize;
35                 Debug.Log("download size : " + totalDownloadSize);
36                 if (totalDownloadSize > 0)
37                 {
38                     var downloadHandle = Addressables.DownloadDependenciesAsync(keys);
39                     while (!downloadHandle.IsDone)
40                     {
41                         float percentage = downloadHandle.PercentComplete;
42                         Debug.Log($"已下載: {percentage}");
43                         UpdateLb.text = UpdateLb.text + $"\n已下載: {percentage}";
44                         yield return null;
45                     }
46                     Debug.Log("下載完畢!");
47                     UpdateLb.text = UpdateLb.text + "\n下載完畢" ;
48                     Addressables.Release(downloadHandle);
49                 }
50             }
51             Addressables.Release(updateHandle);
52         }
53         else
54         {
55             UpdateLb.text = UpdateLb.text + "\n沒有檢測到更新";
56         }
57         Addressables.Release(checkHandle);
58 
59 
60         Load();
61     }
62     void Load()
63     {
64         Addressables.LoadAssetAsync<GameObject>(_entryName).Completed +=
65             (op) =>
66             {
67                 if (op.Status == AsyncOperationStatus.Succeeded)
68                 {
69                     Debug.Log(op.Result as GameObject);
70                     var go = Instantiate(op.Result as GameObject, Vector3.zero, Quaternion.identity, transform);
71                     go.transform.localPosition = Vector3.zero;
72                     go.transform.localScale = Vector3.one;
73                 }
74             };
75     }

原文是打了Log,我為了方便查看加了一個Text
將Addressable資源build好上傳遠程

 

 

 

 

工程打包,這里我打了pc包

運行

======================================================================

好的,這里出了事故

第一次運行之后顯示了加載更新4039,剛好和資源大小一致,但是我直接關了進程想看看第二次打開會不會更新,沒有更新但是第一次的更新沒截圖=  =

然后悲催的我想重新打個包應該就行了,結果是

 

我就不淡定了,我包是重新打了,講道理應該會重新更新才對,

 

於是我把包又重新刪掉,遠程的資源也刪了,按照上面的流程重新操作一遍

結果.... 只顯示更新size 為0

裂開了.....我在重新嘗試中.......

 想到一個辦法,我要坑同事了,哈哈哈..

 

 果然,成功截圖..

在我幾次嘗試后,有這么個疑問:

現象:更新過后,不管我是把包刪除重新build,還是把遠程資源都刪掉,都會顯示無更新

但是我把新加的資源和原有的資源(新打的),一起傳到遠程,他只會更新新加的,但是原有的資源也是可以正常加載出來的

問題:他是把資源更新加載到電腦內存里面嗎,我重新打包和刪除遠程資源無效,是因為更新還在內存中嗎?

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM