You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.1 KiB
45 lines
1.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class SoundEffectLibrary : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private SoundEffectGroup[] soundEffectGroups;
|
|
private Dictionary<string, List<AudioClip>> soundDictionary;
|
|
|
|
private void Awake()
|
|
{
|
|
InitializeDictionary();
|
|
}
|
|
|
|
private void InitializeDictionary()
|
|
{
|
|
soundDictionary = new Dictionary<string, List<AudioClip>>();
|
|
foreach (SoundEffectGroup soundEffectGroup in soundEffectGroups)
|
|
{
|
|
soundDictionary[soundEffectGroup.name] = soundEffectGroup.audioClips;
|
|
}
|
|
}
|
|
|
|
public AudioClip GetRandomClip(string name)
|
|
{
|
|
if (soundDictionary.ContainsKey(name))
|
|
{
|
|
List<AudioClip> audioClips = soundDictionary[name];
|
|
if(audioClips.Count > 0)
|
|
{
|
|
return audioClips[UnityEngine.Random.Range(0, audioClips.Count)];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
[System.Serializable]
|
|
public struct SoundEffectGroup
|
|
{
|
|
public string name;
|
|
public List<AudioClip> audioClips;
|
|
}
|
|
}
|