ScriptableObjects are asset files used to store information. They are very useful to store configuration data for your game and your objects. By organizing Wwise Events (or other types) in ScriptableObjects, we can avoid manually changing Events in different scripts for example in case we need to do that. It is also nice to keep Events organized in a central place by avoiding to hard-code event paths in your project.
Setting up a ScriptableObject in Unity
We start by creating script with multiple Ak.Wwise.Event types for a hypothetical player. This allows us later to create an asset file that we can use in other scripts to access the Events:
[CreateAssetMenu(menuName = "SO/Audio/Player", fileName = "New Player Sheet")]
public class PlayerAudioData : ScriptableObject
{
[Header("Movement")]
public AK.Wwise.Event footsteps = null;
public AK.Wwise.Event jump = null;
public AK.Wwise.Event dash = null;
public AK.Wwise.Event land = null;
[Header("Attacks")]
public AK.Wwise.Event swordAttack = null;
public AK.Wwise.Event hammerAttack = null;
public AK.Wwise.Event shield = null;
}
We cannot use this ScriptableObject directly as a component. Instead, we create an asset file that contains all the data we defined by navigating in Unity’s Assets menu and selecting the Player asset:
Unity will create a new asset file called New Player Sheet (which we can of course change in the previously created script). Let’s name the file something like PlayerAudio. When we click on the file, we can see empty Wwise Events in the Inspector:
Posting Wwise Events with ScriptableObjects
When we create a script to play Events, we first declare the ScriptableObject:
[SerializeField]
private PlayerAudioData playerAudio;
Then we access any Ak.Wwise.Event
that we have declared in the ScriptableObject and post our Events like this:
playerAudio.jump.Post(this.gameObject);
Or alternatively using AkSoundEngine.PostEvent
:
AkSoundEngine.PostEvent(playerAudio.jump.Id, this.gameObject);
In Unity we attach the ScriptableObject .asset file in the inspector field that we created in the script:
Now if you we want to change a Wwise Event, we can just edit the .asset file instead of looking for occurrences of the Event in multiple scripts, saving us a lot of time.