Game Framework provides simplified audio support for your game including setup, volume controls and ease of playback.
There is support in the framework for managing background music and triggering audio effects. The core of this is the GameManager component that maintains references to AudioSources (one for background music and others for sound effects). To set this up, you simply need to add at least 2 AudioSource components to the same gameobject that contains the GameManager compontnt. The first AudioSource is used for playing background music and subsequent ones audio effects. If desired you should set the AudioClip for the background audio source and set Play On Awake and Loop as suited. GameManager will automatically detect and use these AudioSources. You can use the following properties and methods to access and reference these:
Any changes to the volume levels will automatically be saved across game restarts. In game volume changes will cause BackgroundVolumeChangedMessage or EffectVolumeChangedMessage to be sent out via the messaging system. This allows any components that you write to easily and automatically react to volume changes.
Audio Settings In Game
Aside from allowing you to configure volumes through code, Game Framework provides an extendable in game settings window that allows players to control the desired volume along as other settings. The Getting Started Tutorial runs through how you can easily add this to your game.
While you can access the audiosources directly on GameManager as shown above, there are also helper methods and components for controlling the audio.
The GameManager PlayEffect method can be used for easily playing an audio effect in accordance with current volume settings etc.
public void PlayEffect(AudioClip clip, float pitchLow = 1, float pitchHigh = 1)
The StartStopBackgroundMusic component can be used to provide actions for the background music so that it can be automatically started / stopped in the components Enable / Disable states. This could for example be used to easily play title music when entering the title screen, and stopping it when you start the game
If you have seperate AudioSources for other background or audio effects then you can add the CopyBackgroundVolume and CopyGlobalEffectVolume components respectivley to the same gameobject as the AudioSource to have the global effect volume automatically set on the AudioSource.
See also :
- Getting Started with Game Framework for Unity – Part1 – The Basics, Audio, Settings and Localisation
- API Documentation
If you have any thoughts or suggestions on improvements or additions to the Audio features then please let us know!