63c5f6766c
- Replace PlayAfterDelay coroutine with AudioSource.PlayDelayed - Add Stop(audioId) public API - Add startWithMusic toggle - Clean up namespace and debug macros
41 lines
1.6 KiB
C#
41 lines
1.6 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace OCES.Audio
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|