| | | 1 | | using UnityEngine; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Play a simple sounds using Play one shot with volume, and pitch |
| | | 5 | | /// </summary> |
| | | 6 | | [RequireComponent(typeof(AudioSource))] |
| | | 7 | | public class PlayQuickSound : MonoBehaviour |
| | | 8 | | { |
| | | 9 | | [Tooltip("The sound that is played")] |
| | 15 | 10 | | public AudioClip sound = null; |
| | | 11 | | |
| | | 12 | | [Tooltip("The volume of the sound")] |
| | 15 | 13 | | public float volume = 1.0f; |
| | | 14 | | |
| | | 15 | | [Tooltip("The range of pitch the sound is played at (-pitch, pitch)")] |
| | 15 | 16 | | [Range(0, 1)] public float randomPitchVariance = 0.0f; |
| | | 17 | | |
| | 15 | 18 | | private AudioSource audioSource = null; |
| | | 19 | | |
| | 15 | 20 | | private float defaultPitch = 1.0f; |
| | | 21 | | |
| | | 22 | | private void Awake() |
| | 12 | 23 | | { |
| | 12 | 24 | | audioSource = GetComponent<AudioSource>(); |
| | 12 | 25 | | } |
| | | 26 | | |
| | | 27 | | public void Play() |
| | 6 | 28 | | { |
| | 6 | 29 | | float randomVariance = Random.Range(-randomPitchVariance, randomPitchVariance); |
| | 6 | 30 | | randomVariance += defaultPitch; |
| | | 31 | | |
| | 6 | 32 | | audioSource.pitch = randomVariance; |
| | 6 | 33 | | audioSource.PlayOneShot(sound, volume); |
| | 6 | 34 | | audioSource.pitch = defaultPitch; |
| | 6 | 35 | | } |
| | | 36 | | |
| | | 37 | | private void OnValidate() |
| | 15 | 38 | | { |
| | 15 | 39 | | AudioSource audioSource = GetComponent<AudioSource>(); |
| | 15 | 40 | | audioSource.playOnAwake = false; |
| | 15 | 41 | | } |
| | | 42 | | } |