341 lines
7.5 KiB
C#
341 lines
7.5 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 int _playID;
|
|
|
|
public CriAtomExPlayback CriAtomExPlayback;
|
|
public bool IsSpeak => _isSpeak;
|
|
|
|
private float _playPassTime = 0f;
|
|
private float _fadeValue = 1f;
|
|
private float _fadeTime = 4f;
|
|
private bool _isFadeIn;
|
|
private bool _isFadeOut;
|
|
|
|
#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();
|
|
}
|
|
|
|
public void PlayWithFade(float volume = 1f)
|
|
{
|
|
playState = PlayState.Playing;
|
|
//_criAtomSource.player?.AttachFader();
|
|
_criAtomSource.Play();
|
|
}
|
|
public void Stop()
|
|
{
|
|
_isFadeOut = false;
|
|
_playPassTime = 0;
|
|
if (_criAtomSource.status == CriAtomSourceBase.Status.Stop && playState == PlayState.Stopped)
|
|
{
|
|
return;
|
|
}
|
|
_criAtomSource.Stop();
|
|
//_criAtomSource.player?.DetachFader();
|
|
base.Stop();
|
|
}
|
|
|
|
public void StopWithFade()
|
|
{
|
|
_isFadeOut = true;
|
|
_playPassTime = 0f;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Utils
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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)
|
|
{
|
|
_criAtomSource.loop = isLoop;
|
|
}
|
|
|
|
public void SetIsSpeak(bool isSpeak)
|
|
{
|
|
_isSpeak = isSpeak;
|
|
}
|
|
|
|
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 = AudioCriManager.Instance.CriAtom.GetCueSheetInternal(cueSheet).acb;
|
|
if (acb.GetCueInfo(cueName, out var cueInfo))
|
|
{
|
|
var length = cueInfo.length / 1000;
|
|
return length;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
#endregion
|
|
} |