using System.Collections; using System.Collections.Generic; using UnityEngine; namespace Bytes.Sound { [RequireComponent(typeof(AudioSource))] public class SoundPlayer : MonoBehaviour { [SerializeField] private SoundTemplate[] 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 SoundTemplate sound)) { _audioSource.clip = sound.GetRandomClip(); _audioSource.pitch = sound.GetRandomPitch(); _audioSource.volume = ((overrideVolume != -1f) ? overrideVolume : sound.volume) * volumeMultiplier; _audioSource.Play(); } } } }