赞
踩
AssetBundle又称AB包,是Unity提供的一种用于存储资源的资源压缩包,是对Unity 初始Resources的一种扩展;一般使用的策略是把必须的资源和不需要更新的资源放在Resources文件夹下,其他的资源放在AssetBundle下面。
Unity中的AssetBundle系统是对资源管理的一种扩展,通过将资源分布在不同的AB包中可以最大程度地减少运行时的内存压力,可以动态地加载和卸载AB包,继而有选择地加载内容。
AssetBundle | Resources |
---|---|
资源可分布在多个包中 | 所有资源打包成一个大包 |
存储位置自定义灵活 | 必须存放在Resources目录下 |
压缩方式灵活(LZMA,LZ4) | 资源全部会压缩成二进制 |
支持后期进行动态更新 | 打包后资源只读无法动态更改 |
本次主要介绍Unity官方提供的AB包管理插件AssetBundle Browser 进行打包
正确获取到并安装完插件后,通过 Windows ----> AssetBundle Browser下打开AB包管理面板 一共有三个面板
Configure面板 :能查看当前AB包及其内部资源的基本情况(大小,资源,依赖情况等)
Build面板:负责AssetBundle打包的相关设置 按Build即可进行打包
NoCompression:不压缩,解压快,包较大,不建议使用。
LZMA: 压缩最小,解压慢,用一个资源要解压包下所有资源。
LZ4: 压缩稍大,解压快,用什么解压什么,内存占用低,一般建议使用这种。
一般需要进行更改的设置即为图中勾选的相关选项设置。
Inspect面板:主要用来查看已经打包后的AB包文件的一些详细情况(大小,资源路径等)
3、设置资源所属的AssetBundle包
在需要打包的资源的Inspector面板下方即可选择其应放在哪个AB包下,也可通过New新建AB包将资源放入,放入后再次Build打包即可将此资源打入相应的AB包中。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ABLoadTest : MonoBehaviour { private Dictionary<string, AssetBundle> abCache; private AssetBundle mainAB = null; //主包 private AssetBundleManifest mainManifest = null; //主包中配置文件---用以获取依赖包 private string basePath { get { #if UNITY_EDITOR || UNITY_STANDALONE return Application.dataPath + "/StreamingAssets/"; #elif UNITY_IPHONE return Application.dataPath + "/Raw/"; #elif UNITY_ANDROID return Application.dataPath + "!/assets/"; #endif } } void Start() { abCache = new Dictionary<string, AssetBundle>(); var ab = LoadABTest(); GameObject model = ab.LoadAsset<GameObject>("Cube"); var b = Instantiate<GameObject>(model); // dosomething } AssetBundle LoadABTest() { AssetBundle ab; string abName = "3dmodel.first"; if (mainAB == null) { mainAB = AssetBundle.LoadFromFile(basePath + "StandaloneWindows"); mainManifest = mainAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest"); } //根据manifest获取所有依赖包的名称 固定API string[] dependencies = mainManifest.GetAllDependencies(abName); //循环加载所有依赖包 for (int i = 0; i < dependencies.Length; i++) { //如果不在缓存则加入 if (!abCache.ContainsKey(dependencies[i])) { //根据依赖包名称进行加载 ab = AssetBundle.LoadFromFile(basePath + dependencies[i]); //注意添加进缓存 防止重复加载AB包 abCache.Add(dependencies[i], ab); } } //加载目标包 -- 同理注意缓存问题 if (abCache.ContainsKey(abName)) { Debug.Log($"have load {abName}"); return abCache[abName]; } else { ab = AssetBundle.LoadFromFile(basePath + abName); abCache.Add(abName, ab); Debug.Log($"new load {abName}"); return ab; } } // Update is called once per frame void Update() { if(Input.GetKeyDown(KeyCode.A)) // 同步加载 { var ab = LoadABTest(); GameObject model = ab.LoadAsset<GameObject>("Cube"); var b = Instantiate<GameObject>(model); } else if (Input.GetKeyDown(KeyCode.S))// 异步加载 { var ab = LoadABTest(); StartCoroutine(LoadResAsyncTest(ab)); } else if (Input.GetKeyDown(KeyCode.D))// 单个卸载 { UnLoad("3dmodel.first"); Debug.Log("have UnLoadAll 3dmodel.first"); } else if (Input.GetKeyDown(KeyCode.F))// 全部卸载 { UnLoadAll(); Debug.Log("have UnLoadAll"); } } private IEnumerator LoadResAsyncTest(AssetBundle ab) { if (ab == null) yield return null; var model1 = ab.LoadAssetAsync<GameObject>("Cube"); yield return model1; var async_model = Instantiate((GameObject)model1.asset); // dosomething } //====================AB包的两种卸载方式================= //单个包卸载 public void UnLoad(string abName) { if (abCache.ContainsKey(abName)) { abCache[abName].Unload(false); //注意缓存需一并移除 abCache.Remove(abName); } } //所有包卸载 public void UnLoadAll() { AssetBundle.UnloadAllAssetBundles(false); //注意清空缓存 abCache.Clear(); mainAB = null; mainManifest = null; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。