JoeyL
JoeyL

Reputation: 1305

Creating a sound manager

This is how I currently have my Sound Manager setup for a small RPG and I was just wondering if this was good practice.

What I was trying to go for was to have my two methods (PlayBGMusic and PlaySound) to be "public static" so I would not have to grab the GameObject by tag and the Script in almost all of my other scripts that want to play a sound. If I do make them "public static" then I would have to make my variables the same which takes away from placing anything in the inspector.

public class Sound_Manager : MonoBehaviour {
    // Allow the user to decide if music should be on or off.
    public bool backgroundMusicOn;
    // Allow the user to decide if sound should be on or off.
    public bool soundOn;
    // The music volume.
    [Range(0,1)]
    public float musicVolume;
    // The sound volume.
    [Range(0,1)]
    public float soundVolume;
    // The Background music to be used for any manipulations.
    private AudioSource _bgMusic;

// Play a background song.
public void PlayBGMusic(AudioSource music){
...
}

// Mute Current Background Song.
public void MuteUnMuteBGMusic(){
....
}

// Play a sound.
public void PlaySound(AudioClip sfx, Vector3 location){
...
}
}

Is this a proper way of handling something like this or is there another way that could be more simple?

Upvotes: 0

Views: 11928

Answers (2)

LumbusterTick
LumbusterTick

Reputation: 1075

I recommend turning the sound manager into a singleton class. In that way you can have all the properties in the inspector and use the singleton object to globally access all your properties, even destroying the gameobject completely when not needed .

Upvotes: 1

ren boxue
ren boxue

Reputation: 31

I think you can't play BGmusic and sound in one GameObject,because every audio must play whith single audion source,this is my code of AudioManager.

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

public class AudioManager : MonoBehaviour
{
    public AudioClip[] audioSources;
    public GameObject audioPrefabSource;
    public Dictionary<string,AudioClip> audioClips;
    static GameObject audioPrefab;
    static GameObject instance;
    static AudioSource musicPlayer;
    public static AudioManager audioManager;
    Dictionary<string,Audio> aliveSounds;
    AudioListener al;

    void Awake ()
    {
            audioManager = this;
            al = GetComponent<AudioListener> ();
            audioClips = new Dictionary<string, AudioClip> ();
            foreach (AudioClip a in audioSources) {
                    audioClips.Add (a.name, a);
            }

            instance = this.gameObject;
            audioPrefab = audioPrefabSource;
            musicPlayer = audio;
            aliveSounds = new Dictionary<string, Audio> ();
            //DontDestroyOnLoad(gameObject);
    }

    void Update ()
    {
            if (!GameSetting.hasMusic) {
                    musicPlayer.Pause ();
            } else {
                    if (!musicPlayer.isPlaying) {
                            musicPlayer.Play ();
                    }
            }
            if (!GameSetting.hasSound && aliveSounds.Count > 0) {
                    foreach (Audio a in aliveSounds.Values) {
                            a.StopSound ();
                    }
                    aliveSounds.Clear ();
            }
            if (!al.enabled) {
                    al.enabled = true;
            }
    }

    public static void PlaySoundOnce (string name)
    {
            if (!GameSetting.hasSound) {
                    return;
            }
            if (!audioManager.audioClips.ContainsKey (name)) {
                    return;
            }
            GameObject go = GameObject.Instantiate (audioPrefab) as GameObject;
            go.transform.parent = instance.transform;
            Audio a = go.GetComponent<Audio> ();
            a.PlaySoundOnce (audioManager.audioClips [name]);
    }


    public static void PlayMusic (string name)
    {
            if (!GameSetting.hasMusic) {
                    return;
            }

            if (musicPlayer.clip == null || musicPlayer.clip.name != name) {
                    //      musicPlayer.clip = audioManager.audioClips [name];
                    musicPlayer.clip = Resources.Load ("Audio/" + name, typeof(AudioClip)) as AudioClip;
                    musicPlayer.Stop ();
                    musicPlayer.loop = true;
                    musicPlayer.Play ();
            } else {
                    musicPlayer.loop = true;
                    musicPlayer.Play ();
            }

    }
}

There is another class for Audio,it must be made to a audioprefab.

using UnityEngine;
using System.Collections;

public class Audio : MonoBehaviour
{
    public void PlaySoundOnce (AudioClip audioClip)
    {
            StartCoroutine (PlaySoundCoroutine (audioClip));
    }

    IEnumerator PlaySoundCoroutine (AudioClip audioClip)
    {
            audio.PlayOneShot (audioClip);
            yield return new WaitForSeconds (audioClip.length);
            Destroy (gameObject);
    }

    public void PlaySoundLoop (AudioClip audioClip)
    {
            audio.clip = audioClip;
            audio.loop = true;
            audio.Play ();
    }

    public void StopSound ()
    {
            audio.Stop ();
            Destroy (gameObject);
    }
}

Like this,and I ignored audio's position,I used 2D sound.

enter image description here

Upvotes: 2

Related Questions