2024-07-17 13:59:24 +08:00
|
|
|
|
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;
|
2024-07-17 13:59:24 +08:00
|
|
|
|
|
2024-07-22 19:35:34 +08:00
|
|
|
|
public CriAtomExPlayback CriAtomExPlayback;
|
2024-10-08 16:59:48 +08:00
|
|
|
|
public bool IsSpeak => _isSpeak;
|
2024-10-16 15:18:33 +08:00
|
|
|
|
public bool IsStory => _isStory;
|
2024-07-22 19:35:34 +08:00
|
|
|
|
|
2024-09-10 14:56:42 +08:00
|
|
|
|
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;
|
2024-09-10 14:56:42 +08:00
|
|
|
|
private bool _isFadeIn;
|
2024-09-10 16:21:12 +08:00
|
|
|
|
private bool _isFadeOut;
|
2024-09-10 14:56:42 +08:00
|
|
|
|
|
2024-10-12 18:21:33 +08:00
|
|
|
|
public enum VolumeType
|
|
|
|
|
|
{
|
|
|
|
|
|
Music,
|
|
|
|
|
|
Sound,
|
|
|
|
|
|
Speak
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-17 13:59:24 +08:00
|
|
|
|
#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();
|
2024-07-22 19:35:34 +08:00
|
|
|
|
_criAtomSource.volume = GlobalVolume;
|
2024-07-17 13:59:24 +08:00
|
|
|
|
_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()
|
2024-09-10 14:56:42 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
public override void Update(float dt)
|
2024-07-17 13:59:24 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (!_criAtomSource)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//SetVolume
|
2024-09-10 14:56:42 +08:00
|
|
|
|
if (playState == PlayState.Playing || _criAtomSource.status == CriAtomSourceBase.Status.Playing)
|
2024-07-17 13:59:24 +08:00
|
|
|
|
{
|
2024-09-10 16:21:12 +08:00
|
|
|
|
//渐入渐出
|
|
|
|
|
|
_UpdateFadeValue(dt);
|
2024-07-17 13:59:24 +08:00
|
|
|
|
|
2024-09-10 14:56:42 +08:00
|
|
|
|
switch (audioType)
|
2024-07-17 13:59:24 +08:00
|
|
|
|
{
|
2024-09-10 14:56:42 +08:00
|
|
|
|
case (AudioType.Music):
|
2024-07-17 13:59:24 +08:00
|
|
|
|
{
|
2024-09-10 14:56:42 +08:00
|
|
|
|
_SetMusicVolume();
|
|
|
|
|
|
break;
|
2024-07-17 13:59:24 +08:00
|
|
|
|
}
|
2024-09-10 14:56:42 +08:00
|
|
|
|
|
|
|
|
|
|
case (AudioType.Sound):
|
2024-07-17 13:59:24 +08:00
|
|
|
|
{
|
2024-09-10 14:56:42 +08:00
|
|
|
|
if (_isSpeak)
|
|
|
|
|
|
{
|
|
|
|
|
|
_SetSpeakVolume();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_SetSoundVolume();
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2024-07-17 13:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-10 14:56:42 +08:00
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2024-07-17 13:59:24 +08:00
|
|
|
|
}
|
2024-09-10 14:56:42 +08:00
|
|
|
|
|
2024-07-17 13:59:24 +08:00
|
|
|
|
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-07-17 13:59:24 +08:00
|
|
|
|
}
|
2024-08-20 16:22:25 +08:00
|
|
|
|
|
2024-07-17 13:59:24 +08:00
|
|
|
|
public override void Pause()
|
|
|
|
|
|
{
|
2024-07-22 19:35:34 +08:00
|
|
|
|
_criAtomSource.Pause(true);
|
2024-08-20 16:22:25 +08:00
|
|
|
|
playState = PlayState.Paused;
|
2024-07-17 13:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Resume()
|
|
|
|
|
|
{
|
2024-07-22 19:35:34 +08:00
|
|
|
|
_criAtomSource.Pause(false);
|
2024-08-20 16:22:25 +08:00
|
|
|
|
playState = PlayState.Playing;
|
2024-07-17 13:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-31 16:05:26 +08:00
|
|
|
|
public override void Play(float volume = 1f)
|
2024-07-17 13:59:24 +08:00
|
|
|
|
{
|
2024-07-31 16:05:26 +08:00
|
|
|
|
playState = PlayState.Playing;
|
2024-09-10 16:21:12 +08:00
|
|
|
|
_isFadeOut = false;
|
2024-09-10 14:56:42 +08:00
|
|
|
|
//_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);
|
2024-07-17 13:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-10 14:56:42 +08:00
|
|
|
|
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);
|
2024-09-10 14:56:42 +08:00
|
|
|
|
}
|
2024-07-17 13:59:24 +08:00
|
|
|
|
public void Stop()
|
|
|
|
|
|
{
|
2024-09-11 19:59:36 +08:00
|
|
|
|
_isFadeOut = false;
|
|
|
|
|
|
_playPassTime = 0;
|
|
|
|
|
|
if (_criAtomSource.status == CriAtomSourceBase.Status.Stop && playState == PlayState.Stopped)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-07-17 13:59:24 +08:00
|
|
|
|
_criAtomSource.Stop();
|
2024-09-10 14:56:42 +08:00
|
|
|
|
//_criAtomSource.player?.DetachFader();
|
2024-07-17 13:59:24 +08:00
|
|
|
|
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);
|
2024-07-17 13:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Utils
|
|
|
|
|
|
|
2024-10-16 15:18:33 +08:00
|
|
|
|
private long _GenPlayID()
|
|
|
|
|
|
{
|
|
|
|
|
|
return ++_curPlayID;
|
|
|
|
|
|
}
|
2024-07-17 13:59:24 +08:00
|
|
|
|
private void _SetMusicVolume()
|
|
|
|
|
|
{
|
2024-09-10 16:21:12 +08:00
|
|
|
|
_criAtomSource.volume = _fadeValue * GlobalMusicVolume * GlobalVolume;
|
2024-07-17 13:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void _SetSoundVolume()
|
|
|
|
|
|
{
|
2024-09-10 16:21:12 +08:00
|
|
|
|
_criAtomSource.volume = _fadeValue * GlobalSoundVolume * GlobalVolume;
|
2024-07-17 13:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void _SetSpeakVolume()
|
|
|
|
|
|
{
|
2024-09-10 16:21:12 +08:00
|
|
|
|
_criAtomSource.volume = _fadeValue * GlobalSpeakVolume * GlobalVolume;
|
2024-07-17 13:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 14:56:42 +08:00
|
|
|
|
{
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-10 14:56:42 +08:00
|
|
|
|
if (_isFadeIn && _playPassTime < _fadeTime)
|
|
|
|
|
|
{
|
2024-09-10 16:21:12 +08:00
|
|
|
|
_fadeValue = _playPassTime / _fadeTime;
|
|
|
|
|
|
if (_fadeValue > 1)
|
|
|
|
|
|
_fadeValue = 1;
|
2024-09-10 14:56:42 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-10 16:21:12 +08:00
|
|
|
|
_fadeValue = 1f;
|
2024-09-10 14:56:42 +08:00
|
|
|
|
}
|
2024-07-17 13:59:24 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region API
|
|
|
|
|
|
|
|
|
|
|
|
public void Play(string cueName, AudioInfo audioInfo, bool isLoop = false)
|
|
|
|
|
|
{
|
2024-09-10 14:56:42 +08:00
|
|
|
|
_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);
|
2024-07-17 13:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Play(string cueSheet, string cueName)
|
|
|
|
|
|
{
|
|
|
|
|
|
_criAtomSource.cueSheet = cueSheet;
|
|
|
|
|
|
_criAtomSource.cueName = cueName;
|
2024-09-10 16:21:12 +08:00
|
|
|
|
|
2024-09-10 14:56:42 +08:00
|
|
|
|
_playPassTime = 0f;
|
2024-09-10 16:21:12 +08:00
|
|
|
|
_isFadeOut = false;
|
|
|
|
|
|
_isFadeIn = false;
|
|
|
|
|
|
|
2024-09-10 14:56:42 +08:00
|
|
|
|
//_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);
|
2024-09-10 14:56:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void PlayWithFade(string cueSheet, string cueName)
|
|
|
|
|
|
{
|
|
|
|
|
|
_criAtomSource.cueSheet = cueSheet;
|
|
|
|
|
|
_criAtomSource.cueName = cueName;
|
2024-09-10 16:21:12 +08:00
|
|
|
|
|
2024-09-10 14:56:42 +08:00
|
|
|
|
_playPassTime = 0f;
|
2024-09-10 16:21:12 +08:00
|
|
|
|
_isFadeOut = false;
|
|
|
|
|
|
_isFadeIn = true;
|
|
|
|
|
|
SetVolume(0);
|
|
|
|
|
|
|
2024-09-10 14:56:42 +08:00
|
|
|
|
//_criAtomSource.player?.AttachFader();
|
2024-07-22 19:35:34 +08:00
|
|
|
|
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);
|
2024-07-17 13:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
2024-07-22 19:35:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void SetPos(Vector3 position)
|
|
|
|
|
|
{
|
|
|
|
|
|
_rootGo.transform.position = position;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SetIsLoop(bool isLoop)
|
|
|
|
|
|
{
|
2024-10-16 15:18:33 +08:00
|
|
|
|
loop = isLoop;
|
2024-07-22 19:35:34 +08:00
|
|
|
|
_criAtomSource.loop = isLoop;
|
2024-10-12 17:04:17 +08:00
|
|
|
|
//DebugUtil.LogError($"audioID:{audioID}, , name:{_criAtomSource.cueName}, isLoop:{isLoop}");
|
2024-07-22 19:35:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SetIsSpeak(bool isSpeak)
|
|
|
|
|
|
{
|
|
|
|
|
|
_isSpeak = isSpeak;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-16 15:18:33 +08:00
|
|
|
|
public void SetIsStory(bool isStory)
|
|
|
|
|
|
{
|
|
|
|
|
|
_isStory = isStory;
|
|
|
|
|
|
}
|
2024-09-10 14:56:42 +08:00
|
|
|
|
public void SetIsFadeIn(bool isFadeIn)
|
|
|
|
|
|
{
|
|
|
|
|
|
_isFadeIn = isFadeIn;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-22 19:35:34 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2024-07-17 13:59:24 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
}
|