一、问题

原先写了一个播放背景音乐的类

存在的问题是,只能播放单一的背景音乐,没法播放多个背景音乐。

二、搜索

关键词“unity3d multi audio manager”

http://www.blog.silentkraken.com/2010/04/06/audiomanager/

 

三、代码

using UnityEngine;
using System.Collections;

/// <summary>
/// Filename: AudioManager.cs
/// Description: Manager of ground music
/// Author: Star
/// Date: [12/4/31]
/// </summary>
public class AudioManager : MonoBehaviour {
    
    private static AudioManager instance = null;
    
    public AudioSource m_AudioMgr;
        
    private AudioClip m_PlayClip;
    private string m_CurMusicName = "";
    
    public static AudioManager Instance
    {
        get
        { 
            return instance;
        }
    }
    
    void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            instance = this;
        }
        
        DontDestroyOnLoad(this.gameObject);
    }
    
    /// <summary>
    /// Play the background music which will go through the scene.
    /// </summary>
    /// <param name='fileName'>
    /// File name.
    /// </param>
    public void  PlayBG(string fileName)
    {
        if (!fileName.Equals(m_CurMusicName))
        {
            m_PlayClip = Resources.Load("Audio/Source/BGMusic/"+fileName) as AudioClip;
            m_AudioMgr.clip = m_PlayClip;
            m_AudioMgr.Play();
            m_CurMusicName = fileName;
        }
        
        Debug.Log("AudioManager::Play()");
    }
    
    /// <summary>
    /// Play the background music which will go through the scene.
    /// </summary>
    /// <param name='m_PlayClip'>
    /// M_ play clip.
    /// </param>
    public void PlayBG(AudioClip m_PlayClip)
    {
        m_AudioMgr.clip = m_PlayClip;
        m_AudioMgr.Play();
    }
    
    /// <summary>
    /// Stops the background music.
    /// </summary>
    public void StopBG()
    {
        m_AudioMgr.Stop();
        m_CurMusicName = "";
        Debug.Log("Stop bm_PlayClipkground music");
    }
    
    public AudioSource Play(AudioClip clip, Transform emitter, bool loop)
    {
        return Play(clip, emitter, 1f, 1f, loop);
    }
    
    public AudioSource Play(AudioClip clip, Transform emitter, float volume, bool loop)
    {
        return Play(clip, emitter, volume, 1f, loop);
    }
    
    /// <summary>
    /// Plays a sound by creating an empty game object with an AudioSource
    /// and attaching it to the given transform (so it moves with the transform). 
    /// Destroys it after it finished playing if it dosen't loop.
    /// </summary>
    /// <param name='clip'>
    /// Clip.
    /// </param>
    /// <param name='emitter'>
    /// Emitter.
    /// </param>
    /// <param name='volume'>
    /// Volume.
    /// </param>
    /// <param name='pitch'>
    /// Pitch.
    /// </param>
    /// <param name='loop'>
    /// Loop.
    /// </param>
    public AudioSource Play(AudioClip clip, Transform emitter, float volume, float pitch, bool loop)
    {
        GameObject go = new GameObject("Audio:"+clip.name);
        go.transform.position = emitter.position;
        go.transform.parent = emitter;
        
        // create the source
        AudioSource source = go.AddComponent<AudioSource>();
        source.clip = clip;
        source.volume = volume;
        source.pitch = pitch;
        source.loop = loop;
        
        if (!loop)
        {
            Destroy(go, clip.length);
        }
        
        return source;
    }
    
    public AudioSource Play(AudioClip clip, Vector3 point, bool loop)
    {
        return Play(clip, point, 1f, 1f, loop);
    }
    
    public AudioSource Play(AudioClip clip, Vector3 point, float volume, bool loop)
    {
        return Play(clip, point, volume, 1f, loop);
    }
    
    /// <summary>
    /// Plays a sound at the given point in space by creating an empty game object with an AudioSource
    /// in that place and destroys it after it finished playing if it dosen't loop.
    /// </summary>
    /// <param name='clip'>
    /// Clip.
    /// </param>
    /// <param name='point'>
    /// Point.
    /// </param>
    /// <param name='volume'>
    /// Volume.
    /// </param>
    /// <param name='pitch'>
    /// Pitch.
    /// </param>
    /// <param name='loop'>
    /// Loop.
    /// </param>
    public AudioSource Play(AudioClip clip, Vector3 point, float volume, float pitch, bool loop)
    {
        GameObject go = new GameObject("Audio:" + clip.name);
        go.transform.position = point;
        
        AudioSource source = go.AddComponent<AudioSource>();
        source.clip = clip;
        source.volume = volume;
        source.pitch = pitch;
        source.loop = loop;
        
        if (!loop)
        {
            DestroyObject(go, clip.length);
        }
        
        return source;
    }
}

 

四、总结

1.不足:无法播放多个跨场景的背景音乐(有些背景音乐可能是合成音乐),这个用类似的动态创建不删除的物体声音源也是可能解决的,只是我们现在不需要。