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 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()
|
|
|
|
|
{
|
2026-04-24 20:50:13 +08:00
|
|
|
if (m_AudioMixer == null)
|
|
|
|
|
{
|
|
|
|
|
throw new GameFrameworkException("AudioMixer is not assigned. Please assign an AudioMixer in the inspector.");
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 17:21:36 +08:00
|
|
|
AudioGroupConfig[] configs = m_AudioGroupConfigs != null ? m_AudioGroupConfigs.GroupConfigs : null;
|
2026-04-24 20:50:13 +08:00
|
|
|
_audioService.Initialize(configs, transform, m_AudioMixer);
|
2026-04-23 17:21:36 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-05 19:46:30 +08:00
|
|
|
}
|
|
|
|
|
}
|