Wwise Integration and C# Code
These are the steps to integrate a new Wwise project with a fresh build of the Unity Roll-a-Ball tutorial. These steps are also here for slightly older versions of Unity & Wwise
- Create a new Unity project OR import a tutorial file from the Unity Asset Store.
- Save Scene & Project.
- Quit Unity.
- Open Wwise Launcher. Go to the Unity tab and find your project.
- Click the blue Integrate Wwise into Unity button. Review the stats... This will be different depending on current version numbers at the time you do this. Just be sure that versions of Wwise are identical for both the middleware editing application and the Unity integration package.
- Click Integrate; sit tight…
- Click Open in Wwise to open the relevant Wwise project. Get to work! Once you have completed your SFX and music cues, you can bundle everything in a sound bank and get it ready for Unity in the next step.
- Choose Layouts > SoundBank and click SoundBanks Tab. Create a new soundbank called MAIN as a Child of the Default Work Unit.
- Drag MAIN to the Default Work Unit in the SoundBank Manager.
- Tick the boxes for Platform and Language as appropriate.
- Click Generate at the top of the SoundBank Manager.
- Switch to the Events Tab. Drag all of your audio events to MAIN (in the SoundBank Manager window) and click Generate once again. A dialog box will appear when this process is complete. Check to see that number of media items is correct in the Log.
- Launch Unity and open the your project. Note that Wwise Listeners are already attached to the Main Camera. Look at the hierarchy in Window > Wwise Picker to confirm that all of your Events are available here in Unity.
- Drag the MAIN SoundBank from Wwise Picker to the Player Game Object (or any other element that will persist throughout the game). Set Load On to Start and Unload On to Nothing.
- At this point matters get personal... Depending on what you need to do in your game there are many options to cue sounds that are meant to coincide with in-game events and actions.
To play a persistent sound, or to trigger on an in-game event you can often limit your work to the Unity Inspector. Select a Game Object in Unity, click Add Component in the object's Inspector, and define the following parameters:
- Trigger On determines what event will cue the sound. For example, AkTriggerEnter will cue the sound if the object has a Box Collider component and IS a trigger. Awake will cue the sound as soon as the game object is ready within its scene.
- Event Name, as you'd imagine, specifies which sound from your Wwise project you need to play.
Some solutions require additional work in code. To get Unity to communicate with Wwise in a more sophisticated manner, copy these code starters and paste into the C# files for your game.
Play Wwise Event (and the sound(s) it contains)
Play/cue a sound directly from a C# script. This approach makes no use of the Unity Inspector at all.
- Sound_Event is the name of the event created in Wwise. These Events are of the "Play" type which is defined in the Event Editor. Each has a "Target" such as a Random Container, Sound SFX, Switch Container, and so on.
- No other details are required. gameObject as an argument is detailed in the Wwise documentation.
// This line goes inside a function or other code block to cue the sound at the time it's needed
AkSoundEngine.PostEvent ("Sound_Event", gameObject);
Play Wwise Event and Modulate an Associated Parameter
Play/cue the sound using the steps above. In addition, use this C# statement to modulate the RTPC time line of that sound using an in-game parameter.
Note the different parameter references progress_ge and Progress_MW:
- Progress_MW is the name of the RTPC created in Wwise. The _MW suffix is there to make it clear that this is part of the MiddleWare (Wwise)
- progress_ge is the name for the game parameter in Unity that is meant to drive the RTPC. The _ge suffix should clarify that this is an element that exists in code and is created within the Game Engine (Unity).
// This line goes inside a function like FixedUpdate() so that values are continuously updated
AkSoundEngine.SetRTPCValue ("Progress_MW",progress_ge,GameObject.FindGameObjectWithTag("Player")); // send velocity value (progress_ge) to Wwise Engine as RTPC (Progress_MW) and modulate playback
Control Interactive Music States
With Wwise most of the heavy lifting is done in the middleware tool: States are defined and paired with Switch Containers. With these elements in place, Unity "directs traffic" by cueing a State at the appropriate time.
// This line should appear within the code block that is used to prompt a change or development in your music
AkSoundEngine.SetState ("State_Group_MW","State_Name_MW");
// For example, if a player has initiated a new game, tell the Game_Music State Group to play the Intro_State state.
if (GameStarted == true)
{
AkSoundEngine.SetState ("Game_Music","Intro_State");
}
Related Resources
Musical Building Blocks: Wwise Wwise examples drawn from Composing Music for Games by Chance Thomas.
<