5 using System.Collections.Generic;
17 [Tooltip(
"The maximum number of AudioEvents that can be played at once. Zero (0) indicates there is no limit.")]
19 private int globalEventInstanceLimit = 0;
21 [Tooltip(
"The desired behavior when the instance limit is reached.")]
31 public Func<GameObject, GameObject> AudioEmitterTransform {
get;
set; }
36 private Dictionary<string, AudioEvent> eventsDictionary;
41 get {
return instance ?? (instance = FindObjectOfType<UAudioManager>()); }
48 CreateEventsDictionary();
58 PlayEvent(eventName, gameObject);
67 public void PlayEvent(
string eventName, GameObject emitter,
string messageOnAudioEnd = null)
69 PlayEvent(eventName, emitter, null, null, messageOnAudioEnd);
78 public void PlayEvent(
string eventName, AudioSource primarySource, AudioSource secondarySource = null)
80 PlayEvent(eventName, primarySource.gameObject, primarySource, secondarySource);
91 private void PlayEvent(
string eventName, GameObject emitter, AudioSource primarySource, AudioSource secondarySource,
string messageOnAudioEnd = null)
93 if (!CanPlayNewEvent())
98 emitter = ApplyAudioEmitterTransform(emitter);
103 emitter = gameObject;
106 if (
string.IsNullOrEmpty(eventName))
108 Debug.LogWarning(
"Audio Event string is null or empty!");
114 if (!eventsDictionary.TryGetValue(eventName, out currentEvent))
116 Debug.LogFormat(
"Could not find event \"{0}\"", eventName);
121 if (currentEvent.InstanceLimit != 0 && GetInstances(eventName) >= currentEvent.InstanceLimit)
126 Debug.LogFormat(
this,
"Instance limit reached, not playing event \"{0}\"", eventName);
132 KillOldestInstance(eventName);
136 if (primarySource == null)
138 primarySource = GetUnusedAudioSource(emitter);
141 if (currentEvent.IsContinuous() && secondarySource == null)
143 secondarySource = GetUnusedAudioSource(emitter);
146 PlayEvent(currentEvent, emitter, primarySource, secondarySource, messageOnAudioEnd);
159 AudioSource primarySource,
160 AudioSource secondarySource,
161 string messageOnAudioEnd = null)
163 ActiveEvent tempEvent =
new ActiveEvent(audioEvent, emitter, primarySource, secondarySource, messageOnAudioEnd);
166 PlayContainer(tempEvent);
177 for (
int i = ActiveEvents.Count - 1; i >= 0; i--)
183 StopEvent(activeEvent.
AudioEvent.
Name, gameObjectToStop, fadeOutTime);
194 public void StopAllEvents(
string eventName, GameObject emitter = null,
float fadeOutTime = 0f)
197 for (
int i = ActiveEvents.Count - 1; i >= 0; i--)
205 StartCoroutine(StopEventWithFadeCoroutine(activeEvent, fadeOutTime));
219 public void StopAll(GameObject emitter = null,
float fadeOutTime = 0f)
225 StartCoroutine(StopEventWithFadeCoroutine(activeEvent, fadeOutTime));
242 public void StopEvent(
string eventName, GameObject emitter = null,
float fadeOutTime = 0f)
244 emitter = ApplyAudioEmitterTransform(emitter);
248 emitter = gameObject;
251 for (
int i = ActiveEvents.Count - 1; i >= 0; i--)
258 if (fadeOutTime > 0f)
260 StartCoroutine(StopEventWithFadeCoroutine(activeEvent, fadeOutTime));
276 public void SetPitch(
string eventName,
float newPitch)
278 if (newPitch <= 0 || newPitch > 3)
280 Debug.LogErrorFormat(
this,
"Invalid pitch {0} set for event \"{1}\"", newPitch, eventName);
284 for (
int i = ActiveEvents.Count - 1; i >= 0; i--)
303 if (!eventsDictionary.TryGetValue(eventName, out currentEvent))
305 Debug.LogErrorFormat(
this,
"Could not find event \"{0}\"", eventName);
309 if (newLoopTime <= 0)
311 Debug.LogErrorFormat(
this,
"Invalid loop time set for event \"{0}\"", eventName);
315 currentEvent.Container.LoopTime = newLoopTime;
326 emitter = ApplyAudioEmitterTransform(emitter);
333 for (
int i = 0; i < ActiveEvents.Count; i++)
337 if (ActiveEvents[i].
AudioEvent.
Name == eventName && ActiveEvents[i].AudioEmitter == emitter)
352 private AudioSource GetUnusedAudioSource(GameObject emitter,
ActiveEvent currentEvent = null)
356 if (sourcesReference != null)
358 List<AudioSource> sources = sourcesReference.
AudioSources;
359 for (
int s = 0; s < sources.Count; s++)
361 if (!sources[s].isPlaying && !sources[s].enabled)
363 if (currentEvent == null)
367 else if (sources[s] != currentEvent.PrimarySource)
388 private bool CanPlayNewEvent()
390 if (globalEventInstanceLimit == 0 || ActiveEvents.Count < globalEventInstanceLimit)
398 StopEvent(ActiveEvents[0]);
412 private void KillOldestInstance(
string eventName)
414 for (
int i = 0; i < ActiveEvents.Count; i++)
420 StopEvent(tempEvent);
433 private GameObject ApplyAudioEmitterTransform(GameObject emitter)
435 if (AudioEmitterTransform != null)
437 emitter = AudioEmitterTransform(emitter);
448 CreateEventsDictionary();
454 private void CreateEventsDictionary()
457 for(
int i=0; i<LoadedBanks.Count; i++)
459 numEvents += LoadedBanks[i].Events.Length;
462 eventsDictionary =
new Dictionary<string, AudioEvent>(numEvents);
464 for (
int b = 0; b < LoadedBanks.Count; b++)
466 for (
int i = 0; i < LoadedBanks[b].Events.Length; i++)
468 AudioEvent tempEvent = LoadedBanks[b].Events[i];
471 eventsDictionary.Add(tempEvent.
Name, tempEvent);
473 catch (ArgumentException)
475 Debug.LogErrorFormat(
"Name {0} already exists in Event dictionary", tempEvent.
Name);