1、分为三大类:背景音、音效、对话音
2、将所有声音放到某文件目录下,本文以Resources为例
3、原理是三大类声音中的每个都有对应的AudioSources组件,当本类内切换声音时会自动停止上一个声音片段的播放。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Util
{
public enum AudioType { bgm, effect, speek }
public class AudioPlayer : MonoBehaviour
{
public static string SpeechStart = "SpeechStart";
public static string SpeechEnd = "SpeechEnd";
public AudioSource bgm;
public AudioSource effect;
public AudioSource speek;
private Coroutine speechCoroutine = null;
public AudioClip[] sounds;
public bool speechIsPlaying = true;
private void Awake()
{
bgm = gameObject.AddComponent<AudioSource>();
bgm.loop = true;
bgm.volume = 0.2f;
effect = gameObject.AddComponent<AudioSource>();
effect.loop = false;
speek = gameObject.AddComponent<AudioSource>();
speek.loop = false;
speek.volume = MainVarController.Instance.startSpeekVolum;
//sounds = new List<AudioClip>();
sounds= Resources.LoadAll<AudioClip>("AudioClips");
// PlayAudio(AudioType.bgm, 16); //-------------播放背景音乐
}
private void OnEnable()
{
ManagerEvent.Register(SpeechEnd, SpeechEndHandler);
}
private void OnDisable()
{
ManagerEvent.Unregister(SpeechEnd, SpeechEndHandler);
}
private void SpeechEndHandler(params object[] args)
{
speechIsPlaying = false;
//Debug.LogError("speechIsPlaying222 " + speechIsPlaying);
}
public void PlayAudio(AudioType type, int index)
{
//Debug.LogError("speechIsPlaying--- " + type + " ====== " + index);
switch (type)
{
case AudioType.bgm:
case AudioType.effect:
AudioClip clip = GetSound(index);
AudioSource source = GetAudioSource(type);
if (clip != null && source != null)
{
if (source.isPlaying && source.clip.name.Equals(clip.name))
{
return;
}
source.clip = clip;
source.Play();
}
break;
case AudioType.speek:
StopSpeech();
speechIsPlaying = true;
//Debug.LogError("speechIsPlaying000 " + speechIsPlaying);
StopAllCoroutines();
speechCoroutine = StartCoroutine(SpeechPlay(index));
break;
}
}
/// <summary>
/// 音效类声音播放
/// </summary>
/// <param name="index">音频资源 序号</param>
/// <param name="audioSpace">空间设置(2d:0 , 3d:1 , 其他为混合音效)</param>
public void EffectPlay(int index, float audioSpace)
{
PlayAudio(AudioType.effect,index);
effect.spatialBlend = audioSpace;
}
public IEnumerator SpeechPlay(int index, System.Action onPlayFinish = null)
{
speechIsPlaying = true;
AudioClip clip = GetSound(index);
AudioSource source = GetAudioSource(AudioType.speek);
if (clip != null && source != null)
{
source.clip = clip;
source.Play();
ManagerEvent.Send(SpeechStart, index);
while (source.isPlaying)
{
yield return null;
}
StopSpeech();
if (onPlayFinish != null)
onPlayFinish();
//Debug.LogError("speechIsPlaying111 " + speechIsPlaying);
ManagerEvent.Send(SpeechEnd, index);
}
}
public void StopSpeech()
{
if (speechCoroutine != null)
{
AudioSource source = GetAudioSource(AudioType.speek);
source.Stop();
StopCoroutine(speechCoroutine);
speechCoroutine = null;
}
}
public void StopAudio(AudioType type)
{
switch (type)
{
case AudioType.bgm:
case AudioType.effect:
AudioSource source = GetAudioSource(type);
if (source != null)
{
source.Stop();
}
break;
case AudioType.speek:
StopSpeech();
break;
}
}
private AudioSource GetAudioSource(AudioType type)
{
switch (type)
{
case AudioType.bgm: return bgm;
case AudioType.effect: return effect;
case AudioType.speek: return speek;
}
return null;
}
private AudioClip GetSound(int index)
{
AudioClip clip = null;
if (sounds != null && sounds.Length - 1 >= index && index >= 0)
{
return sounds[index];
}
return clip;
}
}
}
4、使用方法:控制播放某种声音的某个声音片段时,调用函数PlayAudio(声音种类,声音资源序号(Resources中加载的声音列表)),如PlayAudio(AudioType.bgm, 1)