NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/Audio/AudioCri.cs

374 lines
8.8 KiB
C#
Raw Normal View History

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;
2024-10-16 15:18:33 +08:00
private bool _isStory;
private static long _curPlayID;
private long _playID = -1; //播放ID越小越早
public long PlayID => _playID;
public CriAtomExPlayback CriAtomExPlayback;
public bool IsSpeak => _isSpeak;
2024-10-16 15:18:33 +08:00
public bool IsStory => _isStory;
private float _playPassTime = 0f;
2024-09-10 16:21:12 +08:00
private float _fadeValue = 1f;
2024-09-12 17:22:41 +08:00
private float _fadeTime = 4f;
private bool _isFadeIn;
2024-09-10 16:21:12 +08:00
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)
{
2024-09-10 16:21:12 +08:00
//渐入渐出
_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();
}
2024-09-10 16:21:12 +08:00
if (_criAtomSource.status == CriAtomSourceBase.Status.Stop)
{
Stop();
}
}
2024-08-20 16:22:25 +08:00
public override void Pause()
{
_criAtomSource.Pause(true);
2024-08-20 16:22:25 +08:00
playState = PlayState.Paused;
}
public override void Resume()
{
_criAtomSource.Pause(false);
2024-08-20 16:22:25 +08:00
playState = PlayState.Playing;
}
2024-07-31 16:05:26 +08:00
public override void Play(float volume = 1f)
{
2024-07-31 16:05:26 +08:00
playState = PlayState.Playing;
2024-09-10 16:21:12 +08:00
_isFadeOut = false;
//_criAtomSource.player?.DetachFader();
2024-07-31 16:05:26 +08:00
_criAtomSource.Play();
2024-10-16 15:18:33 +08:00
_playID = _GenPlayID();
AudioManager.Instance.AudioMgrObj.Add2Playing(audioType, audioID, _isSpeak);
}
public void PlayWithFade(float volume = 1f)
{
playState = PlayState.Playing;
//_criAtomSource.player?.AttachFader();
_criAtomSource.Play();
2024-10-16 15:18:33 +08:00
_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();
2024-10-16 15:18:33 +08:00
AudioManager.Instance.AudioMgrObj.RemovePlaying(audioType, audioID);
2024-09-10 16:21:12 +08:00
}
public void StopWithFade()
{
_isFadeOut = true;
_playPassTime = 0f;
2024-10-16 15:18:33 +08:00
AudioManager.Instance.AudioMgrObj.RemovePlaying(audioID);
}
#endregion
#region Utils
2024-10-16 15:18:33 +08:00
private long _GenPlayID()
{
return ++_curPlayID;
}
private void _SetMusicVolume()
{
2024-09-10 16:21:12 +08:00
_criAtomSource.volume = _fadeValue * GlobalMusicVolume * GlobalVolume;
}
private void _SetSoundVolume()
{
2024-09-10 16:21:12 +08:00
_criAtomSource.volume = _fadeValue * GlobalSoundVolume * GlobalVolume;
}
private void _SetSpeakVolume()
{
2024-09-10 16:21:12 +08:00
_criAtomSource.volume = _fadeValue * GlobalSpeakVolume * GlobalVolume;
}
private void _SetIs3D(bool is3D)
{
_criAtomSource.use3dPositioning = is3D;
}
2024-09-10 16:21:12 +08:00
private void _UpdateFadeValue(float dt)
{
if (_isFadeIn)
{
if (_playPassTime < _fadeTime)
{
_playPassTime += dt;
}
}
if (_isFadeOut)
{
_playPassTime += dt;
}
_SetCurFadeValue();
}
private void _SetCurFadeValue()
{
2024-09-10 16:21:12 +08:00
if (_isFadeOut)
{
_isFadeIn = false;
}
if (_isFadeOut)
{
_fadeValue = (1 - _playPassTime / _fadeTime);
if (_fadeValue < 0)
{
_fadeValue = 0;
Stop();
}
return;
}
if (_isFadeIn && _playPassTime < _fadeTime)
{
2024-09-10 16:21:12 +08:00
_fadeValue = _playPassTime / _fadeTime;
if (_fadeValue > 1)
_fadeValue = 1;
return;
}
2024-09-10 16:21:12 +08:00
_fadeValue = 1f;
}
#endregion
#region API
public void Play(string cueName, AudioInfo audioInfo, bool isLoop = false)
{
_playPassTime = 0f;
2024-09-10 16:21:12 +08:00
_isFadeOut = false;
_isFadeIn = false;
2024-07-31 16:05:26 +08:00
playState = PlayState.Playing;
2024-10-16 15:18:33 +08:00
_playID = _GenPlayID();
AudioManager.Instance.AudioMgrObj.Add2Playing(audioType, audioID, _isSpeak);
}
public void Play(string cueSheet, string cueName)
{
_criAtomSource.cueSheet = cueSheet;
_criAtomSource.cueName = cueName;
2024-09-10 16:21:12 +08:00
_playPassTime = 0f;
2024-09-10 16:21:12 +08:00
_isFadeOut = false;
_isFadeIn = false;
//_criAtomSource.player?.DetachFader();
CriAtomExPlayback = _criAtomSource.Play();
playState = PlayState.Playing;
2024-10-16 15:18:33 +08:00
_playID = _GenPlayID();
AudioManager.Instance.AudioMgrObj.Add2Playing(audioType, audioID, _isSpeak);
}
public void PlayWithFade(string cueSheet, string cueName)
{
_criAtomSource.cueSheet = cueSheet;
_criAtomSource.cueName = cueName;
2024-09-10 16:21:12 +08:00
_playPassTime = 0f;
2024-09-10 16:21:12 +08:00
_isFadeOut = false;
_isFadeIn = true;
SetVolume(0);
//_criAtomSource.player?.AttachFader();
CriAtomExPlayback = _criAtomSource.Play();
2024-07-31 16:05:26 +08:00
playState = PlayState.Playing;
2024-10-16 15:18:33 +08:00
_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)
{
2024-10-16 15:18:33 +08:00
loop = isLoop;
_criAtomSource.loop = isLoop;
2024-10-12 17:04:17 +08:00
//DebugUtil.LogError($"audioID:{audioID}, , name:{_criAtomSource.cueName}, isLoop:{isLoop}");
}
public void SetIsSpeak(bool isSpeak)
{
_isSpeak = isSpeak;
}
2024-10-16 15:18:33 +08:00
public void SetIsStory(bool isStory)
{
_isStory = isStory;
}
public void SetIsFadeIn(bool isFadeIn)
{
_isFadeIn = isFadeIn;
}
public string GetCueSheetName()
{
if (_criAtomSource)
{
return _criAtomSource.cueSheet;
}
return "";
}
2024-09-10 16:21:12 +08:00
public long GetCurPlayLength()
{
var cueName = _criAtomSource.cueName;
var cueSheet = _criAtomSource.cueSheet;
2024-10-16 15:18:33 +08:00
var acb = AudioManager.Instance.AudioMgrObj.CriAtom.GetCueSheetInternal(cueSheet).acb;
2024-09-10 16:21:12 +08:00
if (acb.GetCueInfo(cueName, out var cueInfo))
{
var length = cueInfo.length / 1000;
return length;
}
return -1;
}
#endregion
}