Unity Addressable Loading Banks

I am trying to load my banks using Addressables but not getting anywhere. The documentation isn’t quite clear on this, but I am trying to load what I think you guys call stub files? It’s not clear to me how these text files point to the .bank files that I think I should be loading.

When I do I keep getting this error:

[FMOD] assert : assertion: 'mRiffChunk.mType == ChunkType_Riff && mRiffChunk.mID == id' failed
Failed to load Master.bank with error: ERR_FORMAT

Down below is a copy of my code.

 public class AudioMixer : MonoBehaviour
  {
    #region Fields

    public static AudioMixer Instance => _instance;
    private static AudioMixer _instance;
    private const string MasterBankKey = "FMODBanks/Master.bytes";

    public AssetReference MasterBank;
    public AssetReference MasterStringBank;
    public List<AssetReference> Banks;

    public bool PreloadSamples;

    #endregion

    #region Methods

    private void OnEnable()
    {
      Load();
    }

    private void Awake()
    {
      if (_instance == null)
      {
        _instance = this;
        InitializeFMODSystem();
        // StartCoroutine(LoadMasterBank());
      }
      else
      {
        Debug.LogWarning("AudioMixer instance already exists!");
        Destroy(this);
      }
    }

    private void Start()
    {
      RuntimeUtils.EnforceLibraryOrder();
    }

    private void InitializeFMODSystem()
    {
      FMOD.Studio.System system = RuntimeManager.StudioSystem;
      if (!system.isValid())
      {
        Debug.LogError("Cannot get FMOD.Studio.System instance!");
        return;
      }
    }

    public void Load()
    {
      foreach (var bankRef in Banks)
      {
        try
        {
          RuntimeManager.LoadBank(bankRef, PreloadSamples);
        }
        catch (BankLoadException e)
        {
          RuntimeUtils.DebugLogException(e);
        }
      }

      RuntimeManager.WaitForAllSampleLoading();
    }

    private IEnumerator LoadMasterBank()
    {
      AsyncOperationHandle<TextAsset> handle = MasterBank.LoadAssetAsync<TextAsset>();
      yield return handle;

      if (handle.Status == AsyncOperationStatus.Succeeded)
      {
        var bankTextAsset = handle.Result;
        Debug.Log("Loaded TextAsset for Master.bank with size: " + bankTextAsset.bytes.Length);
        FMOD.RESULT result = RuntimeManager.StudioSystem.loadBankMemory(bankTextAsset.bytes, LOAD_BANK_FLAGS.NORMAL, out Bank bank);
        if (result == FMOD.RESULT.OK)
        {
          Debug.Log("Successfully loaded Master.bank");
        }
        else
        {
          Debug.LogError("Failed to load Master.bank with error: " + result);
        }

        // Addressables automatically handle releasing the asset
      }
      else
      {
        Debug.LogError("Failed to load Master.bank addressable");
      }
    }

I would love to see how someone setup their project and an example on how they load banks using Addressables.

1 Like

Hi,

Thank you for this script. I was not able to load events with it however.

Correct, they are text files that contain stub data for loading the source bank. When using addressable, however, we are trying to load them as AssetReferences.

Please set logging to level Log in the FMOD settings
image
and let me know if there are FMOD errors logged in the console.

The issue may be trying to play events before the banks have been loaded. The solution may be loading the banks in a different scene than the one playing the events.

I have slightly modified the Asynchronous Loading example script to work with Addressables:

Addressables Async Loading
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.AddressableAssets;

class ScriptUsageLoading : MonoBehaviour
{
	public List<AssetReference> Banks = new List<AssetReference>();

	// The name of the scene to load and switch to
	public string Scene = null;

	public void Start()
	{
		StartCoroutine(LoadGameAsync());
	}

	void Update()
	{
		// Update the loading indication
	}

	IEnumerator LoadGameAsync()
	{
		// Start an asynchronous operation to load the scene
		AsyncOperation async = SceneManager.LoadSceneAsync(Scene);

		// Don't let the scene start until all Studio Banks have finished loading
		async.allowSceneActivation = false;

		// Iterate all the Studio Banks and start them loading in the background
		// including the audio sample data
		foreach (var bank in Banks)
		{
			FMODUnity.RuntimeManager.LoadBank(bank, true);
		}

		// Keep yielding the co-routine until all the bank loading is done
		// (for platforms with asynchronous bank loading)
		while (!FMODUnity.RuntimeManager.HaveAllBanksLoaded)
		{
			yield return null;
		}

		// Keep yielding the co-routine until all the sample data loading is done
		while (FMODUnity.RuntimeManager.AnySampleDataLoading())
		{
			yield return null;
		}

		// Allow the scene to be activated. This means that any OnActivated() or Start()
		// methods will be guaranteed that all FMOD Studio loading will be completed and
		// there will be no delay in starting events
		async.allowSceneActivation = true;

		// Keep yielding the co-routine until scene loading and activation is done.
		while (!async.isDone)
		{
			yield return null;
		}	
	}
}

Add this to an empty scene and add all the banks that you will need. Then your main scene containing the event emitters and it should load successfully.

Hope this helps, if you have any other issues please do not hesitate to ask!