| | 1 | | using System.Collections; |
| | 2 | | using UnityEngine; |
| | 3 | | using ReorderableList; |
| | 4 | |
|
| | 5 | | [System.Serializable, CreateAssetMenu(fileName = "AudioEvent", menuName = "AudioEvents/AudioEvent")] |
| | 6 | | public class AudioEvent : ScriptableObject |
| | 7 | | { |
| | 8 | | private static bool VERBOSE = false; |
| | 9 | |
|
| | 10 | | [System.Serializable] |
| | 11 | | public class AudioClipList : ReorderableArray<AudioClip> { } |
| | 12 | |
|
| | 13 | | public bool loop = false; |
| | 14 | |
|
| | 15 | | [Range(0f, 1f)] |
| 1904 | 16 | | public float initialVolume = 1.0f; |
| | 17 | |
|
| 1904 | 18 | | public float initialPitch = 1f; |
| | 19 | |
|
| | 20 | | [Range(0f, 1f)] |
| | 21 | | public float randomPitch = 0.0f; |
| | 22 | |
|
| | 23 | | public float cooldownSeconds = 0.0f; |
| | 24 | |
|
| | 25 | | [Reorderable] |
| | 26 | | public AudioClipList clips; |
| | 27 | |
|
| | 28 | | [Space(25)] |
| | 29 | | public AudioEvent stopEventOnPlay; |
| | 30 | |
|
| | 31 | | [HideInInspector] |
| | 32 | | public AudioSource source; |
| | 33 | |
|
| | 34 | | private int clipIndex, lastPlayedIndex; |
| | 35 | | protected float pitch; |
| | 36 | | private float lastPlayedTime, nextAvailablePlayTime; // Used for cooldown |
| | 37 | | private Coroutine fadeInCoroutine, fadeOutCoroutine; |
| | 38 | |
|
| | 39 | | [HideInInspector] public event System.Action OnPlay, OnStop, OnFadedIn, OnFadedOut; |
| | 40 | |
|
| | 41 | | public virtual void Initialize(AudioContainer audioContainer) |
| | 42 | | { |
| 2234 | 43 | | if (audioContainer == null) |
| 0 | 44 | | return; |
| | 45 | |
|
| 2234 | 46 | | pitch = initialPitch; |
| 2234 | 47 | | lastPlayedTime = 0f; |
| 2234 | 48 | | nextAvailablePlayTime = 0f; |
| 2234 | 49 | | lastPlayedIndex = -1; |
| 2234 | 50 | | RandomizeIndex(); |
| | 51 | |
|
| | 52 | | // Add AudioSource component for event |
| 2234 | 53 | | source = audioContainer.gameObject.AddComponent(typeof(AudioSource)) as AudioSource; |
| | 54 | |
|
| 2234 | 55 | | if (clips.Length == 0) |
| | 56 | | { |
| 0 | 57 | | Debug.LogWarning("There are no clips in the audio event '" + name + "' (" + audioContainer.name + ")"); |
| 0 | 58 | | } |
| | 59 | | else |
| | 60 | | { |
| 2234 | 61 | | source.clip = clips[0]; |
| | 62 | | } |
| | 63 | |
|
| 2234 | 64 | | source.volume = initialVolume; |
| 2234 | 65 | | source.loop = loop; |
| 2234 | 66 | | source.playOnAwake = false; |
| | 67 | |
|
| 2234 | 68 | | source.outputAudioMixerGroup = audioContainer.audioMixerGroup; |
| 2234 | 69 | | source.spatialBlend = audioContainer.spatialBlend; |
| 2234 | 70 | | source.dopplerLevel = audioContainer.dopplerLevel; |
| 2234 | 71 | | source.minDistance = audioContainer.minDistance; |
| 2234 | 72 | | source.maxDistance = audioContainer.maxDistance; |
| 2234 | 73 | | } |
| | 74 | |
|
| 5346 | 75 | | public void RandomizeIndex() { RandomizeIndex(0, clips.Length); } |
| | 76 | |
|
| | 77 | | // Randomize the index from (inclusive) to y (exclusive) |
| | 78 | | public void RandomizeIndex(int from, int to) |
| | 79 | | { |
| | 80 | | int newIndex; |
| | 81 | | do |
| | 82 | | { |
| 2797 | 83 | | newIndex = Random.Range(from, to); |
| 2797 | 84 | | } while (clips.Length > 1 && newIndex == lastPlayedIndex); |
| | 85 | |
|
| 2673 | 86 | | clipIndex = newIndex; |
| 2673 | 87 | | } |
| | 88 | |
|
| | 89 | | public virtual void Play(bool oneShot = false) |
| | 90 | | { |
| 405 | 91 | | if (stopEventOnPlay != null) |
| 0 | 92 | | stopEventOnPlay.Stop(); |
| | 93 | |
|
| 405 | 94 | | if (source == null) |
| | 95 | | { |
| 54 | 96 | | if (VERBOSE) |
| 0 | 97 | | Debug.Log($"AudioEvent: Tried to play {name} with source equal to null."); |
| 54 | 98 | | return; |
| | 99 | | } |
| | 100 | |
|
| 351 | 101 | | if (source.clip == null) |
| | 102 | | { |
| 0 | 103 | | if (VERBOSE) |
| 0 | 104 | | Debug.Log($"AudioEvent: Tried to play {name} with audioClip equal to null."); |
| 0 | 105 | | return; |
| | 106 | | } |
| | 107 | |
|
| | 108 | | // Check if AudioSource is active and check cooldown time |
| 351 | 109 | | if (!source.gameObject.activeSelf) |
| | 110 | | { |
| 0 | 111 | | return; |
| | 112 | | } |
| | 113 | |
|
| 351 | 114 | | if (Time.time < nextAvailablePlayTime) |
| | 115 | | { |
| 17 | 116 | | return; |
| | 117 | | } |
| | 118 | |
|
| 334 | 119 | | source.clip = clips[clipIndex]; |
| 334 | 120 | | source.pitch = pitch + Random.Range(0f, randomPitch) - (randomPitch * 0.5f); |
| | 121 | |
|
| | 122 | | // Play |
| 334 | 123 | | if (oneShot) |
| 332 | 124 | | source.PlayOneShot(source.clip); |
| | 125 | | else |
| 2 | 126 | | source.Play(); |
| | 127 | |
|
| 334 | 128 | | lastPlayedIndex = clipIndex; |
| 334 | 129 | | RandomizeIndex(); |
| | 130 | |
|
| 334 | 131 | | lastPlayedTime = Time.time; |
| 334 | 132 | | nextAvailablePlayTime = lastPlayedTime + cooldownSeconds; |
| | 133 | |
|
| 334 | 134 | | OnPlay?.Invoke(); |
| 0 | 135 | | } |
| | 136 | |
|
| | 137 | | public void PlayScheduled(float delaySeconds) |
| | 138 | | { |
| 117 | 139 | | if (source == null) |
| 0 | 140 | | return; |
| | 141 | |
|
| | 142 | | // Check if AudioSource is active and check cooldown time (taking delay into account) |
| 117 | 143 | | if (!source.gameObject.activeSelf || Time.time + delaySeconds < nextAvailablePlayTime) |
| 12 | 144 | | return; |
| | 145 | |
|
| 105 | 146 | | source.clip = clips[clipIndex]; |
| 105 | 147 | | source.pitch = pitch + Random.Range(0f, randomPitch) - (randomPitch * 0.5f); |
| 105 | 148 | | source.PlayScheduled(AudioSettings.dspTime + delaySeconds); |
| | 149 | |
|
| 105 | 150 | | lastPlayedIndex = clipIndex; |
| 105 | 151 | | RandomizeIndex(); |
| | 152 | |
|
| 105 | 153 | | lastPlayedTime = Time.time + delaySeconds; |
| 105 | 154 | | nextAvailablePlayTime = lastPlayedTime + cooldownSeconds; |
| | 155 | |
|
| 105 | 156 | | OnPlay?.Invoke(); |
| 0 | 157 | | } |
| | 158 | |
|
| | 159 | | public void Stop() |
| | 160 | | { |
| 0 | 161 | | source.Stop(); |
| 0 | 162 | | OnStop?.Invoke(); |
| 0 | 163 | | } |
| | 164 | |
|
| 0 | 165 | | public void ResetVolume() { source.volume = initialVolume; } |
| | 166 | |
|
| 0 | 167 | | public void SetIndex(int index) { clipIndex = index; } |
| | 168 | |
|
| 0 | 169 | | public void SetPitch(float pitch) { this.pitch = pitch; } |
| | 170 | |
|
| | 171 | | /// <summary>Use StartCoroutine() on this one.</summary> |
| | 172 | | public IEnumerator FadeIn(float fadeSeconds) |
| | 173 | | { |
| 0 | 174 | | float startVolume = source.volume; |
| 0 | 175 | | while (source.volume < initialVolume) |
| | 176 | | { |
| 0 | 177 | | source.volume += (initialVolume - startVolume) * (Time.unscaledDeltaTime / fadeSeconds); |
| 0 | 178 | | yield return null; |
| | 179 | | } |
| | 180 | |
|
| 0 | 181 | | source.volume = initialVolume; |
| 0 | 182 | | OnFadedIn?.Invoke(); |
| 0 | 183 | | } |
| | 184 | |
|
| | 185 | | /// <summary>Use StartCoroutine() on this one.</summary> |
| | 186 | | public IEnumerator FadeOut(float fadeSeconds, bool stopWhenDone = true) |
| | 187 | | { |
| 0 | 188 | | float startVolume = source.volume; |
| 0 | 189 | | while (source.volume > 0) |
| | 190 | | { |
| 0 | 191 | | source.volume -= startVolume * (Time.unscaledDeltaTime / fadeSeconds); |
| 0 | 192 | | yield return null; |
| | 193 | | } |
| | 194 | |
|
| 0 | 195 | | if (stopWhenDone) |
| | 196 | | { |
| 0 | 197 | | Stop(); |
| 0 | 198 | | source.volume = initialVolume; |
| | 199 | | } |
| | 200 | |
|
| 0 | 201 | | OnFadedOut?.Invoke(); |
| 0 | 202 | | } |
| | 203 | | } |