things
This commit is contained in:
parent
a17810ffeb
commit
4e91f448c9
826 changed files with 66 additions and 8 deletions
61
Assets/Scripts/Singletons/SoundManager.cs
Normal file
61
Assets/Scripts/Singletons/SoundManager.cs
Normal file
|
@ -0,0 +1,61 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
[Serializable]
|
||||
public class SoundEntry
|
||||
{
|
||||
public string Key;
|
||||
public AudioClip Value;
|
||||
}
|
||||
|
||||
public class SoundManager : MonoBehaviourSingletonPersistent<SoundManager>
|
||||
{
|
||||
// Audio players components.
|
||||
public AudioSource EffectsSource;
|
||||
public AudioSource MusicSource;
|
||||
|
||||
public List<SoundEntry> SoundsList;
|
||||
private Dictionary<string, AudioClip> soundsDictionary;
|
||||
|
||||
|
||||
new void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
// Convert the List to a Dictionary at runtime for easier access
|
||||
soundsDictionary = new Dictionary<string, AudioClip>();
|
||||
foreach (var entry in SoundsList)
|
||||
{
|
||||
if (!soundsDictionary.ContainsKey(entry.Key))
|
||||
{
|
||||
soundsDictionary.Add(entry.Key, entry.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private AudioClip GetSound(string key)
|
||||
{
|
||||
print(soundsDictionary.Count);
|
||||
AudioClip clip = soundsDictionary.TryGetValue(key, out var value) ? value : null;
|
||||
if (clip == null)
|
||||
{
|
||||
Debug.LogError($"Sound {key} not found");
|
||||
}
|
||||
return clip;
|
||||
}
|
||||
|
||||
public void Play(string keyName)
|
||||
{
|
||||
EffectsSource.clip = GetSound(keyName);
|
||||
EffectsSource.Play();
|
||||
}
|
||||
|
||||
public void PlayMusic(string keyName)
|
||||
{
|
||||
MusicSource.clip = GetSound(keyName);
|
||||
MusicSource.Play();
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue