The AudioSourcesReference class encapsulates a cache of references to audio source components on a given local audio emitter game object. Used primarily by UAudioManager, it improves performance by bypassing having to requery for list of attached components on each use.
Inheritance: UnityEngine.MonoBehaviour
        /// <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());
        }
Exemplo n.º 2
0
        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;
        }