Skip to content

Commit

Permalink
Merge pull request #37 from VirtueSky/dev
Browse files Browse the repository at this point in the history
Update AudioManager.cs
  • Loading branch information
VirtueSky authored Nov 15, 2024
2 parents fa74acb + 4c9359d commit 145038e
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions VirtueSky/Audio/Runtime/AudioManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using VirtueSky.Core;
using VirtueSky.DataType;
Expand Down Expand Up @@ -42,8 +44,7 @@ [Space] [TitleColor("AudioManager Settings", CustomColor.DeepSkyBlue, CustomColo

private SoundComponent music;

[ReadOnly, SerializeField] private DictionaryCustom<int, SoundComponent> dictSfxCache =
new DictionaryCustom<int, SoundComponent>();
[ReadOnly, SerializeField] private List<SoundComponent> listCacheSfx = new List<SoundComponent>();

private int key = 0;

Expand Down Expand Up @@ -100,9 +101,9 @@ void OnMusicVolumeChanged(float volume)

void OnSfxVolumeChanged(float volume)
{
foreach (var cache in dictSfxCache)
for (var i = 0; i < listCacheSfx.Count; i++)
{
cache.Value.Volume = volume;
listCacheSfx[i].Volume = volume;
}
}

Expand All @@ -115,7 +116,7 @@ private SoundCache PlaySfx(SoundData soundData)
if (!soundData.loop) sfxComponent.OnCompleted += OnFinishPlayingAudio;
SoundCache soundCache = GetSoundCache(soundData);
sfxComponent.Key = key;
dictSfxCache.Add(soundCache.key, sfxComponent);
listCacheSfx.Add(sfxComponent);
return soundCache;
}

Expand All @@ -124,10 +125,6 @@ private void StopSfx(SoundCache soundCache)
var soundComponent = GetSoundComponent(soundCache);
if (soundComponent == null) return;
StopAndCleanAudioComponent(soundComponent);
if (dictSfxCache.ContainsKey(soundCache.key))
{
dictSfxCache.Remove(soundCache.key);
}
}

private void PauseSfx(SoundCache soundCache)
Expand All @@ -154,15 +151,14 @@ private void FinishSfx(SoundCache soundCache)

private void StopAllSfx()
{
foreach (var cache in dictSfxCache)
var listTemp = listCacheSfx.ToList();
for (int i = 0; i < listTemp.Count; i++)
{
StopAndCleanAudioComponent(cache.Value);
StopAndCleanAudioComponent(listTemp[i]);
}

if (dictSfxCache.Count > 0)
{
dictSfxCache.Clear();
}
listCacheSfx.Clear();
listTemp.Clear();

key = 0;
}
Expand Down Expand Up @@ -225,7 +221,7 @@ void StopAndCleanAudioComponent(SoundComponent soundComponent)

soundComponent.Stop();
soundComponent.gameObject.DeSpawn();
dictSfxCache.Remove(soundComponent.Key);
if (listCacheSfx.Contains(soundComponent)) listCacheSfx.Remove(soundComponent);
}

void StopAudioMusic(SoundComponent soundComponent)
Expand All @@ -237,13 +233,9 @@ void StopAudioMusic(SoundComponent soundComponent)
SoundComponent GetSoundComponent(SoundCache soundCache)
{
if (soundCache == null) return null;
if (!dictSfxCache.ContainsKey(soundCache.key)) return null;
foreach (var cache in dictSfxCache.GetDict)
for (var i = 0; i < listCacheSfx.Count; i++)
{
if (cache.Key == soundCache.key)
{
return cache.Value;
}
if (soundCache.key == listCacheSfx[i].Key) return listCacheSfx[i];
}

return null;
Expand Down

0 comments on commit 145038e

Please sign in to comment.