The Unity Audio Manager is an easy-to-use system for managing music and sound effects (SFX) in your Unity games. With this tool, you can control volume levels for music and SFX separately, use object pooling to improve performance, and create smooth transitions between music tracks with fade effects. It’s designed to be simple to integrate into any project, saving you time and hassle.
- Singleton Pattern: Ensures only one instance of the Audio Manager exists across scenes.
- Music and SFX Control: Allows separate volume adjustments for background music and sound effects.
- Object Pooling: Efficiently manages audio objects to prevent unnecessary instantiation, improving performance.
- Music Fading: Supports smooth fading between different music tracks.
- Persistent Volume Settings: Volume preferences are saved using
PlayerPrefsand automatically restored on game startup.
- Create an empty GameObject in your scene and attach the
AudioObjectscript to it. - Assign an
AudioSourceto this GameObject and set theAudioType(either Music or SFX). - Make this GameObject a Prefab to reuse in the object pool.
- The volume of this
AudioObjectwill be automatically adjusted by the Audio Manager based on user settings.
- Create another empty GameObject in your scene and attach the
ObjectPoolManagerscript. - Assign the
AudioObjectprefab to the Object Pool Manager and configure the pool size as needed. - This setup ensures efficient sound management by reusing audio objects rather than creating and destroying them each time.
- Create an empty GameObject and attach the
AudioManagerscript. - Assign the
ObjectPoolManagerto the Audio Manager. - Optionally, connect UI sliders for music and SFX control to the Audio Manager to allow dynamic adjustments of volume in the game.
The Audio Manager allows players to adjust music and SFX volume using in-game UI sliders. These values are automatically saved using PlayerPrefs and will be restored when the game is restarted.
To play a sound effect, simply call the PlayAudio method from the Audio Manager, specifying the audio clip and position:
AudioManager.Instance.PlayAudio(sfxClip, 1.0f, position, AudioObject.AudioType.SFX);To play background music, use the PlayMusic method:
AudioManager.Instance.PlayMusic(musicClip);The Audio Manager supports smooth transitions between background music tracks using fade in/out effects. When a new music track is played, the current one fades out, and the new track fades in:
AudioManager.Instance.PlayMusic(newMusicClip);- The
ObjectPoolManagerallows you to configure the size of the pool and the prefab used for pooling audio objects. You can adjust these settings based on the needs of your game. - Increasing the pool size may be useful for games with frequent sound effects to prevent object creation during gameplay.
This project is licensed under the MIT License. See the LICENSE file for more details.