feat: 解耦音频与触感系统
This commit is contained in:
@@ -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
|
namespace OCES.Audio
|
||||||
{
|
{
|
||||||
public class AudioSystem : MonoBehaviour
|
public partial class AudioSystem : MonoBehaviour
|
||||||
{
|
{
|
||||||
public static AudioSystem Instance { get; private set; }
|
public static AudioSystem Instance { get; private set; }
|
||||||
public bool startWithMusic;
|
public bool startWithMusic;
|
||||||
@@ -29,6 +29,7 @@ namespace OCES.Audio
|
|||||||
AudioGroupConfig m_groups;
|
AudioGroupConfig m_groups;
|
||||||
AudioMixer m_mixer;
|
AudioMixer m_mixer;
|
||||||
Tween m_lowpassTween;
|
Tween m_lowpassTween;
|
||||||
|
partial void OnAwakeComplete();
|
||||||
|
|
||||||
|
|
||||||
// ─────────────────────────────────────────────
|
// ─────────────────────────────────────────────
|
||||||
@@ -348,6 +349,9 @@ namespace OCES.Audio
|
|||||||
Parameters.EnumIds.RegisterAllGameState();
|
Parameters.EnumIds.RegisterAllGameState();
|
||||||
|
|
||||||
ActiveStates = new Dictionary<Type, Enum>();
|
ActiveStates = new Dictionary<Type, Enum>();
|
||||||
|
|
||||||
|
//引入HapticSystem
|
||||||
|
OnAwakeComplete();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ using System;
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using OCES.Haptic;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Audio;
|
using UnityEngine.Audio;
|
||||||
|
|
||||||
@@ -33,6 +32,7 @@ namespace OCES.Audio
|
|||||||
AudioContainerSelector m_containerSelector;
|
AudioContainerSelector m_containerSelector;
|
||||||
PitchStepResolver m_pitchStepResolver;
|
PitchStepResolver m_pitchStepResolver;
|
||||||
VolumeStepResolver m_volumeStepResolver;
|
VolumeStepResolver m_volumeStepResolver;
|
||||||
|
internal Action<ActiveSound> OnSoundStarted, OnSoundStopped;
|
||||||
|
|
||||||
#if UNITY_EDITOR || DEVELOPMENT_BUILD
|
#if UNITY_EDITOR || DEVELOPMENT_BUILD
|
||||||
void Update()
|
void Update()
|
||||||
@@ -435,14 +435,7 @@ namespace OCES.Audio
|
|||||||
IncrementClipCount(activeSound.AudioObject.Id);
|
IncrementClipCount(activeSound.AudioObject.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (activeSound.AudioObject.ContainerType == ContainerType.Blend && activeSound.AudioObject.Haptic > 0)
|
this.OnSoundStarted?.Invoke(activeSound);
|
||||||
{
|
|
||||||
Debug.LogWarning($"[Haptic System] Blend container {activeSound.AudioObject.Id} should not have haptic feedback!");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
TryStartHaptic(activeSound);
|
|
||||||
}
|
|
||||||
|
|
||||||
activeSound.Coroutine = StartCoroutine(RemoveWhenFinished(activeSound));
|
activeSound.Coroutine = StartCoroutine(RemoveWhenFinished(activeSound));
|
||||||
}
|
}
|
||||||
@@ -565,6 +558,7 @@ namespace OCES.Audio
|
|||||||
{
|
{
|
||||||
StopCoroutine(active.Coroutine);
|
StopCoroutine(active.Coroutine);
|
||||||
//Debug.Log($"[StopSound] 协程已终止: {active.AudioObject.Name[0]}");
|
//Debug.Log($"[StopSound] 协程已终止: {active.AudioObject.Name[0]}");
|
||||||
|
this.OnSoundStopped?.Invoke(active);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(active.Source) active.Source.Stop();
|
if(active.Source) active.Source.Stop();
|
||||||
@@ -572,18 +566,6 @@ namespace OCES.Audio
|
|||||||
this.m_activeSounds.Remove(active);
|
this.m_activeSounds.Remove(active);
|
||||||
this.m_pool.ReturnToPool(active.Source.gameObject);
|
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>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user