参考链接:
https://www.jianshu.com/p/b37ee8cea04c
1.AssetBundle.Unload(false):释放AssetBundle文件的内存镜像,但不摧毁从包中Load出来的Asset对象(如下例子2)
2.AssetBundle.Unload(true):释放AssetBundle文件的内存镜像,同时摧毁从包中Load出来的Asset对象(如下例子3)
3.Resources.UnloadUnusedAssets():释放所有没有引用的Asset对象(如下例子4)
测试:
1.新建一个Cube和材质,将材质赋给Cube,然后打包,即Cube包依赖材质包
1 using UnityEngine; 2 3 public class TestAB : MonoBehaviour 4 { 5 void Start() 6 { 7 AssetBundle matAB = AssetBundle.LoadFromFile("Assets/AB/new material"); 8 AssetBundle cubeAB = AssetBundle.LoadFromFile("Assets/AB/cube"); 9 if (cubeAB != null) 10 { 11 GameObject go = Instantiate(cubeAB.LoadAsset<GameObject>("cube")); 12 go.transform.localScale = Vector3.one * 2; 13 } 14 } 15 }
2.
在1的基础上增加一句:matAB.Unload(false);
3.
在1的基础上增加一句:matAB.Unload(true);
4.
AssetBundle.Unload(false),这种卸载方式比较温和,但会存在卸载不干净的情况
AssetBundle.Unload(true),这种卸载方式比较暴力,可能会导致错误的情况
对于AssetBundle.Unload(false),可以配合Resources.UnloadUnusedAssets(),在清掉资源的所有引用后调用,即可清掉残留的资源
如下,两个包产生的资源都会被正确地回收了
1 using UnityEngine; 2 3 public class TestAB : MonoBehaviour 4 { 5 void Start() 6 { 7 AssetBundle matAB = AssetBundle.LoadFromFile("Assets/AB/new material"); 8 AssetBundle cubeAB = AssetBundle.LoadFromFile("Assets/AB/cube"); 9 GameObject go = null; 10 if (cubeAB != null) 11 { 12 go = Instantiate(cubeAB.LoadAsset<GameObject>("cube")); 13 go.transform.localScale = Vector3.one * 2; 14 } 15 matAB.Unload(false); 16 cubeAB.Unload(false); 17 Destroy(go); 18 Resources.UnloadUnusedAssets(); 19 } 20 }
分类:
Unity热更新
【推荐】国内首个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 中如何实现缓存的预热?
2018-05-26 [Unity基础]镜头管理类
2018-05-26 [Unity工具]批量修改Texture