Search Unity

[Solved] How to release an AssetReference that's an Audio Clip?

Discussion in 'Addressables' started by jerotas, Dec 13, 2019.

  1. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    I have code that loads an Audio Clip asset reference and puts it in an Audio Source so I can play it.
    Code (csharp):
    1.  
    2.        public AssetReference addressable;
    3.      
    4.         var loadHandle = addressable.LoadAssetAsync<AudioClip>();
    5.         while (!loadHandle.IsDone) {
    6.             yield return new WaitForEndOfFrame();
    7.         }
    8.         var addressableClip = loadHandle.Result;
    9.         AudioSource.clip = addressableClip; // then play it
    10.  
    Later, when it's done playing, how do I unload it and get that memory back?
    Tried: I set AudioSource.clip to null and release the actor, doesn't work, I get no audio memory back:
    Code (csharp):
    1.  
    2. AudioSource.clip = null;
    3. addressable.ReleaseInstance(AudioSource.GameObject);
    4.  
    If I try to call .ReleaseAsset, it says:
    Cannot release a null or unloaded asset.
    UnityEngine.AddressableAssets.AssetReference:ReleaseAsset()


    Puzzled here. Any ideas?
     
    liyangw107 likes this.
  2. AwesomeX

    AwesomeX

    Joined:
    Sep 10, 2013
    Posts:
    117
  3. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Thanks for responding. Well yeah I'm trying to release it when it's no longer playing. It's already not playing and I've already made sure that Audio Source's clip is set to null so nothing's holding on to it. The final step maybe we're not doing right? Maybe we need to save a reference to the async loaded clip for the cleanup phase? No idea.

    Edit - I've tried saving the reference to the loadAsync call so I can call Addressables.Release on it later. No dice. Audio Memory is not reclaimed, according to the profiler.
     
    Last edited: Dec 15, 2019
  4. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    This has been solved. The Profiler running in editor isn't very accurate. But it works.

    To release, it's Addressables.Release(theHandleYouGotWhenLoadingAsync);
     
    FlightOfOne, cg-adam and SugoiDev like this.
  5. caochao88_unity

    caochao88_unity

    Joined:
    May 16, 2019
    Posts:
    26
    you should release audioclip by calling Addressable.Release(theClipYouLoadedByAddressables)
     
  6. cg-adam

    cg-adam

    Joined:
    Jul 30, 2019
    Posts:
    44
    Hoping this means addressable support in master audio :D
     
  7. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Yes, it's already been added and released :)
    Working on Addressable support for Core GameKit at the moment.