313 lines
7.9 KiB
C#
313 lines
7.9 KiB
C#
|
|
//流式播放的音频管理器
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using CriWare;
|
|||
|
|
using Framework;
|
|||
|
|
using PhxhSDK;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using AudioType = PhxhSDK.AudioBase.AudioType;
|
|||
|
|
|
|||
|
|
public class AudioCriManager: Singlenton<AudioCriManager>, IStart, IInitable, IUpdatable, IAudioPlay
|
|||
|
|
{
|
|||
|
|
public GameObject AudioRoot;
|
|||
|
|
public GameObject CriAtomRoot;
|
|||
|
|
public CriAtom CriAtom;
|
|||
|
|
public ClipType curClipType;
|
|||
|
|
|
|||
|
|
public enum EPlayType
|
|||
|
|
{
|
|||
|
|
Music,
|
|||
|
|
Sound,
|
|||
|
|
Speak
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private GameObject _criError;
|
|||
|
|
|
|||
|
|
private Dictionary<int, AudioInst> _soundAudioDic;
|
|||
|
|
private Dictionary<int, AudioInst> _musicAudioDic;
|
|||
|
|
private Dictionary<int, AudioInst> _allAudioDic;
|
|||
|
|
private List<GameObject> _soundAudioObj;
|
|||
|
|
private List<GameObject> _musicAudioObj;
|
|||
|
|
private List<AudioInst> _soundAudioQue;
|
|||
|
|
|
|||
|
|
private const int SOUND_MAX_NUM = 20;
|
|||
|
|
private const int MUSIC_MAX_NUM = 2;
|
|||
|
|
|
|||
|
|
#region Property
|
|||
|
|
|
|||
|
|
public float GlobalSoundsVolume;
|
|||
|
|
public float GlobalSpeakVolume;
|
|||
|
|
public float GlobalMusicsVolume;
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region Framework
|
|||
|
|
|
|||
|
|
public void Start()
|
|||
|
|
{
|
|||
|
|
if (!AudioRoot)
|
|||
|
|
{
|
|||
|
|
curClipType = ClipType.CriwareClip;
|
|||
|
|
|
|||
|
|
AudioRoot = new GameObject();
|
|||
|
|
AudioRoot.name = "Audio(Singleton)";
|
|||
|
|
Object.DontDestroyOnLoad(AudioRoot);
|
|||
|
|
|
|||
|
|
if (curClipType == ClipType.CriwareClip)
|
|||
|
|
{
|
|||
|
|
_criError = new GameObject();
|
|||
|
|
_criError.name = "CriwareError";
|
|||
|
|
_criError.AddComponent<CriWareErrorHandler>();
|
|||
|
|
Object.DontDestroyOnLoad(_criError);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
CriAtomRoot = GameObject.Find("CriAtom");
|
|||
|
|
CriAtom = CriAtomRoot.GetComponent<CriAtom>();
|
|||
|
|
Object.DontDestroyOnLoad(CriAtomRoot);
|
|||
|
|
|
|||
|
|
|
|||
|
|
_soundAudioDic = new Dictionary<int, AudioInst>();
|
|||
|
|
_musicAudioDic = new Dictionary<int, AudioInst>();
|
|||
|
|
_allAudioDic = new Dictionary<int, AudioInst>();
|
|||
|
|
|
|||
|
|
// CriAtomRoot = new GameObject()
|
|||
|
|
// {
|
|||
|
|
// name = "CriAtom"
|
|||
|
|
// };
|
|||
|
|
// CriAtom = CriAtomRoot.AddComponent<CriAtom>();
|
|||
|
|
// CriAtom.AddCueSheet()
|
|||
|
|
|
|||
|
|
// for (int i = 0; i < SOUND_MAX_NUM; i++)
|
|||
|
|
// {
|
|||
|
|
// var go = new GameObject
|
|||
|
|
// {
|
|||
|
|
// name = "Sound" + (i+1)
|
|||
|
|
// };
|
|||
|
|
// go.transform.SetParent(AudioRoot.transform);
|
|||
|
|
// }
|
|||
|
|
//
|
|||
|
|
// for (int i = 0; i < MUSIC_MAX_NUM; i++)
|
|||
|
|
// {
|
|||
|
|
// var go = new GameObject
|
|||
|
|
// {
|
|||
|
|
// name = "Music" + (i+1)
|
|||
|
|
// };
|
|||
|
|
// go.transform.SetParent(AudioRoot.transform);
|
|||
|
|
// }
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Init()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Release()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Update(float dt)
|
|||
|
|
{
|
|||
|
|
foreach (var kv in _musicAudioDic)
|
|||
|
|
{
|
|||
|
|
var audio = kv.Value;
|
|||
|
|
if (!audio.IsStopped)
|
|||
|
|
{
|
|||
|
|
audio.Update();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
foreach (var kv in _soundAudioDic)
|
|||
|
|
{
|
|||
|
|
var audio = kv.Value;
|
|||
|
|
if (!audio.IsStopped)
|
|||
|
|
{
|
|||
|
|
audio.Update();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region LoadAsset
|
|||
|
|
|
|||
|
|
private void LoadAcb(string name)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region AudioObj
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取可用的Audio
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="audioType"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
private AudioInst _GetUsableAudio(AudioBase.AudioType audioType)
|
|||
|
|
{
|
|||
|
|
var freeAudio = _GetFreeAudio(audioType);
|
|||
|
|
return freeAudio?? _CreateNewAudio(audioType);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private Dictionary<int, AudioInst> _GetAudioDic(AudioBase.AudioType audioType)
|
|||
|
|
{
|
|||
|
|
switch (audioType)
|
|||
|
|
{
|
|||
|
|
case AudioBase.AudioType.Music:
|
|||
|
|
return _musicAudioDic;
|
|||
|
|
case AudioBase.AudioType.Sound:
|
|||
|
|
return _soundAudioDic;
|
|||
|
|
default:
|
|||
|
|
{
|
|||
|
|
DebugUtil.LogError("获取AudioDic出错!!!传入AudioType有误!");
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private AudioInst _GetFreeAudio(AudioBase.AudioType audioType)
|
|||
|
|
{
|
|||
|
|
var dic = _GetAudioDic(audioType);
|
|||
|
|
if (dic != null)
|
|||
|
|
{
|
|||
|
|
foreach (var kv in _soundAudioDic)
|
|||
|
|
{
|
|||
|
|
var audio = kv.Value;
|
|||
|
|
if (audio.IsStopped)
|
|||
|
|
{
|
|||
|
|
return audio;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private AudioInst _CreateNewAudio(AudioBase.AudioType audioType)
|
|||
|
|
{
|
|||
|
|
var rootGo = new GameObject()
|
|||
|
|
{
|
|||
|
|
name = "Audio"
|
|||
|
|
};
|
|||
|
|
rootGo.transform.SetParent(AudioRoot.transform);
|
|||
|
|
var audio = new AudioInst(audioType, rootGo, null);
|
|||
|
|
rootGo.name += audio.audioID;
|
|||
|
|
|
|||
|
|
return audio;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private int _CreateAudio(AudioBase.AudioType audioType, GameObject rootGo, GameObject posGo = null)
|
|||
|
|
{
|
|||
|
|
var dic = _GetAudioDic(audioType);
|
|||
|
|
if (audioType == AudioBase.AudioType.Music && dic.Count >= MUSIC_MAX_NUM)
|
|||
|
|
{
|
|||
|
|
DebugUtil.Log("音乐播放数量超过上限!!!");
|
|||
|
|
return -1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (audioType == AudioBase.AudioType.Sound && dic.Count >= SOUND_MAX_NUM)
|
|||
|
|
{
|
|||
|
|
DebugUtil.Log("音效播放数量超过上限!!!");
|
|||
|
|
return -1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
AudioInst audio = new AudioInst(audioType, rootGo, posGo);
|
|||
|
|
|
|||
|
|
return audio.audioID;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region Play
|
|||
|
|
|
|||
|
|
private int _PlayAudio(string key, EPlayType type, GameObject posObj = null)
|
|||
|
|
{
|
|||
|
|
// var audioCfg = TableManager.Instance.Tables.Audios.GetByKey(key);
|
|||
|
|
// if (audioCfg == null)
|
|||
|
|
// {
|
|||
|
|
// DebugUtil.LogError($"无法获取音频表配置,key:{key}");
|
|||
|
|
// return -1;
|
|||
|
|
// }
|
|||
|
|
if (CriAtom)
|
|||
|
|
{
|
|||
|
|
if (CriAtom.GetCueSheetInternal(key) == null)
|
|||
|
|
{
|
|||
|
|
if (key.Contains("skill_"))
|
|||
|
|
{
|
|||
|
|
CriAtom.AddCueSheet(key, "CueSheet_SE.acb", "CueSheet_SE.awb", null);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
string path = "CueSheet_VOICE";
|
|||
|
|
|
|||
|
|
switch (type)
|
|||
|
|
{
|
|||
|
|
case EPlayType.Music:
|
|||
|
|
{
|
|||
|
|
return _PlayMusic(key, path, posObj);
|
|||
|
|
}
|
|||
|
|
case EPlayType.Sound:
|
|||
|
|
{
|
|||
|
|
return _PlaySound(key, path, posObj);
|
|||
|
|
}
|
|||
|
|
default: break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return -1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private int _PlayMusic(string key, string path, GameObject posObj = null)
|
|||
|
|
{
|
|||
|
|
AudioInfo audioInfo = new AudioInfo()
|
|||
|
|
{
|
|||
|
|
persist = true,
|
|||
|
|
volume = 1,
|
|||
|
|
fadeInValue = 0.3f,
|
|||
|
|
fadeOutValue = 0.3f,
|
|||
|
|
};
|
|||
|
|
var audio = _GetUsableAudio(AudioBase.AudioType.Music);
|
|||
|
|
if (posObj != null)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
return -1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private int _PlaySound(string key, string path, GameObject posObj = null)
|
|||
|
|
{
|
|||
|
|
AudioInfo audioInfo = new AudioInfo()
|
|||
|
|
{
|
|||
|
|
persist = true,
|
|||
|
|
volume = 1,
|
|||
|
|
fadeInValue = 0.3f,
|
|||
|
|
fadeOutValue = 0.3f,
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
var audio = _GetUsableAudio(AudioBase.AudioType.Sound);
|
|||
|
|
audio.SetPosObj(posObj);
|
|||
|
|
audio.Play(path, key);
|
|||
|
|
|
|||
|
|
return -1;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region API
|
|||
|
|
|
|||
|
|
public int PlayAudio(string key, EPlayType type, GameObject posObj = null)
|
|||
|
|
{
|
|||
|
|
return _PlayAudio(key, type, posObj);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public class AudioInst : AudioCri
|
|||
|
|
{
|
|||
|
|
public AudioInst(AudioType audioType, GameObject rootGo, GameObject posGo, AudioInfo audioInfo = new(),
|
|||
|
|
bool loop = false, bool is3D = false):base(audioType, rootGo, posGo, audioInfo, loop, is3D)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|