Table des matières
Before engaging this section, you must complete the previous section - Loading SoundBanks using Triggers.
A common method in structuring your game contents is separating them into sequences, episodes, or levels; the Wwise Adventure Game is no different. Currently, you are loading the Woodlands SoundBank when entering the Woodlands Trigger game object.
This approach is perfect for some games and very easy to set up, but with that method you have to ensure all Events in the Woodlands SoundBank are not posted before the SoundBank is loaded. Another method of achieving a similar result is to load the SoundBank from a Scene containing all the related Events. This allows you to very effectively manage the assets related to a SoundBank, and as the Awake and Start functions are called depending on the initialization of the Scene they are located in, you can use those Trigger On options to ensure that the assets are loaded in the correct order.
Another advantage is that by unloading a Scene in Unity, all Events within it will be stopped. As such, by keeping Woodlands exclusive content in it's own scene, you ensure all Events related to the region SoundBank start and stop along with the SoundBank. Moreover, you can also add any Unity game object to the scene, like enemies or a rotating windmill, allowing to better manage your game's memory and CPU usage. For more information on Unity additive scenes, please refer to the Unity documentation.
In the following steps, you will create a new Woodlands Scene, add the AkBank component into that Scene, and then load it.
In the Project view's Scenes folder, right-click in an empty area in the right-hand pane, then go to Create and select Scene.
Once you select Scene, a newly added Scene file will appear with its name highlighted for you to rename it.
Name it Woodlands.
Usually, you could just open a Scene by double-clicking it in the Project window. However, you need the Woodlands Scene to be loaded along with the 'L3_1 - Loading SoundBanks using Triggers' scene, so you will have to drag it into the Hierarchy, opening it additively.
Drag the Woodlands Scene into the Hierarchy.
When creating an empty scene, you will have a few default game objects included, like a camera and a light. As the L3_1 - Loading SoundBanks Scene is the primary scene, you will be using its Main Camera and Directional Light, and so we will remove those objects from the Woodlands Scene.
Select all game objects in the Woodlands scene, then right-click and select Delete.
Let's make a new game object that we'll use to load the SoundBank.
Right-click on the Woodlands Scene header, then go to GameObject and select Create Empty.
Name it Wwise.
In the 'L3_1 - Loading SoundBanks using Triggers' Scene you'll find a Woodlands Trigger game object (created in the previous section, 'Loading SoundBanks using Triggers'), where you'll find the AkBank component we'll use in the Woodlands Scene.
In the 'L3_1 - Loading SoundBanks using Triggers' scene, select the Woodlands Trigger game object.
In the Inspector you'll find the AkBank component that loads the Woodlands SoundBank.
Click and hold on the AkBank title bar, and drag it onto the Wwise game object in the Woodlands Scene.
This will remove the AkBank (including property settings) from the Woodlands Trigger game object and add it to the Wwise game object.
In the Woodlands scene, select the Wwise game object.
Notice the AkBank component has now been added to the Wwise game object.
Next, you need to change the Load on property. When the Woodlands Trigger is entered, the entire Woodlands Scene will be loaded. As such, you should not load the SoundBank on AkTriggerEnter, but instead on Awake of the Woodlands Scene.
Set the Load On property to Awake only.
The SoundBank should also be unloaded once the Scene is removed from runtime, so let's set the Unload On property to Destroy, which is when the Scene is unloaded and removed.
Set the Unload On property to Destroy only.
You don't need to make any other changes in the Woodlands scene, so you can remove the Scene as it will be loading from the 'L3_1 - Loading SoundBanks using Triggers' Scene. First off, let's save the Scene.
Right-click the Woodlands Scene and select Save Scene.
Once the Scene is saved, the small star next to the Woodlands name will disappear. Next, you should also remove the Scene from the Hierarchy because the Scene will be loaded by the LoadSceneAsyncOnEnter script. If we don't remove it, we would end up with this Scene loaded twice.
Right-click it again and select Remove Scene.
The Woodlands Scene is no longer in the Hierarchy, but you can still find it in the Project tab. You will now use the LoadSceneAsyncOnEnter script to load the Scene dynamically so that it will reappear in the Hierarchy once the Player enters the Woodlands Trigger, thereby loading the Woodlands SoundBanks at the same time. The LoadSceneAsyncOnEnter script is a custom script, which will listen for the AkAudioListener (Main Camera) entering the Trigger before loading a Scene.
Select the Woodlands Trigger game object.
In the Inspector, click Add Component, then search for LoadSceneAsyncOnEnter and select it by double-clicking.
Notice that in the bottom of the LoadSceneAsyncOnEnter you'll find a warning message saying 'WARNING: No Scene name!'.
The script will automatically search through the scenes added to the Build Settings and look for scenes matching the text in this field. As it is an empty string right now, it displays a warning message.
The Build Settings are the settings for making a compiled version of the game. Once you are in Play mode, this is also where the game will look for additional scenes to load. You can only add a Scene to the Build Settings if it is located within the Unity project.
In the Scene to Load property, type in Woodlands.
A new warning has appeared. To load scenes that are not in the Hierarchy in runtime, you need to add them to the build settings, so the Play mode will be aware of them.
In the Unity menu, go to File and select Build Settings.
Drag the Woodlands Scene from the Project view into the Scenes in Build list.
The Scene can be added to any position in the list, as long as it's not the topmost element, which you should keep as the Main Scene if you ever make a compiled version of the game. Make sure the warning is not appearing, else check for spelling mistakes. You've now changed the Woodlands Trigger to load a Scene.
Close the Build Settings window and click Play.
Run to the Woodlands.
Notice that the Woodlands Scene is now loaded in the Hierarchy.
Run out of the Woodlands Trigger, into the Village.
Look in the Hierarchy to observe that the Woodlands Scene is yet again being unloaded. You can now add objects into the Woodlands Scene to make the SoundBank load on Trigger entry with not only sound, but also game objects like trees, the waterfall, and so on.
Press ESC to open the WAG menu and click Play again to exit Play mode.