- Unity 2018 Cookbook(Third Edition)
- Matt Smith
- 844字
- 2025-02-23 18:56:39
How to do it...
To add volume control sliders to your scene, follow these steps:
- Create a new Unity 3D project.
- Import the provided Unity package, Volume, into your project.
- Open the Volume scene from Project panel folder, VolumeControl | _Scenes.
- Play the scene and walk toward the semitransparent green wall in the tunnel, using the W A S D keys (press Shift to run). You will be able to listen to:
- A looping music soundtrack
- Bells ringing
- A robotic speech whenever the character collides with the wall
- In the Project panel, use the Create menu to add an Audio Mixer file. Rename this new file MainMixer.
- Double-click MainMixer to open the Audio Mixer panel.
- In the the Audio Mixer panel Groups section, highlight Master and click the + (plus) sign to add a child to the Master group. Name this child Music. Repeat these actions to add a second child of Master named FX:

- In the the Audio Mixer panel Mixers section, highlight MainMixer and click the + (plus) sign to add a new item to the Mixers group. Rename this MusicMixer (you may need to rename this via the Project panel, since you've created a new Audio Mixer file through this process).
- Drag MusicMixer onto MainMixer (child it), and select Music as its output in the pop-up dialog window.
- In the the Audio Mixer panel Mixers section, highlight MainMixer and click the + (plus) sign to add a new item to the Mixers group. Name this FxMixer.
- Child (drag) FxMixer onto MainMixer, and select Fx as its output in the pop-up dialog window.
- Select MusicMixer. Select its Master group and add a child named Soundtrack.
- Select FxMixer and add two children to its Master group: one named Speech, and another named Bells:

- Select the DialogueTrigger GameObject in the Hierarchy. In the Inspector, change its Audio Source component Output track to FxMixer | Speech:

- Select the Soundtrack GameObject in the Hierarchy. In the Inspector, change its Audio Source component Output track to MusicMixer | Soundtrack.
- Select Signal from the Prefabs folder in the Project panel. In the Inspector, change its Audio Source component Output track to FxMixer | Bells.
- Select MainMixer in the Audio Mixer panel, and select its Master track. In the Inspector panel, right-click on Volume in the Attenuation component. From the pop-up-context menu, select Expose Volume (of Master) to script:

- Repeat the operation to expose the Volume to scripts for both the Music and FX Groups.
- At the top-right of the Audio Mixer panel, you should see Exposed Parameters (three). Click the drop-down icon, and rename them as follows: MyExposedParam to OverallVolume; MyExposedParam1 to MusicVolume, and MyExposedParam2 to FxVolume. Note the sequence of the three parameters may not match the order you added them, so double-check that the greyed-out track names on the right correspond correctly:

- In the Hierarchy panel, use the Create drop-down menu to add a UI Panel to the scene (menu: Create | UI | Panel). Unity will automatically add a Canvas parent for this panel.
- In the Hierarchy panel, create a UI Slider to the scene (menu: Create | UI | Slider). Make it a child of the Panel object. Rename this slider as Slider-overall. Set the slider's Min Value to 0.000025 (or 2.5e-05).
- Duplicate it and rename the new copy to Slider-music. In the Inspector panel, Rect Transform component, change its Pos Y parameter to -40.
- Duplicate Slider-music and rename the new copy to Slider-fx. Change its Pos Y parameter to -70:

- Create a C# script class, VolumeControl, in the _Scripts folder, containing the following code, and add an instance as a scripted component to the Main Camera GameObject:
using UnityEngine; using UnityEngine.Audio; public class VolumeControl : MonoBehaviour { public GameObject panel; public AudioMixer myMixer; private bool isPaused = false; void Start(){ panel.SetActive(false); ON_CHANGE_OverallVol(0.01F); ON_CHANGE_MusicVol(0.01F); ON_CHANGE_FxVol(0.01F); } void Update() { if (Input.GetKeyUp (KeyCode.Escape)) { panel.SetActive(!panel.activeInHierarchy); if(isPaused) Time.timeScale = 1.0f; else Time.timeScale = 0.0f; isPaused = !isPaused; } } public void ON_CHANGE_OverallVol(float vol) { myMixer.SetFloat("OverallVolume", Mathf.Log10(vol) * 20f); } public void ON_CHANGE_MusicVol(float vol) { myMixer.SetFloat ("MusicVolume", Mathf.Log10(vol) * 20f); } public void ON_CHANGE_FxVol(float vol) { myMixer.SetFloat ("FxVolume", Mathf.Log10(vol) * 20f); } }
- With Main Camera selected in the Hierarchy panel, drag the Panel GameObject into the Inspector for the public Panel variable.
- With Main Camera selected in the Hierarchy panel, drag MainMixer from the Project panel into the Inspector for the public My Mixer variable.
- Select the OverallSlider component. Below the On Value Changed list, click the + sign to add an action. From the Hierarchy panel, drag Main Camera into the Object slot and using the drop-down menu, choose VolumeControl | ON_CHANGE_OverallVol option. For testing purposes, change the appropriate selector from Runtime Only to Editor and Runtime.
- Repeat the previous step with MusicSlider and FxSlider, but this time, choose the ON_CHANGE_MusicVol and ON_CHANGE_FxVol options, respectively, from the drop-down menu.
- Play the scene. You will be able to access the sliders when pressing ESCAPE on your keyboard and adjust volume settings from there.