πŸ”ŠAudio Manager

Used to play sounds/music

It's ready for one shot sound Effects, Music theme switcher.

How to use

add using Game.Audio;

In Assets_Main\Prefabs\Common\Audio you can find SoundPool and SoundNoPool prefabs. In _Loader scene by default 5 SoundPool object will be instantiated in PoolSystem for future usage.

Select AudioClip files in project and right-click -> Create -> Sound Audio Clip Selected. It will create sound configs. See image below:

Example how to create SoundAudioClip form AudioClip assets

And result:

Created SoundAudioClips result

You should name your audio file with end string like "_sfx" (for sound effects) or "_music" or "_m" (for music). So configs will be automaticaly linked to correspond mixer group. If there is no end strings found then config will be in Sounds (SFX) mixer group.

Also you can create empty sound audio clip scriptable object by create asset menu: Create -> Game -> Audio -> Sound Audio Clip.

Prepare your SoundAudioClip in inspector

You need to set bool for 2D sound. If it's false -> it will be 3D. AudioClip -> what sound to play. MixerGroup -> on what channel we should play AudioClip (by default in Mixer we have Music and Sound groups) Volume -> if you sound too loud, then change value less then 1.

How SoundAudioClip looks in inspector

Using sounds

Reference Sound Audio Clip scriptable object in your controller scripts. See below is example:

// Example how to call sound play
using Game.Audio;

public class Weapon : Monobehaviour {
    // used to spawn shoot obj, we can use it for 3d sound too;
    [SerializedField] private Transform _shootPoint;

    // declare sound clip info, note that this is Scriptable object,
    // you can create instance of it by Create/Game/Audio/Sound Audio Clip;
    [SerializedField] private SoundAudioClip _shootSound;
    
    public void Shoot() {
        // some logic
        
        // if 2D sound                                 // here is pitch random
        AudioManager.Instance.PlaySingleShot(_shootSound, Random.Range(0.9f, 1.1f));
        // if you want callback on sound end
        AudioManager.Instance.PlaySingleShot(_shootSound,
                                             onPlayed: () => print("soundEnd"),
                                             Random.Range(0.9f, 1.1f));
        
        
        
        
        // if 3D sound (no need to call this for 2D, it's empty overhead)
        AudioManager.Instance.PlaySingleShotAt(_shootSound, 
                                               _shootPoint.position,
                                               Random.Range(0.9f, 1.1f));
        // with on sound end callback
        AudioManager.Instance.PlaySingleShotAt(_shootSound, 
                                               _shootPoint.position,
                                               onPlayed: () => print("soundEnd"),
                                               Random.Range(0.9f, 1.1f));
    }
}

You can also use sounds not from prefab using SoundNoPool prefab. Replace it in scene or other prefab and use PlaySingleShot(Sound soundObj) method in AudioManager. You need to reference SoundNoPool obj from scene in your script where you want to call AudioManager. If you use this method (sound will not overlap on itself if you try to play it again before old one finishes) but the easiest way is like script example above (also sounds will overlap each other with pool system in use, so sounds will not cut if you try to use the same sound again).

Other scripts

RepeatSound2D

You can use RepeatSound2D as field in controller scripts. Use it's Play() and Stop() methods. Usefull for sounds that needs to continiously play while player touches screen or similar cases.

CompoundSound

You can use CompoundSound as field in controller scripts. It contains several sounds that will be played on Play() call and you can stop by Stop(). Usefull if you want to play multiple sounds with delay and random pitch for each.

More examples in project in Assets\_Examples\Audio System\

Last updated