feat: implement volume step feature
- Add volume step functionality with VolumeStepResolver class - Add VolumeStepThreshold, Volume, and VolumeStep properties to AudioObject - Integrate volume step resolution in SfxSystem for dynamic audio volume control
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace OCES.Audio.HandWritten
|
||||
{
|
||||
public class VolumeStepResolver
|
||||
{
|
||||
readonly Dictionary<uint, int> m_volumeStepCounts = new();
|
||||
readonly Dictionary<uint, double> m_volumeStepLastTime = new();
|
||||
|
||||
internal float ResolveVolume(AudioObject audioObject, double time)
|
||||
{
|
||||
// 计算一下配置的音量是多少
|
||||
float baseVolume = Mathf.Pow(10,audioObject.Volume / 20f);
|
||||
|
||||
// 优化版。看看表现,要是确实Mathf.Pow造成性能卡点了就用这个。
|
||||
//float baseVolume = audioObject.Volume == 0 ? 1f : Mathf.Pow(10,audioObject.Volume / 20f);
|
||||
|
||||
if (audioObject.VolumeStepThreshold == 0) //没配置VolumeStep
|
||||
{
|
||||
return baseVolume;
|
||||
}
|
||||
|
||||
// 超时了,或者没播过
|
||||
if (!this.m_volumeStepLastTime.TryGetValue(audioObject.Id, out double lastVolumeStepTime)
|
||||
|| time - lastVolumeStepTime > audioObject.VolumeStepThreshold)
|
||||
{
|
||||
this.m_volumeStepCounts[audioObject.Id] = 0;
|
||||
this.m_volumeStepLastTime[audioObject.Id] = time;
|
||||
return baseVolume;
|
||||
}
|
||||
|
||||
//命中了
|
||||
int volumeStepCount = this.m_volumeStepCounts[audioObject.Id];
|
||||
volumeStepCount = this.m_volumeStepCounts[audioObject.Id] = volumeStepCount + 1;
|
||||
this.m_volumeStepLastTime[audioObject.Id] = time;
|
||||
return Mathf.Clamp(Mathf.Pow(10, (audioObject.Volume + audioObject.VolumeStep * volumeStepCount) / 20f), 0f, 1f);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user