zoukankan      html  css  js  c++  java
  • [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 }
  • 相关阅读:
    Conversions
    Mispelling4
    A hard puzzle
    Easier Done Than Said?
    利用map可以对很大的数出现的次数进行记数
    A+B Coming
    结构体成员变量
    NSString 类介绍及用法
    复习回顾
    函数与方法对比
  • 原文地址:https://www.cnblogs.com/lyh916/p/10928270.html
Copyright © 2011-2022 走看看