做2D项目比较适合
音频文件位置默认 Resources/Audio
using UnityEngine;
using System;
using System.Linq;
using System.Collections.Generic;
public class Sound
{
public AudioManager audioManager;
public string name;
public AudioClip clip;
public AudioSource source;
public Action<Sound> callback;
public bool loop;
public bool interrupts;
private HashSet<Sound> interruptedSounds =
new HashSet<Sound> ();
//声音播放进度
public float progress {
get {
if (source == null || clip == null)
return 0f;
return (float)source.timeSamples / (float)clip.samples;
}
}
///如果声音完成播放,则返回true
///循环声音 返回 false
public bool finished {
get {
return !loop && progress >= 1f;
}
}
// 播放
public bool playing {
get {
return source != null && source.isPlaying;
}
set {
if (value) {
audioManager.RegisterSound(this);
}
PlayOrPause(value, interrupts);
}
}
/// <summary>
/// 注册音频
/// </summary>
/// <param name="newName">音频名字 必须有这个音频</param>
public Sound(string newName)
{
name = newName;
clip = (AudioClip)Resources.Load("Audio/"+name, typeof(AudioClip));
if (clip == null)
Debug.log("音频不存在");
}
public void Update() {
if (source != null)
source.loop = loop;
if (finished)
Finish();
}
/// <summary>
/// 播放或者暂停
/// </summary>
/// <param name="play">播放或者暂停</param>
/// <param name="pauseOthers"> 其他音频</param>
public void PlayOrPause(bool play, bool pauseOthers) {
if (pauseOthers) {
if (play) {
interruptedSounds = new HashSet<Sound>(audioManager.sounds.Where(snd => snd.playing &&
snd != this));
}
interruptedSounds.ToList().ForEach(sound => sound.PlayOrPause(!play, false));
}
if (play && !source.isPlaying) {
source.Play();
} else {
source.Pause();
}
}
///在声音完成时执行必要的操作
public void Finish() {
PlayOrPause(false, true);
if (callback != null)
callback(this);
MonoBehaviour.Destroy(source);
source = null;
}
//重新开始声音
public void Reset() {
source.time = 0f;
}
}
要挂载在对象上
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System;
public class AudioManager : MonoBehaviour
{
public static AudioManager m_Instance = null;
public static AudioManager Instance
{
get
{
if (m_Instance == null)
{
//自己找合适的对象名称
if (GameObject.Find("GameManager").GetComponent<AudioManager>() == null)
{
IntAudio();
}
}
return GameObject.Find("GameManager").GetComponent<AudioManager>();
// return Camera.main.GetComponent<AudioManager>();
}
}
public HashSet<Sound> sounds =
new HashSet<Sound> ();
static void IntAudio()
{
GameObject.Find("GameManager").AddComponent<AudioManager>();
}
/// <summary>
/// 播放一个新的声音,注册它,给它指定的属性,并开始播放它
/// </summary>
/// <param name="名字"></param>
/// <param name="循环"></param>
/// <param name="中断其他"></param>
/// <param name="行为"></param>
/// <returns></returns>
public Sound PlayNewSound(string soundName,bool loop=false, bool interrupts=false, Action<Sound> callback=null) {
Sound sound = NewSound(soundName, loop, interrupts, callback);
sound.playing = true;
return sound;
}
/// <summary>
/// 创建一个新的声音,注册它,并给它指定的属性 不播放
/// </summary>
/// <param name="名字"></param>
/// <param name="循环"></param>
/// <param name="中断其他"></param>
/// <param name="行为"></param>
/// <returns></returns>
public Sound NewSound(string soundName, bool loop=false, bool interrupts=false, Action<Sound> callback=null) {
Sound sound = new Sound(soundName);
RegisterSound(sound);
sound.loop = loop;
sound.interrupts = interrupts;
sound.callback = callback;
return sound;
}
//注册一个音频播放组件
public void RegisterSound(Sound sound) {
sounds.Add(sound);
sound.audioManager = this;
if (sound.source == null) {
AudioSource source = gameObject.AddComponent<AudioSource>();
source.clip = sound.clip;
sound.source = source;
}
}
private void Update() {
sounds.ToList().ForEach(sound => {
sound.Update();
});
}
}
例子
// 注册
private Sound Bg;
//实例
Bg = AudioManager.Instance.NewSound("Dungeon", loop: true);
//是否播放
Bg.playing = false;