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