374 lines
8.8 KiB
C#
374 lines
8.8 KiB
C#
using CriWare;
|
||
using PhxhSDK;
|
||
using UnityEngine;
|
||
|
||
public class AudioCri: AudioBase
|
||
{
|
||
public static float GlobalVolume = 1f;
|
||
public static float GlobalMusicVolume = 1f;
|
||
public static float GlobalSoundVolume = 1f;
|
||
public static float GlobalSpeakVolume = 1f;
|
||
|
||
private GameObject _posGo;
|
||
private CriAtomSource _criAtomSource;
|
||
private bool _isSpeak;
|
||
private bool _isStory;
|
||
private static long _curPlayID;
|
||
private long _playID = -1; //播放ID,越小越早
|
||
public long PlayID => _playID;
|
||
|
||
public CriAtomExPlayback CriAtomExPlayback;
|
||
public bool IsSpeak => _isSpeak;
|
||
public bool IsStory => _isStory;
|
||
|
||
private float _playPassTime = 0f;
|
||
private float _fadeValue = 1f;
|
||
private float _fadeTime = 4f;
|
||
private bool _isFadeIn;
|
||
private bool _isFadeOut;
|
||
|
||
public enum VolumeType
|
||
{
|
||
Music,
|
||
Sound,
|
||
Speak
|
||
}
|
||
|
||
#region Construct
|
||
|
||
public AudioCri(AudioType audioType, GameObject rootGo, GameObject posGo, AudioInfo audioInfo,
|
||
bool loop = false, bool is3D = false)
|
||
{
|
||
this.audioType = audioType;
|
||
_rootGo = rootGo;
|
||
_posGo = posGo;
|
||
_criAtomSource = _GetSource();
|
||
_criAtomSource.volume = GlobalVolume;
|
||
_isSpeak = audioInfo.isSpeak;
|
||
}
|
||
|
||
private CriAtomSource _GetSource()
|
||
{
|
||
var source = _rootGo.GetComponent<CriAtomSource>();
|
||
if (!source)
|
||
{
|
||
source = _rootGo.AddComponent<CriAtomSource>();
|
||
}
|
||
|
||
return source;
|
||
}
|
||
|
||
private void SetSourceData(AudioInfo audioInfo)
|
||
{
|
||
|
||
}
|
||
#endregion
|
||
|
||
|
||
#region Inherit
|
||
|
||
public override void Update()
|
||
{
|
||
|
||
}
|
||
public override void Update(float dt)
|
||
{
|
||
if (!_criAtomSource)
|
||
{
|
||
return;
|
||
}
|
||
|
||
//SetVolume
|
||
if (playState == PlayState.Playing || _criAtomSource.status == CriAtomSourceBase.Status.Playing)
|
||
{
|
||
//渐入渐出
|
||
_UpdateFadeValue(dt);
|
||
|
||
switch (audioType)
|
||
{
|
||
case (AudioType.Music):
|
||
{
|
||
_SetMusicVolume();
|
||
break;
|
||
}
|
||
|
||
case (AudioType.Sound):
|
||
{
|
||
if (_isSpeak)
|
||
{
|
||
_SetSpeakVolume();
|
||
}
|
||
else
|
||
{
|
||
_SetSoundVolume();
|
||
}
|
||
break;
|
||
}
|
||
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (_posGo && playState == PlayState.Playing)
|
||
{
|
||
_rootGo.transform.position = _posGo.transform.position;
|
||
}
|
||
|
||
//Update Play Status
|
||
if (_criAtomSource.status == CriAtomSourceBase.Status.PlayEnd)
|
||
{
|
||
Stop();
|
||
}
|
||
|
||
if (_criAtomSource.status == CriAtomSourceBase.Status.Stop)
|
||
{
|
||
Stop();
|
||
}
|
||
}
|
||
|
||
public override void Pause()
|
||
{
|
||
_criAtomSource.Pause(true);
|
||
playState = PlayState.Paused;
|
||
}
|
||
|
||
public override void Resume()
|
||
{
|
||
_criAtomSource.Pause(false);
|
||
playState = PlayState.Playing;
|
||
}
|
||
|
||
public override void Play(float volume = 1f)
|
||
{
|
||
playState = PlayState.Playing;
|
||
_isFadeOut = false;
|
||
//_criAtomSource.player?.DetachFader();
|
||
_criAtomSource.Play();
|
||
_playID = _GenPlayID();
|
||
AudioManager.Instance.AudioMgrObj.Add2Playing(audioType, audioID, _isSpeak);
|
||
}
|
||
|
||
public void PlayWithFade(float volume = 1f)
|
||
{
|
||
playState = PlayState.Playing;
|
||
//_criAtomSource.player?.AttachFader();
|
||
_criAtomSource.Play();
|
||
_playID = _GenPlayID();
|
||
AudioManager.Instance.AudioMgrObj.RemovePlaying(audioType, audioID);
|
||
}
|
||
public void Stop()
|
||
{
|
||
_isFadeOut = false;
|
||
_playPassTime = 0;
|
||
if (_criAtomSource.status == CriAtomSourceBase.Status.Stop && playState == PlayState.Stopped)
|
||
{
|
||
return;
|
||
}
|
||
_criAtomSource.Stop();
|
||
//_criAtomSource.player?.DetachFader();
|
||
base.Stop();
|
||
AudioManager.Instance.AudioMgrObj.RemovePlaying(audioType, audioID);
|
||
}
|
||
|
||
public void StopWithFade()
|
||
{
|
||
_isFadeOut = true;
|
||
_playPassTime = 0f;
|
||
AudioManager.Instance.AudioMgrObj.RemovePlaying(audioID);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Utils
|
||
|
||
private long _GenPlayID()
|
||
{
|
||
return ++_curPlayID;
|
||
}
|
||
private void _SetMusicVolume()
|
||
{
|
||
_criAtomSource.volume = _fadeValue * GlobalMusicVolume * GlobalVolume;
|
||
}
|
||
|
||
private void _SetSoundVolume()
|
||
{
|
||
_criAtomSource.volume = _fadeValue * GlobalSoundVolume * GlobalVolume;
|
||
}
|
||
|
||
private void _SetSpeakVolume()
|
||
{
|
||
_criAtomSource.volume = _fadeValue * GlobalSpeakVolume * GlobalVolume;
|
||
}
|
||
|
||
private void _SetIs3D(bool is3D)
|
||
{
|
||
_criAtomSource.use3dPositioning = is3D;
|
||
}
|
||
|
||
private void _UpdateFadeValue(float dt)
|
||
{
|
||
if (_isFadeIn)
|
||
{
|
||
if (_playPassTime < _fadeTime)
|
||
{
|
||
_playPassTime += dt;
|
||
}
|
||
}
|
||
|
||
if (_isFadeOut)
|
||
{
|
||
_playPassTime += dt;
|
||
}
|
||
|
||
_SetCurFadeValue();
|
||
}
|
||
private void _SetCurFadeValue()
|
||
{
|
||
if (_isFadeOut)
|
||
{
|
||
_isFadeIn = false;
|
||
}
|
||
|
||
if (_isFadeOut)
|
||
{
|
||
_fadeValue = (1 - _playPassTime / _fadeTime);
|
||
if (_fadeValue < 0)
|
||
{
|
||
_fadeValue = 0;
|
||
Stop();
|
||
}
|
||
return;
|
||
}
|
||
|
||
if (_isFadeIn && _playPassTime < _fadeTime)
|
||
{
|
||
_fadeValue = _playPassTime / _fadeTime;
|
||
if (_fadeValue > 1)
|
||
_fadeValue = 1;
|
||
return;
|
||
}
|
||
|
||
_fadeValue = 1f;
|
||
}
|
||
#endregion
|
||
|
||
#region API
|
||
|
||
public void Play(string cueName, AudioInfo audioInfo, bool isLoop = false)
|
||
{
|
||
_playPassTime = 0f;
|
||
_isFadeOut = false;
|
||
_isFadeIn = false;
|
||
playState = PlayState.Playing;
|
||
_playID = _GenPlayID();
|
||
AudioManager.Instance.AudioMgrObj.Add2Playing(audioType, audioID, _isSpeak);
|
||
}
|
||
|
||
public void Play(string cueSheet, string cueName)
|
||
{
|
||
_criAtomSource.cueSheet = cueSheet;
|
||
_criAtomSource.cueName = cueName;
|
||
|
||
_playPassTime = 0f;
|
||
_isFadeOut = false;
|
||
_isFadeIn = false;
|
||
|
||
//_criAtomSource.player?.DetachFader();
|
||
CriAtomExPlayback = _criAtomSource.Play();
|
||
playState = PlayState.Playing;
|
||
_playID = _GenPlayID();
|
||
AudioManager.Instance.AudioMgrObj.Add2Playing(audioType, audioID, _isSpeak);
|
||
}
|
||
|
||
public void PlayWithFade(string cueSheet, string cueName)
|
||
{
|
||
_criAtomSource.cueSheet = cueSheet;
|
||
_criAtomSource.cueName = cueName;
|
||
|
||
_playPassTime = 0f;
|
||
_isFadeOut = false;
|
||
_isFadeIn = true;
|
||
SetVolume(0);
|
||
|
||
//_criAtomSource.player?.AttachFader();
|
||
CriAtomExPlayback = _criAtomSource.Play();
|
||
playState = PlayState.Playing;
|
||
_playID = _GenPlayID();
|
||
AudioManager.Instance.AudioMgrObj.Add2Playing(audioType, audioID, _isSpeak);
|
||
}
|
||
|
||
public void SetIs3D(bool is3D = false)
|
||
{
|
||
_SetIs3D(is3D);
|
||
}
|
||
|
||
public void SetPosObj(GameObject go = null)
|
||
{
|
||
if (go)
|
||
{
|
||
_posGo = go;
|
||
if (_rootGo)
|
||
{
|
||
_rootGo.transform.position = _posGo.transform.position;
|
||
_SetIs3D(true);
|
||
}
|
||
|
||
return;
|
||
}
|
||
|
||
_posGo = null;
|
||
_SetIs3D(false);
|
||
}
|
||
|
||
|
||
public void SetPos(Vector3 position)
|
||
{
|
||
_rootGo.transform.position = position;
|
||
}
|
||
|
||
public void SetIsLoop(bool isLoop)
|
||
{
|
||
loop = isLoop;
|
||
_criAtomSource.loop = isLoop;
|
||
//DebugUtil.LogError($"audioID:{audioID}, , name:{_criAtomSource.cueName}, isLoop:{isLoop}");
|
||
}
|
||
|
||
public void SetIsSpeak(bool isSpeak)
|
||
{
|
||
_isSpeak = isSpeak;
|
||
}
|
||
|
||
public void SetIsStory(bool isStory)
|
||
{
|
||
_isStory = isStory;
|
||
}
|
||
public void SetIsFadeIn(bool isFadeIn)
|
||
{
|
||
_isFadeIn = isFadeIn;
|
||
}
|
||
|
||
public string GetCueSheetName()
|
||
{
|
||
if (_criAtomSource)
|
||
{
|
||
return _criAtomSource.cueSheet;
|
||
}
|
||
return "";
|
||
}
|
||
|
||
public long GetCurPlayLength()
|
||
{
|
||
var cueName = _criAtomSource.cueName;
|
||
var cueSheet = _criAtomSource.cueSheet;
|
||
var acb = AudioManager.Instance.AudioMgrObj.CriAtom.GetCueSheetInternal(cueSheet).acb;
|
||
if (acb.GetCueInfo(cueName, out var cueInfo))
|
||
{
|
||
var length = cueInfo.length / 1000;
|
||
return length;
|
||
}
|
||
|
||
return -1;
|
||
}
|
||
#endregion
|
||
} |