In this tutorial we’ll take a quick look at how to post Wwise Events in Unity by using the components provided by the integration plugin and by writing C# code.
How to post Wwise Events in Unity?
In total, we have four different ways to post Wwise Events in Unity:
- Using the Wwise Picker to drag and drop Events into a GameObject.
- Using the AK Event component.
- Calling AKSoundEngine.PostEvent in code.
- Calling the Post method of a Ak.Wwise.Event type in code.
Let’s go through these methods step-by-step.
Adding a Wwise Bank to the Unity Scene
Before posting a Wwise Event, make sure to add the SoundBank containing the Event to the Unity scene. To do this, add new GameObject to the scene and insert the AK Bank component into the GameObject:
Select the SoundBank in the Name drop down list and you’re good to go.
Posting Wwise Events using the Wwise Picker in Unity
Create a new GameObject and drag a Wwise Event from the Wwise Picker into Unity’s inspector:
The Wwise picker will add an Ak Game Obj and an Ak Ambient component to the GameObject:
Ak Game Obj is automatically added to any GameObject that will post an Event. It passes information as position, Switches, RTPC and environment values to the Wwise sound engine.
With Ak Ambient we get additional configuration options for a specific Event:
- Trigger On: A list of Unity events. The Wwise Event will be posted based on a trigger selected here.
- Action On Event: Enables you to directly override Event parameters defined in Wwise. These Event types can be Stop, Pause, Resume, Break and Release Envelope. Note that if you enable the Action On Event, then you cannot enable the Use Callback function.
- Use Callback: Activates the use of callback functions for a specific GameObject.
What is the difference between AK Ambient and AK Event?
Instead of Ak Ambient, we could also have used the Ak Event component to post a Wwise Event. The main difference between the two is that AK Event will only use the Simple Mode Position Type.
Ak Ambient Position Types
With Ak Ambient we can post one Event into multiple positions instead of just one. There are two options to choose from here:
- Multi Position Mode: Allows an event to play at multiple GameObject positions using only one voice.
- Large Position Mode: Allows an event to play at multiple positions by setting up event positions in one GameObject. That means you don’t have to manage multiple GameObjects.
After having dropped the Event from the Wwise picker to the GameObject, you can simply start the scene to hear the sound.
Posting Wwise Events using AKSoundEngine.PostEvent
Playing Events by using the Audiokinetic sound engine namespace is the most simple way to play Events with C# code. The code looks like this:
AkSoundEngine.PostEvent("EventName" , this.gameObject);
The PostEvent
method expects two arguments. The first argument of the PostEvent method is a string and refers to the name of our desired Event. The second argument is a GameObject. This is the GameObject will we post the Event to. We can pass null
as an argument here to affect the global scope instead of the local GameObject scope.
PostEvent
returns a Playing ID that we can use to manually stop the Event (without creating a new Event in Wwise for that) or even check if the Event is actually playing. To make use of this declare an uint variable first:
private uint playingID;
Assign the return value of the PostEvent
method to playingID
when posting an Event:
playingID = AkSoundEngine.PostEvent("EventName", this.gameObject);
Now we can simply stop the Event by using the StopPlayingID
method and passing the playing Id as an argument to the method:
AkSoundEngine.StopPlayingID(playingID, 500, AkCurveInterpolation.AkCurveInterpolation_Constant);
Posting Wwise Events using the Ak.Wwise.Event Type
The Wwise integration provides different types that are very handy when dealing with Wwise objects. The main benefit of the Wwise.Event type is that we get a drop down list in Unity’s inspector which allows us to choose our desired Event without hard coding any string. To use the Wwise.Event type declare it at the start of the class:
[SerializeField]
private AK.Wwise.Event myEvent = null;
We can select the desired Event in Unity’s inspector as shown here:
Then you can just call the Post
method of the Event type to start the Event:
myEvent.Post(gameObject);
And use the Stop
method to stop the Event:
myEvent.Stop(this.gameObject, 500, AkCurveInterpolation.AkCurveInterpolation_Constant);
Alternatively, you can still use AkSoundEngine.PostEvent
to post the Event by passing the Id
of the Wwise.Event type as the first argument of the method:
AkSoundEngine.PostEvent(myEvent.Id, this.gameObject);
Congratulations! You learned how to post Wwise Events in Unity. Check out the tutorials about controlling Wwise RTPCs in Unity, setting Wwise Switches in Unity or setting Wwise States in Unity.