unity声音管理

unity声音管理

using UnityEngine;
using System.Collections;


public class AudioManager : MonoBehaviour
{
    private static AudioManager mInstance;

    private AudioSource mAudioSource;
    private AudioClip mAudioClip;
    private string mSoundName;


    public static AudioManager Instance
    {
        get
        {
            if (mInstance == null)
            {
                GameObject obj = new GameObject("_AudioManager");
                mInstance = obj.AddComponent(typeof(AudioManager)) as AudioManager;
            }

            return mInstance;
        }
    }


    void Awake()
    {
        mAudioSource = this.gameObject.AddComponent(typeof(AudioSource)) as AudioSource;
        DontDestroyOnLoad(this.gameObject);
    }


    public void Play()
    {
        PlaySound();
    }

    public void Play(string fileName)
    {
        Play(fileName, false);
    }

    public void Play(string fileName, bool loop)
    {
        if (!fileName.Equals(mSoundName))
        {
            mSoundName = fileName;
            PlaySound(loop);
        }
    }


    public void Play(AudioClip clip)
    {
        Play(clip, false);
    }

    public void Play(AudioClip clip, bool loop)
    {
        mAudioSource.clip = clip;
        mAudioSource.loop = loop;
        mAudioSource.Play();
    }


    public void Stop()
    {
        mAudioSource.Stop();
    }


    public AudioSource Play(AudioClip clip, Transform emitter)
    {
        return Play(clip, emitter, 1f, 1f, false);
    }

    public AudioSource Play(AudioClip clip, Transform emitter, float volume)
    {
        return Play(clip, emitter, volume, 1f, false);
    }

    public AudioSource Play(AudioClip clip, Transform emitter, float volume, float pitch)
    {
        return Play(clip, emitter, volume, pitch, false);
    }

    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, float pitch, bool loop)
    {
        GameObject go = new GameObject("Audio_" + clip.name);
        go.transform.position = emitter.position;
        go.transform.parent = emitter;

        AudioSource source = go.AddComponent();
        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)
    {
        return Play(clip, point, 1f, 1f, false);
    }

    public AudioSource Play(AudioClip clip, Vector3 point, float volume)
    {
        return Play(clip, point, volume, 1f, false); 
    }

    public AudioSource Play(AudioClip clip, Vector3 point, float volume, float pitch)
    {
        return Play(clip, point, volume, pitch, false);
    }

    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, float pitch, bool loop)
    {
        GameObject go = new GameObject("Audio_" + clip.name);
        go.transform.position = point;

        AudioSource source = go.AddComponent();
        source.clip = clip;
        source.volume = volume;
        source.pitch = pitch;
        source.loop = loop;

        if (!loop)
        {
            Destroy(go, clip.length);
        }

        return source;
    }


    private void PlaySound()
    {
        if (mSoundName != "")
        {
            mAudioSource.Play();
        }
    }

    private void PlaySound(bool loop)
    {
        if (mSoundName != "")
        {
            mAudioClip = Resources.Load("Audio/" + mSoundName) as AudioClip;
            mAudioSource.clip = mAudioClip;
            mAudioSource.loop = loop;
            mAudioSource.Play();
        }
    }
}

你可能感兴趣的:(unity)