60 lines
2.1 KiB
C#
60 lines
2.1 KiB
C#
// Assets/Scripts/Audio/UnderwaterAudioController.cs
|
||
// 进入 LiquidZone 时切换水下 DSP 处理(Architecture 21_LiquidPuzzleModule §3.4)
|
||
using UnityEngine;
|
||
using UnityEngine.Audio;
|
||
using BaseGames.Core.Events;
|
||
using BaseGames.World.Liquid;
|
||
|
||
namespace BaseGames.Audio
|
||
{
|
||
/// <summary>
|
||
/// 挂载于 PlayerController 所在 GameObject。
|
||
/// 订阅 EVT_LiquidEntered / EVT_LiquidExited 事件频道(与 WaterDangerState、UnderwaterPostProcessingController 一致)。
|
||
/// 切换 AudioMixer Snapshot 以应用/解除水下 DSP 处理。
|
||
/// 仅响应 Water 类型液体;Acid / Lava 不切换水下音频。
|
||
/// </summary>
|
||
public class UnderwaterAudioController : MonoBehaviour
|
||
{
|
||
[SerializeField] private AudioMixer _mixer;
|
||
[SerializeField] private float _transitionDuration = 0.3f;
|
||
|
||
[Header("Event Channels")]
|
||
[SerializeField] private LiquidEventChannelSO _onLiquidEntered; // EVT_LiquidEntered
|
||
[SerializeField] private LiquidEventChannelSO _onLiquidExited; // EVT_LiquidExited
|
||
|
||
private readonly CompositeDisposable _subs = new();
|
||
|
||
private void OnEnable()
|
||
{
|
||
_onLiquidEntered?.Subscribe(OnLiquidEntered).AddTo(_subs);
|
||
_onLiquidExited?.Subscribe(OnLiquidExited).AddTo(_subs);
|
||
}
|
||
|
||
private void OnDisable() => _subs.Clear();
|
||
|
||
private void OnLiquidEntered(LiquidEvent evt)
|
||
{
|
||
if (evt.LiquidType == nameof(LiquidType.Water))
|
||
EnterWater();
|
||
}
|
||
|
||
private void OnLiquidExited(LiquidEvent evt)
|
||
{
|
||
if (evt.LiquidType == nameof(LiquidType.Water))
|
||
ExitWater();
|
||
}
|
||
|
||
/// <summary>切换至水下 AudioMixer Snapshot。</summary>
|
||
public void EnterWater()
|
||
{
|
||
_mixer?.FindSnapshot("Underwater")?.TransitionTo(_transitionDuration);
|
||
}
|
||
|
||
/// <summary>切换回默认 AudioMixer Snapshot。</summary>
|
||
public void ExitWater()
|
||
{
|
||
_mixer?.FindSnapshot("Default")?.TransitionTo(_transitionDuration);
|
||
}
|
||
}
|
||
}
|