Unity Advanced Study Notes: Sound Manager

Before creating the sound manager, we first create an empty class Manager to save all the manager singletons, so that when the manager is used later, just call the Manager class directly. Every time a manager class is created, it must be registered in the Manager class

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Manager
{
    
    
    public static AudioManager m_Audio = AudioManager.Instance;
}

Sound Manager complete code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioManager : ManagerBase<AudioManager>
{
    
    

    // environment sound
    private AudioSource envPlayer;
    // sound effect
    private AudioSource sePlayer;
    // music
    private AudioSource Player;

    // Start is called before the first frame update
    void Start()
    {
    
    
        envPlayer = gameObject.AddComponent<AudioSource>();
        sePlayer = gameObject.AddComponent<AudioSource>();
        Player = gameObject.AddComponent<AudioSource>();
        // do not destroy audio manager when switching scene
        GameObject.DontDestroyOnLoad(gameObject);
    }

    // play music
    public void PlayMusic(string name, float volume = 1) {
    
    
        AudioClip clip = Resources.Load<AudioClip>(name);
        PlayMusic(clip, volume);
    }

    public void PlayMusic(AudioClip clip, float volume = 1) {
    
    
        Player.volume = volume;
        Player.clip = clip;
        if (Player.isPlaying) {
    
    
            Player.Stop();
        }
        Player.Play();
    }

    public void StopMusic() {
    
    
        Player.Stop();
    }

    public void ChangeMusicVolume(float volume) {
    
    
        Player.volume = volume;
    }


    // play environment sound
    public void PlayEnvMusic(string name, float volume = 1) {
    
    
        AudioClip clip = Resources.Load<AudioClip>(name);
        PlayEnvMusic(clip, volume);
    }

    public void PlayEnvMusic(AudioClip clip, float volume = 1) {
    
    
        envPlayer.volume = volume;
        envPlayer.clip = clip;
        if (envPlayer.isPlaying) {
    
    
            envPlayer.Stop();
        }
        envPlayer.Play();
    }

    public void StopEnvMusic() {
    
    
        envPlayer.Stop();
    }

    public void ChangeEnvMusicVolume(float volume) {
    
    
        envPlayer.volume = volume;
    }


    // play sound effect
    public void PlaySeSound(string name, float volume = 1) {
    
    
        AudioClip clip = Resources.Load<AudioClip>(name);
        PlaySeSound(clip, volume);
    }

    public void PlaySeSound(AudioClip clip, float volume = 1) {
    
    
        sePlayer.PlayOneShot(clip, volume);
    }

    // play sound effect on an object
    public void PlaySeSoundOnObject(string name, GameObject go, float volume = 1) {
    
    
        AudioClip clip = Resources.Load<AudioClip>(name);
        PlaySeSoundOnObject(clip, go, volume);
    }

    public void PlaySeSoundOnObject(AudioClip clip, GameObject go, float volume = 1) {
    
    
        AudioSource player = go.GetComponent<AudioSource>();
        if (player == null) {
    
    
            player = go.AddComponent<AudioSource>();
        }
        player.volume = volume;
        player.PlayOneShot(clip);
    }

    public override byte GetMessageType() {
    
    
        return MessageType.Type_Audio;
    }
}

Code Interpretation:
1

    // environment sound
    private AudioSource envPlayer;
    // sound effect
    private AudioSource sePlayer;
    // music
    private AudioSource Player;

    // Start is called before the first frame update
    void Start()
    {
    
    
        envPlayer = gameObject.AddComponent<AudioSource>();
        sePlayer = gameObject.AddComponent<AudioSource>();
        Player = gameObject.AddComponent<AudioSource>();
        // do not destroy audio manager when switching scene
        GameObject.DontDestroyOnLoad(gameObject);
    }

Our sound manager sets up three players, envPlayer, sePlayer, and Player, which are used to manage game environment music, sound effects, and background music respectively. Initialize the three managers in the Start method, so that we don't need to mount the AudioSource component on the game object. At the same time, we hope that the sound manager will not be deleted when switching game scenes, use GameObject.DontDestroyOnLoad(gameObject); to achieve

2

    // play music
    public void PlayMusic(string name, float volume = 1) {
    
    
        AudioClip clip = Resources.Load<AudioClip>(name);
        PlayMusic(clip, volume);
    }

    public void PlayMusic(AudioClip clip, float volume = 1) {
    
    
        Player.volume = volume;
        Player.clip = clip;
        if (Player.isPlaying) {
    
    
            Player.Stop();
        }
        Player.Play();
    }

    public void StopMusic() {
    
    
        Player.Stop();
    }

    public void ChangeMusicVolume(float volume) {
    
    
        Player.volume = volume;
    }

In the method of playing music, PlayMusic, we consider two situations: get the AudioClip object directly, or get the music name and find the corresponding AudioClip under the Resources folder.

If we get the AudioClip directly, we can judge whether the player is playing other music after setting the volume and clip. If so, first close the music that is playing, and then start playing a new clip. Of course, here we can also change the playback logic according to specific game needs

If the music name is passed in, load the music from Resources, and then call the second PlayMusic method

At the same time, we set the method of turning off the music and changing the volume

The setting of the ambient sound is exactly the same as that of the background music, so I won’t repeat it here

    // play sound effect
    public void PlaySeSound(string name, float volume = 1) {
    
    
        AudioClip clip = Resources.Load<AudioClip>(name);
        PlaySeSound(clip, volume);
    }

    public void PlaySeSound(AudioClip clip, float volume = 1) {
    
    
        sePlayer.PlayOneShot(clip, volume);
    }

    // play sound effect on an object
    public void PlaySeSoundOnObject(string name, GameObject go, float volume = 1) {
    
    
        AudioClip clip = Resources.Load<AudioClip>(name);
        PlaySeSoundOnObject(clip, go, volume);
    }

    public void PlaySeSoundOnObject(AudioClip clip, GameObject go, float volume = 1) {
    
    
        AudioSource player = go.GetComponent<AudioSource>();
        if (player == null) {
    
    
            player = go.AddComponent<AudioSource>();
        }
        player.volume = volume;
        player.PlayOneShot(clip);
    }

For sound settings, we additionally added the method PlaySeSoundOnObject. This method takes into account that in 3D games, sometimes the sound source is not directly on the player, but on a game object in the distance, and the sound will decrease with distance. The PlaySeSoundOnObject method passes in the game object to be used as the sound source, and judges whether the object has an AudioSource. If it does not exist, add the component, and then let the AudioSource on the object play the sound effect

Guess you like

Origin blog.csdn.net/Raine_Yang/article/details/130657625