Sound + somewhat game loop
This commit is contained in:
parent
29e5e48aee
commit
1d6a879755
33 changed files with 1572 additions and 595 deletions
|
@ -1,36 +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 Dictionary<string, AudioClip> Sounds;
|
||||
|
||||
// Play a single clip through the sound effects source.
|
||||
public void Play(AudioClip clip)
|
||||
|
||||
public List<SoundEntry> SoundsList;
|
||||
private Dictionary<string, AudioClip> soundsDictionary;
|
||||
|
||||
|
||||
new void Awake()
|
||||
{
|
||||
EffectsSource.clip = clip;
|
||||
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();
|
||||
}
|
||||
|
||||
// Play a single clip through the music source.
|
||||
public void PlayMusic(AudioClip clip)
|
||||
public void PlayMusic(string keyName)
|
||||
{
|
||||
MusicSource.clip = clip;
|
||||
MusicSource.clip = GetSound(keyName);
|
||||
MusicSource.Play();
|
||||
}
|
||||
|
||||
// Play a random clip from an array, and randomize the pitch slightly.
|
||||
public void RandomSoundEffect(params AudioClip[] clips)
|
||||
{
|
||||
int randomIndex = Random.Range(0, clips.Length);
|
||||
|
||||
EffectsSource.clip = clips[randomIndex];
|
||||
EffectsSource.Play();
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue