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

304 lines
8.0 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using System;
2024-04-11 19:02:11 +08:00
using System.Collections.Generic;
2023-08-22 19:08:45 +08:00
using UnityEngine;
2023-10-19 15:00:19 +08:00
using CriWare;
using CriWare.Assets;
2023-10-21 11:55:23 +08:00
using PhxhSDK;
2023-08-22 19:08:45 +08:00
//基于Criware的音频控制类
public class AudioCriware: AudioBase
{
2023-10-19 15:00:19 +08:00
private CriAtomSourceForAsset _criAtomSource;
2024-07-03 12:05:52 +08:00
private CriAtomExPlayback criAtomExPlayback;
2023-10-19 15:00:19 +08:00
private CriAtomAssets _criAtomAssets;
private CriAtomAcbAsset _initAcb;
private CriAtomCueReference _cue;
2024-04-11 19:02:11 +08:00
private int _playID = -1;
2024-04-28 14:24:10 +08:00
private bool _isSpeak;
2024-04-11 19:02:11 +08:00
private GameObject _posGo;
public static Dictionary<CriAtomAcbAsset, CriAtomCueReference> CueReferenceDic = new();
public GameObject PosGo
{
get => _posGo;
set =>_posGo = value;
}
2024-07-03 12:05:52 +08:00
public CriAtomExPlayback CriAtomPlayback => criAtomExPlayback;
public CriAtomAcbAsset AcbAsset => _initAcb;
2024-04-11 19:02:11 +08:00
public int PlayID => _playID;
2023-11-15 18:36:43 +08:00
//private GameObject _rootGo;
2023-10-19 15:00:19 +08:00
private int _cueId = -1;
private static int _cueIDs = 0;
public static int CueIDGen()
{
return ++_cueIDs;
}
2024-04-11 19:02:11 +08:00
public int GenPlayID()
{
return ++_playID;
}
2023-11-15 18:36:43 +08:00
//public GameObject RootGo => _rootGo;
2023-10-19 15:00:19 +08:00
public CriAtomSourceForAsset audioSource
{
get => _criAtomSource;
}
2024-04-11 19:02:11 +08:00
protected AudioCriware(CriAtomAcbAsset acbAsset, bool loop, GameObject posObj, GameObject rootGo, bool is3D,
2024-06-12 13:51:49 +08:00
string cueName, AudioInfo audioInfo): base()
2023-10-19 15:00:19 +08:00
{
2024-04-11 19:02:11 +08:00
if (!posObj)
2023-12-13 15:14:52 +08:00
{
2024-04-11 19:02:11 +08:00
//DebugUtil.LogError("音频对象池内,可取对象为空!!!");
2023-10-19 15:00:19 +08:00
}
2024-04-11 19:02:11 +08:00
_posGo = posObj;
_rootGo = rootGo;
2023-12-13 15:14:52 +08:00
2024-04-11 19:02:11 +08:00
sourceTransform = _rootGo.transform;
persist = audioInfo.persist;
2023-10-19 15:00:19 +08:00
targetVolume = audioInfo.volume;
initTargetVolume = audioInfo.volume;
2024-04-11 19:02:11 +08:00
volume = audioInfo.volume;
2023-10-19 15:00:19 +08:00
tempFadeSeconds = -1;
2024-04-11 19:02:11 +08:00
fadeInSeconds = audioInfo.fadeInValue;
fadeOutSeconds = audioInfo.fadeOutValue;
2023-10-19 15:00:19 +08:00
//playState = PlayState.Stopped;
playState = PlayState.Loading;
//等acb文件加载完成后挂source
2024-04-11 19:02:11 +08:00
//acbAsset.OnLoaded += OnAcbLoaded(acbAsset, cueName, loop, is3D);
//OnAcbLoaded(acbAsset, cueName, loop, is3D);
2023-10-19 15:00:19 +08:00
}
protected virtual void AttachAnalyzer()
2023-10-19 15:00:19 +08:00
{
2024-03-26 12:22:47 +08:00
}
//加载音频文件完成后的操作
protected Action<CriAtomAcbAsset> OnAcbLoaded(CriAtomAcbAsset acbAsset, string cueName, bool isLoop = false, bool is3D = false)
{
CreateAudiosource(acbAsset, isLoop, is3D);
AttachAnalyzer();
2023-10-19 15:00:19 +08:00
if (cueName.Length > 0)
{
PlayByCueName(cueName);
}
else
{
Play();
}
return null;
}
2024-04-11 19:02:11 +08:00
public void Play()
{
base.Play();
GenPlayID();
playState = PlayState.Playing;
}
2023-10-19 15:00:19 +08:00
private bool _GetFreeAudioSource()
2023-08-22 19:08:45 +08:00
{
2023-10-19 15:00:19 +08:00
return false;
}
public void CreateAudiosource(CriAtomAcbAsset acbAsset, bool loop = false, bool is3D = false)
{
2024-04-11 19:02:11 +08:00
_criAtomSource = _rootGo.GetComponent<CriAtomSourceForAsset>();
if (!_criAtomSource)
{
_criAtomSource = _rootGo.AddComponent<CriAtomSourceForAsset>();
}
//CriAtomCueReference cueReference = new CriAtomCueReference(acbAsset, CueIDGen());//CueIDGen());
var cueReference = CueReferenceDic[acbAsset];
2023-10-19 15:00:19 +08:00
_criAtomSource.Cue = cueReference;
_criAtomSource.loop = loop;
_criAtomSource.volume = 1f;
_initAcb = acbAsset;
}
2024-04-11 19:02:11 +08:00
public void Stop()
{
base.Stop();
_criAtomSource.Stop();
}
2023-10-19 15:00:19 +08:00
public override void Play(float volume)
{
if (_criAtomSource == null)
{
CreateAudiosource(_initAcb, loop);
}
2024-07-03 12:05:52 +08:00
criAtomExPlayback = _criAtomSource.Play();
2023-10-19 15:00:19 +08:00
playState = PlayState.Playing;
fadeInterpolater = 0f;
onFadeStartVolume = this.volume;
targetVolume = volume;
2024-04-11 19:02:11 +08:00
GenPlayID();
2023-10-19 15:00:19 +08:00
}
public void PlayByCueName(string cueName, bool isLoop = false)
{
if (_criAtomSource == null)
{
CreateAudiosource(_initAcb, loop);
}
2024-07-03 12:05:52 +08:00
criAtomExPlayback = _criAtomSource.Play(cueName);
2023-10-19 15:00:19 +08:00
playState = PlayState.Playing;
2024-04-23 19:42:31 +08:00
2023-10-19 15:00:19 +08:00
fadeInterpolater = 0f;
onFadeStartVolume = this.volume;
targetVolume = volume;
this.loop = isLoop;
2024-04-11 19:02:11 +08:00
GenPlayID();
2023-10-19 15:00:19 +08:00
}
public override void Play(string cueName, bool isLoop = false)
{
PlayByCueName(cueName, isLoop);
2024-04-11 19:02:11 +08:00
GenPlayID();
2023-10-19 15:00:19 +08:00
}
public override void Pause()
{
_criAtomSource.Pause(true);
playState = PlayState.Paused;
}
//继续播放
public override void Resume()
{
_criAtomSource.Pause(false);
playState = PlayState.Playing;
}
public void SetPitch(float _pitch)
{
if (_criAtomSource != null)
{
_criAtomSource.pitch = _pitch;
}
}
public void SetIs3D(bool is3D)
{
_criAtomSource.use3dPositioning = is3D;
}
2024-04-11 19:02:11 +08:00
public void SetAcbAsset(CriAtomAcbAsset acbAsset)
{
_initAcb = acbAsset;
if (_criAtomSource)
{
_criAtomSource.Cue = CueReferenceDic[acbAsset];
}
}
2024-04-28 14:24:10 +08:00
public void SetIsSpeak(bool isSpeak)
{
_isSpeak = isSpeak;
}
2023-10-19 15:00:19 +08:00
public override void Update()
{
if (!_criAtomSource)
{
return;
}
activated = true;
if (Math.Abs(volume - targetVolume) > 0.01f)
{
float fadeValue;
fadeInterpolater += Time.unscaledDeltaTime;
if (volume > targetVolume)
{
fadeValue = Math.Abs(tempFadeSeconds + 1) > 0.01f ? tempFadeSeconds : fadeOutSeconds;
}
else
{
fadeValue = Math.Abs(tempFadeSeconds + 1) > 0.01f ? tempFadeSeconds : fadeInSeconds;
}
volume = Mathf.Lerp(onFadeStartVolume, targetVolume, fadeInterpolater / fadeValue);
}
else if (Math.Abs(tempFadeSeconds - (-1)) > 0.01)
{
tempFadeSeconds = -1;
}
switch (audioType)
{
case AudioType.Music:
{
_criAtomSource.volume = volume * AudioManager.Instance.GlobalMusicVolume * AudioManager.Instance.GlobalVolume;
break;
}
case AudioType.Sound:
{
2024-04-28 14:24:10 +08:00
if (_isSpeak)
{
_criAtomSource.volume = volume * AudioManager.Instance.GlobalSpeakVolume * AudioManager.Instance.GlobalVolume;
break;
}
2023-10-19 15:00:19 +08:00
_criAtomSource.volume = volume * AudioManager.Instance.GlobalSoundsVolume * AudioManager.Instance.GlobalVolume;
break;
2023-10-19 15:00:19 +08:00
}
}
if (volume == 0f && IsStopped)
{
2024-04-11 19:02:11 +08:00
if (audioType == AudioType.Music)
{
DebugUtil.LogError($"Music Stop ID:{audioID}");
}
2023-10-19 15:00:19 +08:00
_criAtomSource.Stop();
}
2024-04-11 19:02:11 +08:00
if (IsStopped && _criAtomSource.status != CriAtomSourceBase.Status.Stop)
2023-10-19 15:00:19 +08:00
{
2024-04-11 19:02:11 +08:00
_criAtomSource.Stop();
}
// Update playing status
// if (_criAtomSource.status != CriAtomSourceBase.Status.Playing && !Application.isFocused)
// {
2023-10-19 15:00:19 +08:00
//playState = PlayState.Playing;
2024-04-11 19:02:11 +08:00
// if (audioType == AudioType.Music)
// {
// DebugUtil.LogError($"Music Stop ID:{audioID}");
// }
//playState = PlayState.Stopped;
//}
if (_criAtomSource.status == CriAtomSourceBase.Status.Stop && playState != PlayState.Stopped)
{
// if (audioType == AudioType.Music)
// {
// DebugUtil.LogError($"Music Stop ID:{audioID}");
// }
2023-10-19 15:00:19 +08:00
playState = PlayState.Stopped;
}
if (_criAtomSource.status == CriAtomSourceBase.Status.PlayEnd && playState != PlayState.Stopped)
{
playState = PlayState.Stopped;
}
2023-08-22 19:08:45 +08:00
}
}