using System.Collections; using System.Collections.Generic; using UnityEngine; namespace GatherAndDefend { [RequireComponent(typeof(AudioSource))] public class AudioPlayerComponent : MonoBehaviour { [SerializeField] private AudioTemplate[] playableSounds; private Dictionary _playableSounds; private AudioSource _audioSource; private void Awake() { _audioSource = GetComponent(); _playableSounds = new Dictionary(); foreach (var sound in playableSounds) { _playableSounds.Add(sound.name, sound); } } public void PlaySound(string soundName, float volumeMultiplier = 1f, float overrideVolume = -1f) { if (_playableSounds.TryGetValue(soundName, out AudioTemplate sound)) { _audioSource.clip = sound.GetRandomClip(); _audioSource.pitch = sound.GetRandomPitch(); _audioSource.volume = ((overrideVolume != -1f) ? overrideVolume : sound.GetVolumeToUse()) * volumeMultiplier; _audioSource.Play(); } } } }