/// <summary> /// Get an available AudioSource. /// </summary> /// <param name="emitter">The audio emitter on which the AudioSource is desired.</param> /// <param name="currentEvent">The current audio event.</param> /// <returns></returns> private AudioSource GetUnusedAudioSource(GameObject emitter, ActiveEvent currentEvent = null) { // Get or create valid AudioSource. AudioSourcesReference sourcesReference = emitter.GetComponent <AudioSourcesReference>(); if (sourcesReference != null) { List <AudioSource> sources = sourcesReference.AudioSources; for (int s = 0; s < sources.Count; s++) { if (!sources[s].isPlaying && !sources[s].enabled) { if (currentEvent == null) { return(sources[s]); } else if (sources[s] != currentEvent.PrimarySource) { return(sources[s]); } } } } else { sourcesReference = emitter.AddComponent <AudioSourcesReference>(); } return(sourcesReference.AddNewAudioSource()); }
public void Initialize(AudioEvent audioEvent, GameObject emitter, AudioSource primarySource = null, AudioSource secondarySource = null) { this.audioEvent = audioEvent; AudioEmitter = emitter; audioSourcesReference = AudioEmitter.GetComponent <AudioSourcesReference>(); if (audioSourcesReference == null) { audioSourcesReference = AudioEmitter.AddComponent <AudioSourcesReference>(); } SetEventProperties(); this.PrimarySource = primarySource; this.SecondarySource = secondarySource; initialized = true; }