unity声音的管理

a:声音audioClip加载方式:1:直接拖到场景中  2:resource.load()  3:网络加载

b:播放:AudioSource.play

c:播放方式: 1:背景音乐,一直循环播放 2:音效,播放一次或者循环播放,条件截止

d: 格式 :wav音效——————加载 解压缩    mp3背景----------------------加载 内存   [ 待验证 ]


using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
public enum EFFECT_SOUND_NAME
{
    ZHU_JIEMIAN_BG = 0,
    QIE_SUOLIAN = 1,
}
public enum BG_SOUND_NAME
{
    MAIN = 0,
    BASE_MODE,
}
public class TsoundManager : MonoBehaviour {
    // Use this for initialization
    public static TsoundManager Instance;


    public AudioSource Bg_adudioSource;
    public AudioSource Eff_audioSource;


    private Dictionary _audioDict = new Dictionary();
    void Awake()
    {
        Instance = this;
        DontDestroyOnLoad(this.gameObject);
    }


    private AudioClip getBgSound(int soundId)
    {
        return Resources.Load("soundBg/" + soundId, typeof(AudioClip)) as AudioClip;
    }


    private AudioClip getEffSound(int soundId)
    {
        return Resources.Load("soundEff/" + soundId, typeof(AudioClip)) as AudioClip;
    }


    //
    public void playBgSource(BG_SOUND_NAME soundName,float volume)
    {
        AudioClip audio = getBgSound((int)soundName);
        if(audio!=null)
        {
            Bg_adudioSource.clip = audio;
            Bg_adudioSource.Play();
        }
    }


    //特效
    public void playSFX(EFFECT_SOUND_NAME soundName)
    {
        int soundId = (int)soundName;


        AudioClip audio;
        if (_audioDict.ContainsKey(soundId))
        {
            audio = _audioDict[soundId];
        }
        else
        {
            audio = getEffSound((int)soundName);


            _audioDict.Add(soundId, audio);
        }
        if (audio)
        {
            AudioSource.PlayClipAtPoint(audio, Vector3.zero);
        }
    }


    //
    public void playAudioByLocalSource(EFFECT_SOUND_NAME soundName,float  volum = 1f, bool loop = false)
    {
        int soundId = (int)soundName;
        AudioClip audio;
        if (_audioDict.ContainsKey(soundId))
        {
            audio = _audioDict[soundId];
        }
        else
        {
            audio = getEffSound((int)soundName);


            _audioDict.Add(soundId, audio);
        }
        if (audio)
        {
            Eff_audioSource.clip = audio;
            if (!Eff_audioSource.isPlaying)
                Eff_audioSource.Play();
            Eff_audioSource.loop = loop;
            Eff_audioSource.volume = volum;
        }
    }


    public void stopLocalSource()
    {
        if (Eff_audioSource.isPlaying)
            Eff_audioSource.Stop();
    }


}



参考:http://blog.csdn.net/u014761712/article/details/61196599

你可能感兴趣的:(unity)