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
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.
1 Like
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.
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.
Here is an example script I just wrote for how this could work.
Ill add it to our docs.
using System.Collections;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
/// <summary>
/// In this example the Audio Clip is only used by this script and can be unloaded after the clip has finished playing.
/// By using ReleaseAsset we can tell the localization system that it is safe to unload the asset.
/// </summary>
public class ReleaseAssetExample : MonoBehaviour
{
public LocalizedAudioClip localizedAudioClip = new LocalizedAudioClip { TableReference = "My Table", TableEntryReference = "My Audio Clip" };
public AudioSource audioSource;
bool isLoadingAndPlaying;
private void OnGUI()
{
if (isLoadingAndPlaying)
{
GUILayout.Label("Loading & Playing Clip");
return;
}
if (GUILayout.Button("Load & Play Audio Clip"))
{
StartCoroutine(LoadAndPlay());
}
}
IEnumerator LoadAndPlay()
{
isLoadingAndPlaying = true;
var clipOperation = localizedAudioClip.LoadAssetAsync();
// Acquire the operation. If another part of code was to call ReleaseAsset this would
// prevent the asset from being unloaded whilst we are still using it.
Addressables.ResourceManager.Acquire(clipOperation);
// Wait for the clip to load.
yield return clipOperation;
// Play the clip.
audioSource.clip = clipOperation.Result;
audioSource.Play();
// Wait for the clip to finish.
yield return new WaitForSeconds(clipOperation.Result.length);
// Release our handle
audioSource.clip = null;
Addressables.Release(clipOperation);
// Get the asset table
var table = LocalizationSettings.AssetDatabase.GetTable(localizedAudioClip.TableReference);
// Tell the Asset Table to release the cached version. The asset will now
// be unloaded as long as there are no other references.
table.ReleaseAsset(localizedAudioClip.TableEntryReference);
isLoadingAndPlaying = false;
}
}
1 Like
Thank you very match, it’s all more clear now.
1 Like