/// --------------------------------------------- /// Ultimate Character Controller /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.UltimateCharacterController.Audio { using UnityEngine; /// /// The AudioClipSet contains an array of AudioClips. /// [System.Serializable] public class AudioClipSet { [Tooltip("The delay before the AudioClip should be played.")] [SerializeField] protected float m_Delay; [Tooltip("An array of AudioClips which belong to the state.")] [SerializeField] protected AudioClip[] m_AudioClips; public float Delay { get { return m_Delay; } set { m_Delay = value; } } public AudioClip[] AudioClips { get { return m_AudioClips; } set { m_AudioClips = value; } } /// /// Plays the audio clip with a random set index. /// /// The GameObject that is playing the audio clip. /// The AudioSource that is playing the AudioClip (can be null). public AudioSource PlayAudioClip(GameObject gameObject) { return PlayAudioClip(gameObject, -1); } /// /// Plays the audio clip with a random set index. /// /// The GameObject that is playing the audio clip. /// Does the clip loop? /// The AudioSource that is playing the AudioClip (can be null). public AudioSource PlayAudioClip(GameObject gameObject, bool loop) { return PlayAudioClip(gameObject, -1, loop); } /// /// Plays the audio clip with a random set index. /// /// The index of the component that should be played. -1 indicates any component. /// The GameObject that is playing the audio clip. /// The AudioSource that is playing the AudioClip (can be null). public AudioSource PlayAudioClip(GameObject gameObject, int reservedIndex) { return PlayAudioClip(gameObject, reservedIndex, false); } /// /// Plays the audio clip with a random set index. /// /// The index of the component that should be played. -1 indicates any component. /// The GameObject that is playing the audio clip. /// The AudioSource that is playing the AudioClip (can be null). public AudioSource PlayAudioClip(GameObject gameObject, int reservedIndex, bool loop) { var audioClip = GetAudioClip(); if (audioClip == null) { return null; } return AudioManager.Play(gameObject, audioClip, 1, loop, m_Delay, reservedIndex); } /// /// Plays the audio clip at the specified position. /// /// The position that the audio clip should be played at. /// The AudioSource that is playing the AudioClip (can be null). public AudioSource PlayAtPosition(Vector3 position) { var audioClip = GetAudioClip(); if (audioClip == null) { return null; } return AudioManager.PlayAtPosition(audioClip, position); } /// /// Returns the AudioClip that should be played. /// /// An AudioClip selected randomly out of the AudioClips array. private AudioClip GetAudioClip() { if (m_AudioClips == null || m_AudioClips.Length == 0) { return null; } return m_AudioClips[Random.Range(0, m_AudioClips.Length)]; } /// /// Stops playing the audio on the specified GameObject. /// /// The GameObject to stop the audio on. public void Stop(GameObject gameObject) { AudioManager.Stop(gameObject); } /// /// Stops playing the audio on the specified GameObject. /// /// The GameObject to stop the audio on. /// The index of the component that should be stopped. -1 indicates all components. public void Stop(GameObject gameObject, int reservedIndex) { AudioManager.Stop(gameObject, reservedIndex); } } }