52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(AudioSource))]
|
|
public class RandomizeAudioClipFromList : MonoBehaviour
|
|
{
|
|
[SerializeField] private List<AudioClip> possibleClips = new List<AudioClip>();
|
|
[SerializeField] private bool playOnStart = false;
|
|
[Space]
|
|
[SerializeField] private bool useDelay = false;
|
|
[SerializeField] private float delay = 0.2f;
|
|
[Space]
|
|
[SerializeField] private float pitchVariation = 0.05f;
|
|
[SerializeField] private bool usePitchVariation = false;
|
|
|
|
AudioSource audioSource;
|
|
|
|
int chosenIndex;
|
|
|
|
private void Awake()
|
|
{
|
|
audioSource = GetComponent<AudioSource>();
|
|
|
|
chosenIndex = Random.Range(0, possibleClips.Count);
|
|
|
|
audioSource.clip = possibleClips[chosenIndex];
|
|
|
|
if (usePitchVariation)
|
|
audioSource.pitch = 1f + (Random.Range(-pitchVariation, +pitchVariation));
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
IEnumerator Start()
|
|
{
|
|
if (playOnStart)
|
|
{
|
|
if(useDelay)
|
|
{
|
|
yield return new WaitForSeconds(delay);
|
|
}
|
|
Play();
|
|
}
|
|
yield return null;
|
|
}
|
|
|
|
public void Play()
|
|
{
|
|
audioSource.Play();
|
|
}
|
|
}
|