您的位置:首页 > 移动开发 > Unity3D

Unity 声音播放管理模块

2017-03-10 14:25 337 查看
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class SoundManager : MonoBehaviour
{

private static SoundManager main;

public static SoundManager Instance
{
get
{
if (main == null)
{
GameObject obj = new GameObject("SoundManager");
main = obj.AddComponent<SoundManager>();
}
return main;
}
}

private List<Sound> soundList = new List<Sound>();

private float _deltaTime;

void Update()
{

soundList.ToList().ForEach(x =>
{
if(x.isScaleTime == false){
_deltaTime = Time.unscaledDeltaTime;
}
else {
_deltaTime = Time.deltaTime;
}

x.playTime += _deltaTime;

if (!x.audioSource.isPlaying && x.isFinish)
{
if(!(x.loop))
x.Finish();
}

});
}

public Sound PlayFromAssetbundle(string name,bool isScaleTime = false)
{
AudioClip clip = Resources.Load<AudioClip>(name);
if (clip == null)
clip = Resources.Load<AudioClip>("通用点击");
return PlaySound(clip, name);
}

public Sound PlayFromResource(string path,string name)
{
AudioClip clip = Resources.Load<AudioClip>(path + name);
if (clip == null)
clip = Resources.Load<AudioClip>("00通用点击");
return PlaySound(clip,name);

}
/// <summary>
/// 人声
/// </summary>
public Sound PlayVoice(string name)
{
AudioClip clip = Resources.Load<AudioClip>("Sound/Voice/zh/" + name);
if (clip == null)
{
Debug.LogError("错误的语音:" + name);
clip = Resources.Load<AudioClip>("00通用点击");
}

return PlaySound(clip, name);
}
/// <summary>
/// 场景音效
/// </summary>
public Sound PlaySceneSound(string name)
{
AudioClip clip = Resources.Load<AudioClip>("Sound/SceneSound/" + name);
if (clip == null)
{
Debug.LogError("错误的语音:" + name);
clip = Resources.Load<AudioClip>("00通用点击");
}
return PlaySound(clip, name);
}
/// <summary>
/// 动作音效
/// </summary>
public Sound PlayAnimaSound(string name)
{
//AudioClip clip = Resources.Load<AudioClip>("Sound/AnimaSound/" + name);
//if (clip == null)
//{
//    Debug.LogError("错误的语音:" + name);
//    clip = Resources.Load<AudioClip>("00通用点击");
//}
//return PlaySound(clip, name);

return PlaySceneSound(name);
}
public Sound PlaySound(AudioClip clip,string soundName,bool isScaleTime = false)
{
Sound sound = new Sound(this);
sound.clip = clip;
sound.isScaleTime = isScaleTime;
sound.name = soundName;
sound.audioSource = this.gameObject.AddComponent<AudioSource>();
sound.audioSource.clip = clip;
sound.audioSource.Play();

soundList.Add(sound);
return sound;
}
public bool isPlay(string soundName)
{
List<Sound> sound = new List<Sound>(this.soundList.Where(s => s.name.Contains(soundName)));
if(sound.Count>=1)
{
if (sound[0].audioSource.isPlaying)
return true;
}
return false;
}
public void DestorySound(string name)
{
List<Sound> destoryList = SearchSound(name);

destoryList.ToList().ForEach(x => x.Finish());
}

public void DestorySoundByID(string ID)
{
List<Sound> destoryList = SearchSoundByID(ID);
destoryList.ToList().ForEach(x => x.Finish());
}
public void DestorySoundNoAction(string name)
{
List<Sound> destoryList = SearchSound(name);
destoryList.ToList().ForEach(x => x.FinishNoComplete());
}

public List<Sound> SearchSound(string soundName)
{
List<Sound> sound = new List<Sound>(this.soundList.Where(s => s.name.Contains(soundName)));
return sound;
}
public List<Sound> SearchSoundByID(string ID)
{
List<Sound> sound = new List<Sound>(this.soundList.Where(s => s.ID.Equals(ID)));
return sound;
}
public float GetClipLength(string name)
{
AudioClip clip = Resources.Load<AudioClip>("Sound/Voice/zh/" + name);
if (clip == null)
{
Debug.LogError("错误的语音:" + name);
clip = Resources.Load<AudioClip>("00通用点击");
}

return clip.length;
}
public void RemoveSound(Sound sound)
{
Destroy(sound.audioSource);

soundList.Remove(sound);
sound = null;

}

public void PauseAllSound(){
for(int i = 0;i<soundList.Count;i++){
soundList [i].audioSource.Pause ();
}
}

public void ResumeAllSound(){
for(int i = 0;i<soundList.Count;i++){
soundList [i].audioSource.UnPause ();
}
}

public void ClearSound()
{
soundList.ToList().ForEach(x => x.FinishNoComplete());
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: