Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Resources.UnloadUnusedAssets doesn't work as expected

Discussion in 'Addressables' started by kana1939, Jan 6, 2023.

  1. kana1939

    kana1939

    Joined:
    Jan 4, 2022
    Posts:
    9
    // Resources.UnloadUnusedAssets doesn't work in Coroutine.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEditor;
    4. using UnityEngine;
    5.  
    6. public class TestMaterialUnload : MonoBehaviour
    7. {
    8.     private Renderer renderer1;
    9.     private Action action;
    10.     void Start()
    11.     {
    12.         renderer1 = GameObject.CreatePrimitive(PrimitiveType.Cube).GetComponent<Renderer>();
    13.  
    14.         UnLoad();
    15.         UnLoadImmediate();
    16.         StartCoroutine(UnLoadCoroutine());
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         action?.Invoke();
    22.     }
    23.    
    24.     private void UnLoad()
    25.     {
    26.         var material = Material.Instantiate(renderer1.sharedMaterial);
    27.         Debug.Log("UnLoad Before: " + material); // Standard(Clone) (UnityEngine.Material)
    28.         var a = Resources.UnloadUnusedAssets();
    29.  
    30.         a.completed += (op) =>
    31.         {
    32.             Debug.Log("UnLoad After: " + material); // null
    33.         };
    34.     }
    35.    
    36.     private IEnumerator UnLoadCoroutine()
    37.     {
    38.         var material = Material.Instantiate(renderer1.sharedMaterial);
    39.         Debug.Log("UnLoadCoroutine Before: " + material); // Standard(Clone) (UnityEngine.Material)
    40.         var a = Resources.UnloadUnusedAssets();
    41.  
    42.         a.completed += (op) =>
    43.         {
    44.             Debug.Log("UnLoadCoroutine After completed: " + material); // Standard(Clone) (UnityEngine.Material)
    45.         };
    46.        
    47.         while (!a.isDone)
    48.         {
    49.             yield return null;
    50.         }
    51.  
    52.         Debug.LogWarning("UnLoadCoroutine After: " + material); // Standard(Clone) (UnityEngine.Material)
    53.  
    54.         action = () => { Debug.LogWarning("UnLoadCoroutine action: " + material); }; // Standard(Clone) (UnityEngine.Material)
    55.  
    56.         action();
    57.     }
    58.    
    59.     private void UnLoadImmediate()
    60.     {
    61.         var material = Material.Instantiate(renderer1.sharedMaterial);
    62.         Debug.Log("UnLoadImmediate3 Before: " + material); // Standard(Clone) (UnityEngine.Material)
    63.         EditorUtility.UnloadUnusedAssetsImmediate();
    64.         Debug.Log("UnLoadImmediate3 After: " + material); // null
    65.     }
    66.    
    67.    
    68. }
    69.  
     
  2. alchemist_wurzelpurzel

    alchemist_wurzelpurzel

    Joined:
    Sep 4, 2018
    Posts:
    43
    I am not sure what exactly your question is, but if you want to wait for Resources.UnloadUnusedAssets to complete in a Coroutine, you could try this:

    Code (CSharp):
    1. private IEnumerator UnLoadCoroutine()
    2.     {
    3.         var material = Material.Instantiate(renderer1.sharedMaterial);
    4.         Debug.Log("UnLoadCoroutine Before: " + material); // Standard(Clone) (UnityEngine.Material)
    5.         yield return Resources.UnloadUnusedAssets();
    6.  
    7.         Debug.Log("UnLoadCoroutine After completed: " + material);
    8.  
    9.         action();
    10.     }