feat: Add switch container functionality

This commit is contained in:
2026-04-15 21:05:17 +08:00
parent c3eb094fdc
commit 19faccd312
57 changed files with 782 additions and 75 deletions
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Audio;
@@ -9,7 +10,9 @@ namespace OCES.Audio
public class AudioSystem : MonoBehaviour
{
public static AudioSystem Instance { get; private set; }
// ReSharper disable once MemberCanBePrivate.Global
public IReadOnlyDictionary<Type, Enum> ActiveStates { get; private set; }
const string k_audioConfigPath = "AudioData";
const string k_audioResourcePath = "Audios";
@@ -20,7 +23,7 @@ namespace OCES.Audio
AudioGroupConfig m_groups;
AudioMixer m_mixer;
Tween m_lowpassTween;
// ─────────────────────────────────────────────
// 公开接口
// ─────────────────────────────────────────────
@@ -36,6 +39,16 @@ namespace OCES.Audio
public void Play(AudioObject audioObject, Action onPlay = null)
{
if (audioObject.ContainerType == ContainerType.Switch)
{
audioObject = ResolveSwitchContainer(audioObject);
if (audioObject == null)
{
Debug.Log("[AudioSystem] 无法解析Switch Container,检查配置表!");
return;
}
}
this.m_sfxSystem.TryPlay(audioObject, onPlay);
}
@@ -43,8 +56,7 @@ namespace OCES.Audio
{
Play((uint)audioId);
}
[Obsolete("Use Play(uint) instead")]
public void Play(string audioName)
{
@@ -108,6 +120,7 @@ namespace OCES.Audio
public void SetState<TEnum>(TEnum state) where TEnum : Enum
{
this.m_musicSystem.OnStateChanged(state);
ActiveStates = this.m_musicSystem.ActiveStates;
}
// ─────────────────────────────────────────────
@@ -133,6 +146,7 @@ namespace OCES.Audio
AudioSourcePool sfxPool = new(sfxPoolRoot.transform); // 不传 mixer group,让 SfxSystem 自己设置
this.m_sfxSystem = gameObject.AddComponent<SfxSystem>();
this.m_audioObjects = AudioConfigLoader.Load<AudioObjectConfig>($"{k_audioConfigPath}/AudioObject");
this.m_audioObjects.PreParseSwitchMappings();
this.m_groups = AudioConfigLoader.Load<AudioGroupConfig>($"{k_audioConfigPath}/AudioGroup");
this.m_sfxSystem.Initialize(this.m_groups, this.m_mixer, sfxPool); // 传入 pool
@@ -172,10 +186,38 @@ namespace OCES.Audio
// ── 注册 StateGroup ──
EnumIds.RegisterAllGameState();
ActiveStates = new Dictionary<Type, Enum>();
// ── 启动默认音乐与环境音 ──
// 触发一次初始状态,让音乐系统从默认状态开始匹配
//SetState(GameState.Home);
}
AudioObject ResolveSwitchContainer(AudioObject switchContainer)
{
// 遍历 ActiveStates 找到 TypeId 匹配的枚举类型
Enum currentStateValue = null;
bool foundGroup = false;
foreach (KeyValuePair<Type, Enum> keyValuePair in ActiveStates)
{
if (StateGroupRegistry.GetTypeId(keyValuePair.Key) != switchContainer.SwitchGroupId) continue;
currentStateValue = keyValuePair.Value;
foundGroup = true;
break;
}
if (!foundGroup)
{
Debug.LogWarning($"[AudioSystem] Switch Container {switchContainer.Id} 找不到 TypeId={switchContainer.SwitchGroupId} 对应的状态组。");
return this.m_audioObjects.GetDefaultSwitchOrFallback(switchContainer);
}
// 解析AudioObject对象
AudioObject childContainer = this.m_audioObjects.GetMappingResult(switchContainer.Id, currentStateValue);
return childContainer ?? this.m_audioObjects.GetDefaultSwitchOrFallback(switchContainer);
}
}
public static class AudioConfigLoader
@@ -194,9 +236,9 @@ namespace OCES.Audio
try
{
var config = new T();
using var ms = new MemoryStream(File.ReadAllBytes(path));
using var reader = new BinaryReader(ms);
T config = new T();
using MemoryStream ms = new(File.ReadAllBytes(path));
using BinaryReader reader = new(ms);
config.DeSerialize(reader);
return config;
}