AssetBundle是Unity提供的一種存檔文件,其中包含了可以在運行時加載的用於特定平臺的非代碼資源。可用於下載內容,減小初始安裝大小,加載針對特定平臺的優化過的資源以及減輕運行時內存壓力。
基礎信息
官方教程
https://learn.unity.com/tutorial/assets-resources-and-assetbundles
https://docs.unity3d.com/Manual/AssetBundlesIntro.html
資源打包
打包策略
打包工具
Unity提供了一個打包工具Asset Bundle Browser tool,可以通過Package Manager安裝,也可以通過GitHub下載。
https://github.com/Unity-Technologies/AssetBundles-Browser
腳本
打包
BuildPipeline.BuildAssetBundles
打包
加載包
AssetBundle.LoadFromMemory(Async)
從內存加載,不推薦使用,UnityWebRequest使用加密下載數據並需要從未加密的字節創建AssetBundle時,此功能很有用。
AssetBundle.LoadFromFile(Async)
從文件加載,效果最好
AssetBundleDownloadHandler
最常用
加載資源
LoadAllAssets
加載超過2/3內容
LoadAsset
加載少量單個
LoadAssetWithSubAssets
包含多個嵌入對象
卸載包
AssetBundle.Unload(true);
推薦,卸載所有內容
AssetBundle.Unload(false);
容易產生重複加載,Resources.UnloadUnusedAssets清除引用
加載依賴
AssetBundleManifest.GetAllDependencies
加載所有依賴
AssetBundleManifest.GetDirectDependencies
加載直接子集依賴