Search Unity

Question Release of Localization assets

Discussion in 'Localization Tools' started by MarcBaques, Oct 14, 2022.

  1. MarcBaques

    MarcBaques

    Joined:
    Aug 19, 2021
    Posts:
    14
    Hi,
    We are currently debugging with Addressables Event Viewer our game that uses a LocalizedAudioClip and we are seeing that our AudioClips are not releasing even if we unsuscribe from ChangeAsset event.

    After force a manual release by code we notice that releasing the operation of the LocalisedClip doesn't release the AudioClip when we use Addressables.Release(operation) and we have to release both manually.
    Is this a normal approach with audios and strings or should we take other approachs to Release the audios or the texts of Localized tables?

    Unity: 2021.3.1
    Localization: 1.0.5
    Addressables: 1.19.19
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Hi,

    First please update to 1.3.2, this is the latest version and has many fixes which may help you here.
    Localized assets have a reference inside the AssetTable and then one for each subsequent LocalizedAsset using it.
    So in your example, there are 2 references to the Audio, one in the table and one in the LocalizedAudioClip.
    Unsubscribing from the LocalizedAudioClip will remove a reference leaving it with 1, the cached AssetTable reference.
    If you want to remove that reference you need to get the AssetTable and call ReleaseAsset or ReleaseAssets.
     
    MarcBaques likes this.
  3. MaurizioCarlottaPetoons

    MaurizioCarlottaPetoons

    Joined:
    Jul 9, 2021
    Posts:
    32
    So if understand correctly.
    Loading a LocalizedAudioClip subscribing to AssetChanged and unsubscribing won't unload the referenced audio because as you said it keeps a reference in the cached AssetTable. We have to release the Audio from the table from our side manually or force a change of Locale as states on ReleaseAssets documentation.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Yes. When you load it we hold onto a reference in the table to prevent the asset from being repeatedly loaded and unloaded. You can Release it manually at any time however if you then call LoadAsset it will become cached again.
     
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Here is an example script I just wrote for how this could work.
    Ill add it to our docs.

    Code (csharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.AddressableAssets;
    4. using UnityEngine.Localization;
    5. using UnityEngine.Localization.Settings;
    6.  
    7. /// <summary>
    8. /// In this example the Audio Clip is only used by this script and can be unloaded after the clip has finished playing.
    9. /// By using ReleaseAsset we can tell the localization system that it is safe to unload the asset.
    10. /// </summary>
    11. public class ReleaseAssetExample : MonoBehaviour
    12. {
    13.     public LocalizedAudioClip localizedAudioClip = new LocalizedAudioClip { TableReference = "My Table", TableEntryReference = "My Audio Clip" };
    14.     public AudioSource audioSource;
    15.  
    16.     bool isLoadingAndPlaying;
    17.  
    18.     private void OnGUI()
    19.     {
    20.         if (isLoadingAndPlaying)
    21.         {
    22.             GUILayout.Label("Loading & Playing Clip");
    23.             return;
    24.         }
    25.  
    26.         if (GUILayout.Button("Load & Play Audio Clip"))
    27.         {
    28.             StartCoroutine(LoadAndPlay());
    29.         }
    30.     }
    31.  
    32.     IEnumerator LoadAndPlay()
    33.     {
    34.         isLoadingAndPlaying = true;
    35.  
    36.         var clipOperation = localizedAudioClip.LoadAssetAsync();
    37.  
    38.         // Acquire the operation. If another part of code was to call ReleaseAsset this would
    39.         // prevent the asset from being unloaded whilst we are still using it.
    40.         Addressables.ResourceManager.Acquire(clipOperation);
    41.  
    42.         // Wait for the clip to load.
    43.         yield return clipOperation;
    44.  
    45.         // Play the clip.
    46.         audioSource.clip = clipOperation.Result;
    47.         audioSource.Play();
    48.  
    49.         // Wait for the clip to finish.
    50.         yield return new WaitForSeconds(clipOperation.Result.length);
    51.  
    52.         // Release our handle
    53.         audioSource.clip = null;
    54.         Addressables.Release(clipOperation);
    55.  
    56.         // Get the asset table
    57.         var table = LocalizationSettings.AssetDatabase.GetTable(localizedAudioClip.TableReference);
    58.  
    59.         // Tell the Asset Table to release the cached version. The asset will now
    60.         // be unloaded as long as there are no other references.
    61.         table.ReleaseAsset(localizedAudioClip.TableEntryReference);
    62.  
    63.         isLoadingAndPlaying = false;
    64.     }
    65. }
     
  6. MaurizioCarlottaPetoons

    MaurizioCarlottaPetoons

    Joined:
    Jul 9, 2021
    Posts:
    32
    Thank you very match, it's all more clear now.
     
    karl_jones likes this.