feat: 解耦音频与触感系统

This commit is contained in:
2026-05-15 10:09:55 +08:00
parent 53754cd4b8
commit 51ac57d494
4 changed files with 43 additions and 22 deletions
@@ -0,0 +1,32 @@
using OCES.Haptic;
using UnityEngine;
namespace OCES.Audio
{
public partial class AudioSystem
{
partial void OnAwakeComplete()
{
this.m_sfxSystem.OnSoundStarted += activeSound =>
{
uint hapticId = activeSound.AudioObject.Haptic;
if (hapticId == 0) return;
if (activeSound.AudioObject.ContainerType == ContainerType.Blend && activeSound.AudioObject.Haptic > 0)
{
Debug.LogWarning($"[Haptic System] Blend container {activeSound.AudioObject.Id} should not have haptic feedback!");
return;
}
HapticSystem.Instance.Play(hapticId, isDirectCall: false);
};
this.m_sfxSystem.OnSoundStopped += activeSound =>
{
uint hapticId = activeSound.AudioObject.Haptic;
if (hapticId == 0) return;
HapticSystem.Instance.Stop(hapticId);
};
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 122b47fe8b5741869abb7aca0fcc2638
timeCreated: 1778763456
@@ -7,7 +7,7 @@ using DG.Tweening;
namespace OCES.Audio
{
public class AudioSystem : MonoBehaviour
public partial class AudioSystem : MonoBehaviour
{
public static AudioSystem Instance { get; private set; }
public bool startWithMusic;
@@ -29,6 +29,7 @@ namespace OCES.Audio
AudioGroupConfig m_groups;
AudioMixer m_mixer;
Tween m_lowpassTween;
partial void OnAwakeComplete();
// ─────────────────────────────────────────────
@@ -348,6 +349,9 @@ namespace OCES.Audio
Parameters.EnumIds.RegisterAllGameState();
ActiveStates = new Dictionary<Type, Enum>();
//引入HapticSystem
OnAwakeComplete();
}
void Start()
@@ -2,7 +2,6 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using OCES.Haptic;
using UnityEngine;
using UnityEngine.Audio;
@@ -33,6 +32,7 @@ namespace OCES.Audio
AudioContainerSelector m_containerSelector;
PitchStepResolver m_pitchStepResolver;
VolumeStepResolver m_volumeStepResolver;
internal Action<ActiveSound> OnSoundStarted, OnSoundStopped;
#if UNITY_EDITOR || DEVELOPMENT_BUILD
void Update()
@@ -435,14 +435,7 @@ namespace OCES.Audio
IncrementClipCount(activeSound.AudioObject.Id);
}
if (activeSound.AudioObject.ContainerType == ContainerType.Blend && activeSound.AudioObject.Haptic > 0)
{
Debug.LogWarning($"[Haptic System] Blend container {activeSound.AudioObject.Id} should not have haptic feedback!");
}
else
{
TryStartHaptic(activeSound);
}
this.OnSoundStarted?.Invoke(activeSound);
activeSound.Coroutine = StartCoroutine(RemoveWhenFinished(activeSound));
}
@@ -565,6 +558,7 @@ namespace OCES.Audio
{
StopCoroutine(active.Coroutine);
//Debug.Log($"[StopSound] 协程已终止: {active.AudioObject.Name[0]}");
this.OnSoundStopped?.Invoke(active);
}
if(active.Source) active.Source.Stop();
@@ -572,18 +566,6 @@ namespace OCES.Audio
this.m_activeSounds.Remove(active);
this.m_pool.ReturnToPool(active.Source.gameObject);
}
static void TryStartHaptic(ActiveSound active)
{
uint hapticId = active.AudioObject.Haptic;
if (hapticId == 0) return;
HapticSystem.Instance.Play(hapticId, isDirectCall: false);
}
static void TryStopHaptic(uint hapticId)
{
HapticSystem.Instance.Stop(hapticId);
}
}
/// <summary>