[Unity热更新]04.卸载方式

参考链接:

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 }

 

posted on 2019-05-26 23:14  艰苦奋斗中 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lyh916/p/10928270.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值