Game Development Reference
In-Depth Information
8.
Select the Level Manager in the Hierarchy view, and adjust the sliders in
the viewport.
9.
Resize the views to force an update so you can see the changed values in
the Inspector.
10.
Stop Play mode.
The implementations of the two settings are a bit different. Ambient volume should carry through to
all, menu and garden levels alike.
Adjusting the Ambient Sound Volume
Because a game could easily have its audio categorized as ambient, voice, and special effects,
you will create a tag to identify the audio source components that must be adjusted. Each time the
game enters a new level, the components are located, put into an array, and then processed with
the new value.
1.
Open the SettingsMenu scene.
2.
Create a new tag, and name it Ambient .
3.
Select the Settings Menu in the Hierarchy view.
It is the object that contains the Audio source for the ambient sounds.
4.
Set its tag to the new Ambient tag.
5.
Save the scene.
With the new tag in place, you can add the scripting that will make use of it.
In the LevelManager script, at the top of the ManageLevels function, add
6.
ProcessAudio(); // adjust the ambient volume no matter what the level is
Create the ProcessAudio() function:
7.
void ProcessAudio() {
GameObject[] ambientTags = GameObject.FindGameObjectsWithTag("Ambient");
foreach (GameObject ambientTag in ambientTags) {
ambientTag.audio.volume = ambientVolume;
}
}
In this function, you are finding all gameObjects in the current level with the "Ambient" tag and
putting them into an array, and then iterating through the array and changing the volume setting on
each. You aren't performing a check to make sure the object has an Audio Source component first,
so it is up to you to check for one when you change the object's tag. A more likely scenario is that
you will forget to change the tag on an object.
1.
Save the script.
 
Search WWH ::




Custom Search