Wwise States are a type of Game Sync that allow us to make changes to audio based on predefined global states that are assigned to audio objects and then are triggered in-game. States can be used to change mix settings in response to an event happening in-game for example. In a music system containing musical segments, States are preferred over Switches, since they act on a global scope. To learn more about game object scopes, read the documentation article here: Understanding Global and Game Object Scope.
In this quick tutorial we’ll learn how to create States in the Wwise authoring tool and how to trigger them in Unity.
Table of Contents
Creating States in the Wwise Authoring Tool
To create Wwise States switch to the Game Syncs tab, right click on a States Work Unit and select New Child->State Group:
In this example I will give the State Group the name PlayerState. Now to create States right click on the State Group we just created and select New Child->State:
Create a few States and switch to the States tab of a sound in the Actor-Mixer Hierarchy:
Now we can change the sound properties for each individual state:
Try to change some values for your States, generate the SoundBanks and switch over to Unity.
Setting a Wwise State by using the Ak State component
A very easy method to trigger a State is to use the Ak State component provided by the Wwise integration plugin. It works similar to the Ak Switch component used in the Wwise Switches in Unity tutorial. In this example we want to trigger a State when the player enters a trigger collider. Attach the Ak State component into the GameObject containing the collider:
The Ak State component has following properties:
- Trigger On: A list of Unity Events that will trigger the State.
- Use Other Object: Defines if the State applies to the current or another GameObject. Make sure to check this setting and use the appropriate component provided by the Wwise integration if you want another GameObject to trigger the State. In the example above we want to trigger the State when the player enters the trigger collider. You would want to place the Ak Trigger Enter component in your collider’s GameObject and drag the player GameObject into the Trigger Object field.
- Name: The name of the State the will be triggered.
Check the Use Other Object box, attach the Ak Trigger Enter component to the GameObject, drag the Player GameObject to the Trigger Object field. That’s it!
Setting a Wwise State by using AkSoundEngine.SetState
We can use the
SetState method of the AkSoundEngine namespace to directly set a Switch in code. The code looks like this:
AkSoundEngine.SetState(string stateGroup, string stateValue);
stateGroup is the State Group created in the Wwise Authoring tool and stateValue the State we want to set. Since States are applied on a global scope and not on individual GameObjects like Switches, there is no need to pass a GameObject as an argument.
Setting a Wwise State by using the AK.Wwise.State Type in Unity
Alternatively, we can use the State type to easily set the State in code. To do this, declare the Wwise.State type property first:
[SerializeField] private AK.Wwise.State myState;
You can now select the appropriate State from the inspector window in Unity:
To set the State value simply call the
SetValue method of the State type: