Adventure Creator Wikia
Advertisement

By default, music track data is stored in the Settings Manager - meaning it will be included in the build and loaded upon startup. This script allows you to instead use Unity's Addressables system, so that track data is only loaded when you request it.

To use it:

  1. Importing and set up Unity's Addressables package from the Package Manager.
  2. Use AC's Music Editor window to define your tracks - but only give each track a label. Don't assign a track's AudioClip asset file directly.
  3. For each AudioClip, instead mark it as Addressable.
  4. Copy/paste the code below into a C# file named MusicAddressables.
  5. Create a new GameObject, attach the new Music Addressables component, make it a prefab and remove from the scene.
  6. In the Music Addressables component Inspector, fill in details of each track - pairing each track's ID number (to the left of its name in the Music Editor window) with its associated clip's Addressable.
  7. When you wish to load a track into memory, use the Object: Call event Action to run the prefab's Load function, passing the ID number of the track to load.
  8. (Optional) To unload a track, use the same Action to run the Unload function.


MusicAddressables.cs:

using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.AddressableAssets;
using System.Collections;
using UnityEngine;
using AC;

public class MusicAddressables : MonoBehaviour
{

	[SerializeField] private MusicAddressable[] musicAddressables = new MusicAddressable[0];

	public void Load (int trackID)
	{
		foreach (MusicAddressable musicAddressable in musicAddressables)
		{
			if (musicAddressable.trackID == trackID) StartCoroutine (LoadCo (musicAddressable));
		}
	}

	public void Unload (int trackID)
	{
		foreach (MusicAddressable musicAddressable in musicAddressables)
		{
			if (musicAddressable.trackID == trackID) musicAddressable.Unload ();
		}
	}


	public void LoadAll ()
	{
		foreach (MusicAddressable musicAddressable in musicAddressables)
		{
			StartCoroutine (LoadCo (musicAddressable));
		}
	}

	private void OnDisable ()
	{
		foreach (MusicAddressable musicAddressable in musicAddressables)
		{
			musicAddressable.Unload ();
		}
	}

	private IEnumerator LoadCo (MusicAddressable musicAddressable)
	{
		if (!musicAddressable.handle.IsValid ())
		{
			AsyncOperationHandle<AudioClip> handle = Addressables.LoadAssetAsync<AudioClip> (musicAddressable.reference);
			yield return handle;
			musicAddressable.handle = handle;
		}

		musicAddressable.Load ();
	}

	[System.Serializable]
	private class MusicAddressable
	{

		public int trackID;
		public AssetReference reference;
		public AsyncOperationHandle<AudioClip> handle;

		public void Load ()
		{
			if (handle.Status == AsyncOperationStatus.Succeeded && handle.Result != null)
			{
				KickStarter.settingsManager.musicStorages[trackID].audioClip = handle.Result;
			}
		}

		public void Unload ()
		{
			if (handle.Status == AsyncOperationStatus.Succeeded && handle.Result != null)
			{
				Addressables.Release (handle);
			}
			KickStarter.settingsManager.musicStorages[trackID].audioClip = null;
		}

	}

}
Advertisement