using UnityEngine; namespace ActionGameFramework.Audio { /// /// A helper for playing random audio clips /// The randomness is not uniform but rather based on weights /// [RequireComponent(typeof(AudioSource))] public class RandomAudioSource : MonoBehaviour { /// /// A weighted list of audio clips /// public WeightedAudioList clips; /// /// Configuration for playing a sound randomly on awake /// public bool playOnEnabled; /// /// The attached audio source /// protected AudioSource m_Source; /// /// Cache the audio source and play if necessary /// protected virtual void OnEnable() { if (m_Source == null) { m_Source = GetComponent(); } if (playOnEnabled) { PlayRandomClip(); } } /// /// Plays the random clip using the attached audio source /// public virtual void PlayRandomClip() { if (m_Source == null) { m_Source = GetComponent(); } PlayRandomClip(m_Source); } /// /// Plays the random clip using a specified audio source /// /// Audio source to use public virtual void PlayRandomClip(AudioSource source) { if (source == null) { Debug.LogError("[RANDOM AUDIO SOURCE] Missing audio source"); return; } AudioClip clip = clips.WeightedSelection(); if (clip == null) { Debug.LogError("[RANDOM AUDIO SOURCE] Missing audio clips"); return; } //source.clip = clip; //source.Play(); } } }