参考链接:
https://blog.csdn.net/qq_40338728/article/details/87880870
https://docs.unity3d.com/Manual/AssetBundles-Building.html
https://docs.unity3d.com/Manual/AssetBundles-Native.html
1.BuildAssetBundleOptions
三种压缩方式:
a.None
使用LZMA算法压缩,打出的包体积最小,加载时间最长。使用这种方式压缩的包,在使用前需要整个包解压,从而导致加载时间变长。当整个包解压后,会使用LZ4算法再次压缩
b.UncompressedAssetBundle
不压缩,打出的包体积最大,加载时间最短
c.ChunkBasedCompression
使用LZ4算法压缩,打出的包体积中等,加载时间中等(即介于None和UncompressedAssetBundle之间)。使用这种方式压缩的包,在使用前不需要整个包解压,即要用哪个资源才解压哪个,局部解压
测试(这里只测试了包体积,因为测试的包比较小加载时间差别不大就不贴出来了):
None:
UncompressedAssetBundle:
ChunkBasedCompression:
2.加载AB包的方式(同步 or 异步)
a.AssetBundle.LoadFromMemoryAsync
1 using System.Collections; 2 using System.IO; 3 using UnityEngine; 4 5 public class TestAB : MonoBehaviour 6 { 7 void Start() 8 { 9 StartCoroutine(Load()); 10 } 11 12 IEnumerator Load() 13 { 14 AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes("Assets/AB/cube")); 15 yield return request; 16 AssetBundle cubeAB = request.assetBundle; 17 if (cubeAB != null) 18 { 19 GameObject go = Instantiate(cubeAB.LoadAsset<GameObject>("cube")); 20 go.transform.localScale = Vector3.one * 2; 21 } 22 } 23 }
b.AssetBundle.LoadFromFile
1 using UnityEngine; 2 3 public class TestAB : MonoBehaviour 4 { 5 void Start() 6 { 7 AssetBundle cubeAB = AssetBundle.LoadFromFile("Assets/AB/cube"); 8 if (cubeAB != null) 9 { 10 GameObject go = Instantiate(cubeAB.LoadAsset<GameObject>("cube")); 11 go.transform.localScale = Vector3.one * 2; 12 } 13 } 14 }
c.WWW.LoadFromCacheOrDownload(废弃接口,新接口UnityWebRequest)
1 using System.Collections; 2 using System.IO; 3 using UnityEngine; 4 5 public class TestAB : MonoBehaviour 6 { 7 void Start() 8 { 9 StartCoroutine(Load()); 10 } 11 12 IEnumerator Load() 13 { 14 string path = Application.dataPath + "/AB/cube"; 15 Debug.Log(path); 16 WWW www = WWW.LoadFromCacheOrDownload(path, 1); 17 yield return www; 18 AssetBundle cubeAB = www.assetBundle; 19 if (www.error != null) 20 { 21 Debug.Log(www.error); 22 } 23 if (cubeAB != null) 24 { 25 GameObject go = Instantiate(cubeAB.LoadAsset<GameObject>("cube")); 26 go.transform.localScale = Vector3.one * 2; 27 } 28 } 29 }
d.UnityWebRequest
1 using System.Collections; 2 using UnityEngine; 3 using UnityEngine.Networking; 4 5 public class TestAB : MonoBehaviour 6 { 7 void Start() 8 { 9 StartCoroutine(Load()); 10 } 11 12 IEnumerator Load() 13 { 14 string path = "file:///" + Application.dataPath + "/AB/cube"; 15 Debug.Log(path); 16 UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(path); 17 yield return request.SendWebRequest(); 18 AssetBundle cubeAB = DownloadHandlerAssetBundle.GetContent(request); 19 if (cubeAB != null) 20 { 21 GameObject go = Instantiate(cubeAB.LoadAsset<GameObject>("cube")); 22 go.transform.localScale = Vector3.one * 2; 23 } 24 } 25 }
3.加载AB包中的资源的方式(同步 or 异步)
a.LoadAsset
b.LoadAssetAsync
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?