Search Unity

UnloadUnusedAssets can't unload a asset?

Discussion in 'Asset Bundles' started by GARY_LEE_CH, Oct 31, 2018.

  1. GARY_LEE_CH

    GARY_LEE_CH

    Joined:
    Nov 17, 2016
    Posts:
    1
    HI Guys i use AssetBundle.LoadAllAssetsAsync api to get my Asset,but when i remove it,it always in memroy. Then i use LoadAllAssets api and UnloadUnusedAssets work.
    我获取了一个ab包后,使用LoadAllAssetsAsync 接口来获取Asset对象。但是当我清除所有引用并调用UnloadUnusedAssets后在profile工具中还能看到内存占用。如果换成LoadAllAssets 接口获取Asset对象就能正常释放了。请问可能是什么原因?

    ps: by unity5.6.5 and test in phone
    here is my code:
    Code (CSharp):
    1.  public class test : MonoBehaviour {
    2.      public AssetBundle temp = null;
    3.      public AssetBundleRequest req;
    4.      void Start () {
    5.          temp = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "uialtas/340011.pkg"));
    6.          req = temp.LoadAllAssetsAsync(typeof(Sprite));
    7.          //temp.LoadAllAssets(typeof(Sprite));
    8.          //DestroyImmediate(img.gameObject);
    9.          //temp.Unload(false);
    10.          //StartCoroutine(OnExcuteUnloadUnusedAssets());
    11.          //Resources.UnloadUnusedAssets();
    12.      }
    13.      public void Clean() {
    14.          Resources.UnloadUnusedAssets();
    15.      }
    16.      public IEnumerator OnExcuteUnloadUnusedAssets()
    17.      {
    18.          yield return Resources.UnloadUnusedAssets();
    19.          //Debug.Log(1);
    20.      }
    21.      // Update is called once per frame
    22.      void Update () {
    23.          //if (req!=null&&req.isDone)
    24.          if(req!=null&& req.isDone)
    25.          {
    26.              req = null;
    27.              //    Resources.UnloadUnusedAssets();
    28.              //UnityEditor.EditorUtility.UnloadUnusedAssetsImmediate();
    29.              if(temp!= null)
    30.                  temp.Unload(false);
    31.              StartCoroutine(OnExcuteUnloadUnusedAssets());
    32.              //Resources.UnloadUnusedAssets();
    33.          }
    34.        
    35.      }
    36. }
    here is my test project
     

    Attached Files:

    Last edited: Oct 31, 2018