688 lines
20 KiB
C#
688 lines
20 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using Cysharp.Threading.Tasks;
|
||
using PhxhSDK;
|
||
using UnityEngine;
|
||
using Object = UnityEngine.Object;
|
||
|
||
public class AudioUnityMgr: AudioMgrBase, IAudioPlay
|
||
{
|
||
private static AudioUnityMgr _instance;
|
||
|
||
private Dictionary<int, SingleAudio> _musicAudio;
|
||
private Dictionary<int, SingleAudio> _soundsAudio;
|
||
|
||
private Queue<SingleAudio> _freeMusicAudio;
|
||
private Queue<SingleAudio> _freeSoundsAudio;
|
||
|
||
private List<int> _toBeRemoved;
|
||
|
||
private bool _banSound;
|
||
private bool _banMusic;
|
||
public static AudioUnityMgr Instance
|
||
{
|
||
get
|
||
{
|
||
if (_instance == null)
|
||
{
|
||
_instance = new AudioUnityMgr();
|
||
}
|
||
|
||
return _instance;
|
||
}
|
||
}
|
||
|
||
private const int SOUNDS_POOL_MAX_NUM = 999;
|
||
private const int MUSIC_POOL_MAX_NUM = 10;
|
||
|
||
public override void Start()
|
||
{
|
||
if (initGameObject == null)
|
||
{
|
||
initGameObject = new GameObject();
|
||
initGameObject.name = "Audio(Singleton)";
|
||
Object.DontDestroyOnLoad(initGameObject);
|
||
}
|
||
}
|
||
|
||
public override void Init()
|
||
{
|
||
base.Init();
|
||
_musicAudio = new Dictionary<int, SingleAudio>();
|
||
_soundsAudio = new Dictionary<int, SingleAudio>();
|
||
_freeMusicAudio = new Queue<SingleAudio>();
|
||
_freeSoundsAudio = new Queue<SingleAudio>();
|
||
_toBeRemoved = new List<int>();
|
||
}
|
||
|
||
public override void Release()
|
||
{
|
||
|
||
}
|
||
|
||
public override void Update(float dt)
|
||
{
|
||
using (var e = _musicAudio.GetEnumerator())
|
||
{
|
||
while (e.MoveNext())
|
||
{
|
||
var music = e.Current.Value;
|
||
music.Update();
|
||
if (!music.audioSource)
|
||
{
|
||
_toBeRemoved.Add(e.Current.Key);
|
||
}
|
||
else if (!music.IsPlaying && !music.IsPaused && !music.IsLoading)
|
||
{
|
||
_toBeRemoved.Add(e.Current.Key);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (_toBeRemoved.Count > 0)
|
||
{
|
||
for (int i = 0; i < _toBeRemoved.Count; i++)
|
||
{
|
||
if (_musicAudio.TryGetValue(_toBeRemoved[i], out var music))
|
||
{
|
||
_musicAudio.Remove(_toBeRemoved[i]);
|
||
if (IsFreePoolRemainder(AudioType.MUSIC))
|
||
{
|
||
_freeMusicAudio.Enqueue(music);
|
||
}
|
||
else
|
||
{
|
||
Object.Destroy(music.audioSource);
|
||
if (music.RootGo && music.RootGo != initGameObject)
|
||
{
|
||
Object.Destroy(music.RootGo);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
using (var e = _soundsAudio.GetEnumerator())
|
||
{
|
||
while (e.MoveNext())
|
||
{
|
||
var music = e.Current.Value;
|
||
music.Update();
|
||
if (!music.audioSource)
|
||
{
|
||
_toBeRemoved.Add(e.Current.Key);
|
||
}
|
||
else if (!music.IsPlaying && !music.IsPaused && !music.IsLoading)
|
||
{
|
||
_toBeRemoved.Add(e.Current.Key);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (_toBeRemoved.Count > 0)
|
||
{
|
||
for (int i = 0; i < _toBeRemoved.Count; i++)
|
||
{
|
||
if (_soundsAudio.TryGetValue(_toBeRemoved[i], out var sound))
|
||
{
|
||
_soundsAudio.Remove(_toBeRemoved[i]);
|
||
if (IsFreePoolRemainder(AudioType.SOUND))
|
||
{
|
||
_freeSoundsAudio.Enqueue(sound);
|
||
}
|
||
else
|
||
{
|
||
Object.Destroy(sound.audioSource);
|
||
if (sound.RootGo && sound.RootGo != initGameObject)
|
||
{
|
||
Object.Destroy(sound.RootGo);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//检测是否到对象池上限
|
||
private bool IsFreePoolRemainder(AudioType audioType)
|
||
{
|
||
if (audioType == AudioType.MUSIC)
|
||
{
|
||
return _freeMusicAudio.Count < MUSIC_POOL_MAX_NUM;
|
||
}
|
||
|
||
return _freeSoundsAudio.Count < SOUNDS_POOL_MAX_NUM;
|
||
}
|
||
|
||
|
||
//预加载AudioClip资源
|
||
// private async UniTask PreLoadClipAssets()
|
||
// {
|
||
// await PreLoadAudioAssets.PreLoadAudioByModule(AudioAssetModule.Fight);
|
||
// }
|
||
|
||
private int _PlayMusic(AudioClip clip, UnityAudio audioData, Transform trans = null, Vector3 pos = default)
|
||
{
|
||
if (clip == null)
|
||
{
|
||
DebugUtil.LogError("Sound Manager: Audio clip is null, cannot play music");
|
||
}
|
||
|
||
try
|
||
{
|
||
if (_banSound)
|
||
{
|
||
return NoneID;
|
||
}
|
||
|
||
//开启重复忽略,同名资源播放时不重复播放
|
||
if (IgnoreDuplicateSounds)
|
||
{
|
||
foreach (var kv in _musicAudio)
|
||
{
|
||
var music = kv.Value;
|
||
if (music.audioSource.clip.name == clip.name)
|
||
{
|
||
return NoneID;
|
||
}
|
||
}
|
||
}
|
||
|
||
//创建Audio,进行播放
|
||
#if UNITY_EDITOR || UNITY_IOS || UNITY_ANDROID || UNITY_WEBGL
|
||
SingleAudio audio;
|
||
GameObject go;
|
||
var audioPos = pos;
|
||
if (trans)
|
||
{
|
||
audioPos = trans.transform.position;
|
||
}
|
||
else if (audioPos == default)
|
||
{
|
||
audioPos = initGameObject.transform.position;
|
||
}
|
||
// if (trans)
|
||
// {
|
||
// if (trans.gameObject != initGameObject)
|
||
// {
|
||
// go = new GameObject
|
||
// {
|
||
// name = "AudioPlayer",
|
||
// transform =
|
||
// {
|
||
// position = trans.position
|
||
// }
|
||
// };
|
||
// go.transform.SetParent(initGameObject.transform);
|
||
// }
|
||
// else
|
||
// {
|
||
// go = initGameObject;
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// go = new GameObject
|
||
// {
|
||
// name = "AudioPlayer",
|
||
// transform =
|
||
// {
|
||
// position = pos
|
||
// }
|
||
// };
|
||
// go.transform.SetParent(initGameObject.transform);
|
||
// }
|
||
|
||
|
||
if (_freeMusicAudio.Count > 0)
|
||
{
|
||
audio = _freeMusicAudio.Dequeue();
|
||
audio.GenNewID();
|
||
audio.RootGo.transform.position = audioPos;
|
||
}
|
||
else
|
||
{
|
||
go = new GameObject
|
||
{
|
||
name = "AudioPlayer",
|
||
transform =
|
||
{
|
||
position = audioPos
|
||
}
|
||
};
|
||
audio = new SingleAudio(AudioBase.AudioType.Music, clip, go.transform, audioData);
|
||
go.transform.SetParent(initGameObject.transform);
|
||
}
|
||
_musicAudio.Add(audio.audioID, audio);
|
||
return audio.audioID;
|
||
#endif
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError(e);
|
||
throw;
|
||
}
|
||
}
|
||
|
||
private int _PlaySound(AudioClip clip, UnityAudio audioData, Transform trans = null, Vector3 pos = default)
|
||
{
|
||
if (clip == null)
|
||
{
|
||
DebugUtil.LogError("Sound Manager: Audio clip is null, cannot play music");
|
||
}
|
||
|
||
try
|
||
{
|
||
if (_banSound)
|
||
{
|
||
return NoneID;
|
||
}
|
||
|
||
//开启重复忽略,同名资源播放时不重复播放
|
||
if (IgnoreDuplicateSounds)
|
||
{
|
||
foreach (var kv in _soundsAudio)
|
||
{
|
||
var sound = kv.Value;
|
||
if (sound.audioSource.clip.name == clip.name)
|
||
{
|
||
return NoneID;
|
||
}
|
||
}
|
||
}
|
||
|
||
//创建Audio,进行播放
|
||
#if UNITY_EDITOR || UNITY_IOS || UNITY_ANDROID || UNITY_WEBGL
|
||
SingleAudio audio;
|
||
GameObject go;
|
||
|
||
var audioPos = pos;
|
||
if (trans)
|
||
{
|
||
audioPos = trans.transform.position;
|
||
}
|
||
else if (audioPos == default)
|
||
{
|
||
audioPos = initGameObject.transform.position;
|
||
}
|
||
// if (trans)
|
||
// {
|
||
// if (trans.gameObject != initGameObject)
|
||
// {
|
||
// go = new GameObject
|
||
// {
|
||
// name = "AudioPlayer",
|
||
// transform =
|
||
// {
|
||
// position = trans.position
|
||
// }
|
||
// };
|
||
// go.transform.SetParent(initGameObject.transform);
|
||
// }
|
||
// else
|
||
// {
|
||
// go = initGameObject;
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// go = new GameObject
|
||
// {
|
||
// name = "AudioPlayer",
|
||
// transform =
|
||
// {
|
||
// position = pos
|
||
// }
|
||
// };
|
||
// go.transform.SetParent(initGameObject.transform);
|
||
// }
|
||
|
||
|
||
if (_freeSoundsAudio.Count > 0)
|
||
{
|
||
audio = _freeSoundsAudio.Dequeue();
|
||
audio.GenNewID();
|
||
audio.RootGo.transform.position = audioPos;
|
||
}
|
||
else
|
||
{
|
||
go = new GameObject
|
||
{
|
||
name = "AudioPlayer",
|
||
transform =
|
||
{
|
||
position = audioPos
|
||
}
|
||
};
|
||
audio = new SingleAudio(AudioBase.AudioType.Sound, clip, go.transform, audioData);
|
||
go.transform.SetParent(initGameObject.transform);
|
||
}
|
||
_soundsAudio.Add(audio.audioID, audio);
|
||
return audio.audioID;
|
||
#endif
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError(e);
|
||
throw;
|
||
}
|
||
}
|
||
|
||
private int _DoPlayAudio(AudioType type, AudioClip clip, Transform trans, UnityAudio audioData)
|
||
{
|
||
int audioID = NoneID;
|
||
switch (type)
|
||
{
|
||
case(AudioType.MUSIC):
|
||
audioID = _PlayMusic(clip, audioData, trans);
|
||
break;
|
||
|
||
case(AudioType.SOUND):
|
||
audioID = _PlaySound(clip, audioData, trans);
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
return audioID;
|
||
}
|
||
|
||
private int _DoPlayAudio(AudioType type, AudioClip clip, Vector3 pos, UnityAudio audioData)
|
||
{
|
||
int audioID = NoneID;
|
||
switch (type)
|
||
{
|
||
case(AudioType.MUSIC):
|
||
audioID = _PlayMusic(clip, audioData, null, pos);
|
||
break;
|
||
|
||
case(AudioType.SOUND):
|
||
audioID = _PlaySound(clip, audioData, null, pos);
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
|
||
return audioID;
|
||
}
|
||
|
||
//根据项目添加播放接口
|
||
public int PlayAudio(AudioType type, AudioClip clip, UnityAudio audioData,Transform trans = null, Vector3 pos = new())
|
||
{
|
||
if (trans)
|
||
{
|
||
return _DoPlayAudio(type, clip, trans, audioData);
|
||
}
|
||
return _DoPlayAudio(type, clip, pos, audioData);
|
||
}
|
||
|
||
public SingleAudio GetSoundAudio(int id)
|
||
{
|
||
_soundsAudio.TryGetValue(id, out var sound);
|
||
return sound;
|
||
}
|
||
|
||
// private int _PlayAudio(cfg.AudioCfg.AudioType type, string path, UnityAudio audioData, Transform trans = null, Vector3 pos = new())
|
||
// {
|
||
// try
|
||
// {
|
||
// var audioType = (type == cfg.AudioCfg.AudioType.Music) ? AudioType.MUSIC : AudioType.SOUND;
|
||
// var clip = AssetManager.Instance.LoadAsset<AudioClip>(path);
|
||
// if (!clip)
|
||
// {
|
||
// DebugUtil.LogError("--- AudioClip did not Loaded!!! ---");
|
||
// return NoneID;
|
||
// }
|
||
//
|
||
// //根据参数选择播放接口
|
||
// if (trans)
|
||
// {
|
||
// return _DoPlayAudio(audioType, clip, trans, audioData);
|
||
// }
|
||
//
|
||
// return _DoPlayAudio(audioType, clip, pos, audioData);
|
||
// }
|
||
// catch (Exception e)
|
||
// {
|
||
// DebugUtil.LogError(e);
|
||
// throw;
|
||
// }
|
||
//
|
||
// }
|
||
|
||
//传入播放数据的接口
|
||
// private int _PlayAudioWithInfo(int id, string key, UnityAudio audioInfo, Transform trans = null, Vector3 pos = new())
|
||
// {
|
||
// cfg.AudioCfg.DataAudio audioData;
|
||
// if (id != NoneID)
|
||
// {
|
||
// audioData = TableManager.Instance.Tables.Audios.GetByID(id);
|
||
// }
|
||
// else
|
||
// {
|
||
// audioData = TableManager.Instance.Tables.Audios.GetByKey(key);
|
||
// }
|
||
//
|
||
// var type = audioData.Type;
|
||
// var path = audioData.Path;
|
||
// if (trans)
|
||
// {
|
||
// if (type == cfg.AudioCfg.AudioType.Music)
|
||
// {
|
||
// return _PlayAudio(type, path, audioInfo, trans);
|
||
// }
|
||
//
|
||
// return _PlayAudio(type, path, audioInfo, trans);
|
||
// }
|
||
//
|
||
// if (type == cfg.AudioCfg.AudioType.Music)
|
||
// {
|
||
// return _PlayAudio(type, path, audioInfo, null, pos);
|
||
// }
|
||
//
|
||
// return _PlayAudio(type, path, audioInfo, null, pos);
|
||
// }
|
||
//
|
||
// public int PlayAudio(string key, UnityAudio audioInfo, Transform trans = null, Vector3 pos = new())
|
||
// {
|
||
// return _PlayAudioWithInfo(NoneID, key, audioInfo, trans, pos);
|
||
// }
|
||
//
|
||
// public int PlayAudio(int id, UnityAudio audioInfo, Transform trans = null, Vector3 pos = new())
|
||
// {
|
||
// return _PlayAudioWithInfo(id, "", audioInfo, trans, pos);
|
||
// }
|
||
|
||
// public int PlayAudio(string key, Transform trans = null)
|
||
// {
|
||
// var audioData = TableManager.Instance.Tables.Audios.GetByKey(key);
|
||
// var type = audioData.Type;
|
||
// var path = audioData.Path;
|
||
// if (!trans)
|
||
// {
|
||
// trans = initGameObject.transform;
|
||
// }
|
||
//
|
||
// if (type == cfg.AudioCfg.AudioType.Music)
|
||
// {
|
||
// var audioInfo = new UnityAudio(true);
|
||
// return _PlayAudio(type, path, audioInfo, trans);
|
||
// }
|
||
// else
|
||
// {
|
||
// var audioInfo = new UnityAudio(false);
|
||
// return _PlayAudio(type, path, audioInfo, trans);
|
||
// }
|
||
// }
|
||
//
|
||
// public int PlayAudio(int id, Transform trans = null)
|
||
// {
|
||
// var audioData = TableManager.Instance.Tables.Audios.GetByID(id);
|
||
// var type = audioData.Type;
|
||
// var path = audioData.Path;
|
||
// if (!trans)
|
||
// {
|
||
// trans = initGameObject.transform;
|
||
// }
|
||
//
|
||
// if (type == cfg.AudioCfg.AudioType.Music)
|
||
// {
|
||
// var audioInfo = new UnityAudio(true);
|
||
// return _PlayAudio(type, path, audioInfo, trans);
|
||
// }
|
||
// else
|
||
// {
|
||
// var audioInfo = new UnityAudio(false);
|
||
// return _PlayAudio(type, path, audioInfo, trans);
|
||
// }
|
||
// }
|
||
//
|
||
// public int PlayAudio(string key, Vector3 pos)
|
||
// {
|
||
// var audioData = TableManager.Instance.Tables.Audios.GetByKey(key);
|
||
// var type = audioData.Type;
|
||
// var path = audioData.Path;
|
||
//
|
||
// if (type == cfg.AudioCfg.AudioType.Music)
|
||
// {
|
||
// var audioInfo = new UnityAudio(true);
|
||
// return _PlayAudio(type, path, audioInfo, null, pos);
|
||
// }
|
||
// else
|
||
// {
|
||
// var audioInfo = new UnityAudio(false);
|
||
// return _PlayAudio(type, path, audioInfo, null, pos);
|
||
// }
|
||
// }
|
||
//
|
||
// public int PlayAudio(int id, Vector3 pos)
|
||
// {
|
||
// var audioData = TableManager.Instance.Tables.Audios.GetByID(id);
|
||
// var type = audioData.Type;
|
||
// var path = audioData.Path;
|
||
//
|
||
// if (type == cfg.AudioCfg.AudioType.Music)
|
||
// {
|
||
// var audioInfo = new UnityAudio(true);
|
||
// return _PlayAudio(type, path, audioInfo, null, pos);
|
||
// }
|
||
// else
|
||
// {
|
||
// var audioInfo = new UnityAudio(false);
|
||
// return _PlayAudio(type, path, audioInfo, null, pos);
|
||
// }
|
||
// }
|
||
|
||
public override void PauseAllMusic()
|
||
{
|
||
List<int> keys = new List<int>(_musicAudio.Keys);
|
||
foreach (int key in keys)
|
||
{
|
||
SingleAudio audio = _musicAudio[key];
|
||
audio.Pause();
|
||
}
|
||
}
|
||
|
||
public override void PauseAllSounds()
|
||
{
|
||
List<int> keys = new List<int>(_soundsAudio.Keys);
|
||
foreach (int key in keys)
|
||
{
|
||
SingleAudio audio = _soundsAudio[key];
|
||
audio.Pause();
|
||
}
|
||
}
|
||
|
||
public override void ResumeAllMusic()
|
||
{
|
||
if (_banMusic)
|
||
return;
|
||
|
||
List<int> keys = new List<int>(_musicAudio.Keys);
|
||
foreach (int key in keys)
|
||
{
|
||
SingleAudio audio = _musicAudio[key];
|
||
audio.Resume();
|
||
}
|
||
}
|
||
|
||
public override void ResumeAllSounds()
|
||
{
|
||
List<int> keys = new List<int>(_soundsAudio.Keys);
|
||
foreach (int key in keys)
|
||
{
|
||
SingleAudio audio = _soundsAudio[key];
|
||
audio.Resume();
|
||
}
|
||
}
|
||
|
||
public override void StopAllMusic(float fadeOutSeconds = -1.0f)
|
||
{
|
||
List<int> keys = new List<int>(_musicAudio.Keys);
|
||
foreach (int key in keys)
|
||
{
|
||
SingleAudio audio = _musicAudio[key];
|
||
if (fadeOutSeconds > 0)
|
||
{
|
||
audio.fadeOutSeconds = fadeOutSeconds;
|
||
}
|
||
|
||
audio.Stop();
|
||
}
|
||
}
|
||
|
||
public override void StopAllSounds()
|
||
{
|
||
List<int> keys = new List<int>(_soundsAudio.Keys);
|
||
foreach (int key in keys)
|
||
{
|
||
SingleAudio audio = _soundsAudio[key];
|
||
audio.Stop();
|
||
}
|
||
}
|
||
|
||
|
||
public void StopSoundById(int soundId)
|
||
{
|
||
if (soundId >= 0)
|
||
{
|
||
List<int> keys = new List<int>(_soundsAudio.Keys);
|
||
foreach (int key in keys)
|
||
{
|
||
SingleAudio audio = _soundsAudio[key];
|
||
if (audio.audioID == soundId)
|
||
{
|
||
audio.Stop();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public void StopMusicById(int soundId)
|
||
{
|
||
if (soundId >= 0)
|
||
{
|
||
List<int> keys = new List<int>(_musicAudio.Keys);
|
||
foreach (int key in keys)
|
||
{
|
||
SingleAudio audio = _musicAudio[key];
|
||
if (audio.audioID == soundId)
|
||
{
|
||
audio.Stop();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public class SingleAudio: AudioAuClip
|
||
{
|
||
public SingleAudio(AudioType audioType, AudioClip clip, Transform sourceTransform, UnityAudio audioData)
|
||
: base(audioType, clip, sourceTransform, audioData.Loop, audioData.Persist, audioData.Volume,
|
||
audioData.FadeInValue, audioData.FadeOutValue,audioData.SpatialBlend, audioData.MinDis, audioData.MaxDis)
|
||
{
|
||
|
||
}
|
||
}
|