2026-03-31 17:25:20 +08:00
|
|
|
using AlicizaX;
|
2025-09-05 19:46:30 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Audio;
|
|
|
|
|
|
|
|
|
|
namespace AlicizaX.Audio.Runtime
|
|
|
|
|
{
|
|
|
|
|
[DisallowMultipleComponent]
|
|
|
|
|
[AddComponentMenu("Game Framework/Audio")]
|
|
|
|
|
public sealed class AudioComponent : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
[SerializeField] private AudioMixer m_AudioMixer;
|
2026-04-23 17:21:36 +08:00
|
|
|
[SerializeField] private Transform m_InstanceRoot;
|
|
|
|
|
[SerializeField] private AudioListener m_AudioListener;
|
|
|
|
|
[SerializeField] private AudioGroupConfigCollection m_AudioGroupConfigs;
|
2025-09-05 19:46:30 +08:00
|
|
|
|
2026-03-26 16:14:05 +08:00
|
|
|
private IAudioService _audioService;
|
2025-09-05 19:46:30 +08:00
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
2026-04-20 13:46:44 +08:00
|
|
|
_audioService = AppServices.RegisterApp(new AudioService());
|
2025-09-05 19:46:30 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-23 17:21:36 +08:00
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
EnsureInstanceRoot();
|
|
|
|
|
EnsureAudioMixer();
|
|
|
|
|
AudioGroupConfig[] configs = m_AudioGroupConfigs != null ? m_AudioGroupConfigs.GroupConfigs : null;
|
|
|
|
|
_audioService.Initialize(configs, m_InstanceRoot, m_AudioMixer);
|
|
|
|
|
if (m_AudioListener != null)
|
|
|
|
|
{
|
|
|
|
|
_audioService.RegisterListener(m_AudioListener);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnEnable()
|
2025-09-05 19:46:30 +08:00
|
|
|
{
|
2026-04-23 17:21:36 +08:00
|
|
|
if (_audioService != null && m_AudioListener != null)
|
2025-09-05 19:46:30 +08:00
|
|
|
{
|
2026-04-23 17:21:36 +08:00
|
|
|
_audioService.RegisterListener(m_AudioListener);
|
2025-09-05 19:46:30 +08:00
|
|
|
}
|
2026-04-23 17:21:36 +08:00
|
|
|
}
|
2025-09-05 19:46:30 +08:00
|
|
|
|
2026-04-23 17:21:36 +08:00
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
if (_audioService != null && m_AudioListener != null)
|
|
|
|
|
{
|
|
|
|
|
_audioService.UnregisterListener(m_AudioListener);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EnsureInstanceRoot()
|
|
|
|
|
{
|
|
|
|
|
if (m_InstanceRoot != null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_InstanceRoot = new GameObject("[AudioService Instances]").transform;
|
|
|
|
|
m_InstanceRoot.SetParent(transform, false);
|
|
|
|
|
m_InstanceRoot.localScale = Vector3.one;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EnsureAudioMixer()
|
|
|
|
|
{
|
2025-09-05 19:46:30 +08:00
|
|
|
if (m_AudioMixer == null)
|
|
|
|
|
{
|
|
|
|
|
m_AudioMixer = Resources.Load<AudioMixer>("AudioMixer");
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-23 17:21:36 +08:00
|
|
|
|
2025-09-05 19:46:30 +08:00
|
|
|
}
|
|
|
|
|
}
|