feat: implement audio object definitions and refactor audio system

- Add AudioObjectDefinitions.cs with name-to-ID mappings and ambiguity detection
- Update AudioSystem.cs to support Play(uint) and deprecated Play(string) with warnings
- Rename PitchStepManager to PitchStepResolver and update all references
- Refactor generated code to use 'this.' prefix and foreach loops
- Remove TestEnum from audio enums and IDs
- Update SampleScene.unity to use new AudioSystem namespace and rain sound parameter
- Optimize binary serialization in generated audio classes
This commit is contained in:
2026-04-02 14:31:46 +08:00
parent d824d65549
commit 7fc3282e80
18 changed files with 208 additions and 32 deletions
@@ -31,7 +31,7 @@ namespace OCES.Audio
AudioMixerGroup m_voiceGroup;
AudioSourcePool m_pool;
AudioContainerSelector m_containerSelector;
PitchStepManager m_pitchStepManager;
PitchStepResolver m_pitchStepResolver;
#if UNITY_EDITOR
void Update()
@@ -70,7 +70,7 @@ namespace OCES.Audio
this.m_pool = new AudioSourcePool(transform);
this.m_containerSelector = new AudioContainerSelector();
this.m_pitchStepManager = new PitchStepManager();
this.m_pitchStepResolver = new PitchStepResolver();
AudioMixerGroup[] sfx = Find("Master/SFX");
if (sfx.Length > 0) this.m_sfxGroup = sfx[0];
@@ -166,7 +166,7 @@ namespace OCES.Audio
}
// 执行播放
float pitch = this.m_pitchStepManager.ResolvePitch(audioObject, now); //算一下用不用变调
float pitch = this.m_pitchStepResolver.ResolvePitch(audioObject, now); //算一下用不用变调
PlayNewSound(audioObject, pitch);
this.m_lastPlayTime[audioObject.Id] = now;
}
@@ -26,6 +26,13 @@ namespace OCES.Audio
// 公开接口
// ─────────────────────────────────────────────
public void Play(uint audioId)
{
AudioObject obj = this.m_audioObjects.QueryById(audioId);
if (obj != null)
Play(obj);
}
public void Play(AudioObject audioObject)
{
this.m_scheduler.TryPlay(audioObject);
@@ -33,14 +40,36 @@ namespace OCES.Audio
public void Play(int audioId)
{
AudioObject obj = this.m_audioObjects.QueryById((uint)audioId);
if (obj != null)
this.m_scheduler.TryPlay(obj);
Play((uint)audioId);
}
[Obsolete("Use Play(uint) instead")]
public void Play(string audioName)
{
// TODO: 按文件名播放
if (!AudioObjectDefinitions.NameToId.TryGetValue(audioName, out uint id))
{
Debug.LogWarning($"[Audio] Name '{audioName}' not found.");
return;
}
if (AudioObjectDefinitions.AmbiguousNames.Contains(audioName))
{
Debug.LogWarning(
$"[AudioSystem] Name '{audioName}' is ambiguous. Using first matched ID: {id}. " +
"Use ID instead to avoid unexpected behavior."
);
}
if (AudioObjectDefinitions.SharedIdNames.Contains(audioName))
{
Debug.LogWarning(
$"[AudioSystem] Name '{audioName}' is a item of a container AudioObject (ID: {id}). " +
"Use ID instead to avoid unexpected behavior."
);
}
Play(id);
}
public void SetLowpass(bool enable)
@@ -3,7 +3,7 @@ using UnityEngine;
namespace OCES.Audio
{
public class PitchStepManager
public class PitchStepResolver
{
readonly Dictionary<uint, int> m_pitchStepCounts = new();
readonly Dictionary<uint, double> m_pitchStepLastTime = new();