My game runs at about ~50 FPS, but I’m seeing huge spikes whenever I load something.
Most of the computation goes to the process “Loading.LoadFileHeaders”.
I think my problem is related to the way I load the assets. I’m using addressables, and bassically I want to load a random sound that matches a number of labels, say for example I have 5 possible audios for when you win a combat and I want to choose one at random. The labels may be [“player”, “combat”, “win”].
To do this, I use the code below, wich probably loads all of the audio and generates the spikes when doing that:
public IEnumerator LoadRandomAudioClip (List<AssetLabelReference> labels, AudioSource source)
{
#region > Checks
if (labels == null || labels.Count < 0) { Debug.LogWarning ($"Guarda, pediste un audo al azar pero la lista de etiquetas que pasaste no tenía nada."); yield break; }
if (source == null) { Debug.LogWarning ($"Source era NULL"); yield break; }
#endregion
//We load all the addresses with the labels you requested
AsyncOperationHandle<IList<IResourceLocation>> soundsHandleList = Addressables.LoadResourceLocationsAsync (labels, Addressables.MergeMode.Intersection);
//While loading, wait...
while (soundsHandleList.Status == AsyncOperationStatus.None) { yield return null; }
//If the list is not valid or it haven't found any results return.
if (!soundsHandleList.IsValid () || soundsHandleList.Result.Count < 1) { Debug.LogWarning ($"Pediste un sonido con una serie de etiquetas que ningún sonido tiene, volviendo"); yield break; }
//From the list of possibles sounds choose one at random.
IResourceLocation audioAddress = soundsHandleList.Result[Random.Range (0, soundsHandleList.Result.Count)];
//We remove the list of sounds from memory.
Addressables.Release (soundsHandleList);
//Load the address
yield return StartCoroutine (LoadAddressableAudio (audioAddress, source));
}
private IEnumerator LoadAddressableAudio (IResourceLocation audioAddress, AudioSource source)
{
//We load the selected audio.
AsyncOperationHandle<AudioClip> audioHandle = Addressables.LoadAssetAsync<AudioClip> (audioAddress);
//We wait till the audio has loaded
while (audioHandle.Status == AsyncOperationStatus.None) { yield return null; }
//We put the audio in the audio source and we run it.
Debug.Log ($"We load the audio {audioHandle.Result.name}");
source.clip = audioHandle.Result;
source.Play ();
//We wait till the audio is finished
while (source.isPlaying) { yield return null; }
//Once the audio is finished we remove it from the audio source
source.clip = null;
//We relese the handle of the audio
Addressables.Release (audioHandle);
}
