[fix]修复音频对象池销毁bug

This commit is contained in:
陈思海 2026-04-23 19:11:50 +08:00
parent aa37eecf8b
commit ac15608019

View File

@ -50,6 +50,7 @@ namespace AlicizaX.Audio.Runtime
private bool _enable = true; private bool _enable = true;
private bool _unityAudioDisabled; private bool _unityAudioDisabled;
private bool _initialized; private bool _initialized;
private bool _isShuttingDown;
private bool _ownsInstanceRoot; private bool _ownsInstanceRoot;
public AudioMixer AudioMixer => _audioMixer; public AudioMixer AudioMixer => _audioMixer;
@ -96,65 +97,6 @@ namespace AlicizaX.Audio.Runtime
} }
} }
internal float MusicVolume
{
get => GetCategoryVolume(AudioType.Music);
set => SetCategoryVolume(AudioType.Music, value);
}
internal float SoundVolume
{
get => GetCategoryVolume(AudioType.Sound);
set => SetCategoryVolume(AudioType.Sound, value);
}
internal float UISoundVolume
{
get => GetCategoryVolume(AudioType.UISound);
set => SetCategoryVolume(AudioType.UISound, value);
}
internal float VoiceVolume
{
get => GetCategoryVolume(AudioType.Voice);
set => SetCategoryVolume(AudioType.Voice, value);
}
internal float AmbientVolume
{
get => GetCategoryVolume(AudioType.Ambient);
set => SetCategoryVolume(AudioType.Ambient, value);
}
internal bool MusicEnable
{
get => GetCategoryEnable(AudioType.Music);
set => SetCategoryEnable(AudioType.Music, value);
}
internal bool SoundEnable
{
get => GetCategoryEnable(AudioType.Sound);
set => SetCategoryEnable(AudioType.Sound, value);
}
internal bool UISoundEnable
{
get => GetCategoryEnable(AudioType.UISound);
set => SetCategoryEnable(AudioType.UISound, value);
}
internal bool VoiceEnable
{
get => GetCategoryEnable(AudioType.Voice);
set => SetCategoryEnable(AudioType.Voice, value);
}
internal bool AmbientEnable
{
get => GetCategoryEnable(AudioType.Ambient);
set => SetCategoryEnable(AudioType.Ambient, value);
}
protected override void OnInitialize() { } protected override void OnInitialize() { }
@ -563,6 +505,17 @@ namespace AlicizaX.Audio.Runtime
sourceObject.Source.transform.SetParent(_instanceRoot, false); sourceObject.Source.transform.SetParent(_instanceRoot, false);
} }
if (_sourcePool == null)
{
return;
}
//在服务拆卸期间 对象池可能已经释放了包装器 在音频完成释放自身引用之前 清除ObjectBase.Target
if (_isShuttingDown && sourceObject.Target == null)
{
return;
}
_sourcePool.Unspawn(sourceObject); _sourcePool.Unspawn(sourceObject);
} }
@ -1177,6 +1130,7 @@ namespace AlicizaX.Audio.Runtime
private void Shutdown(bool destroyRoot) private void Shutdown(bool destroyRoot)
{ {
_isShuttingDown = true;
StopAll(false); StopAll(false);
for (int i = 0; i < _categories.Length; i++) for (int i = 0; i < _categories.Length; i++)
@ -1213,6 +1167,8 @@ namespace AlicizaX.Audio.Runtime
{ {
DestroyOwnedRoot(); DestroyOwnedRoot();
} }
_isShuttingDown = false;
} }
private void DestroyOwnedRoot() private void DestroyOwnedRoot()