private IEnumerator ReinitializeFadeOnLoop(AudioEffectDefinition definition)
        {
            yield return(new WaitForSeconds(audioSource.clip.length));

            StopAllCoroutines();
            FadeAudioIfNeeded(definition);
        }
Example #2
0
        public AudioPlayerBehavior Play(AudioEffectDefinition definition, Vector3 location,
                                        AudioPriority priority = AudioPriority.Normal)
        {
            var currentTime = Time.time;

            if ((currentTime - previousPlayRequestTime < 1f) && soundsPerLastSecond >= SoundsPerSecond && priority != AudioPriority.High)
            {
                return(null);
            }

            if (currentTime - previousPlayRequestTime > 1f)
            {
                soundsPerLastSecond = 0;
            }

            previousPlayRequestTime = currentTime;
            soundsPerLastSecond++;

            InitIfNeeded();

            var audioPlayer = new GameObject(string.Format(defaultPlayerName, definition.name))
                              .AddComponent <AudioPlayerBehavior>();

            audioPlayer.transform.SetParent(audioEffectsStorage);

            audioPlayer.PlayDefinition(definition);
            audioPlayer.transform.position = location;

            activePlayers.Add(audioPlayer);
            audioPlayer.DidEndPlaying += OnPlayerEndPlaying;

            return(audioPlayer);
        }
Example #3
0
 private void InvalidateIfNeeded(AudioEffectDefinition definition)
 {
     if (!invalidated)
     {
         DidEndPlaying(this, new AudioPlayerEventArgs(definition, transform.position));
         invalidated = true;
     }
 }
        private void FadeAudioIfNeeded(AudioEffectDefinition definition)
        {
            if (definition.FadesIn)
            {
                StartCoroutine(FadeAudioIn(definition.FadeInEndPercentage, definition.Volume));
            }
            else
            {
                audioSource.volume = definition.Volume;
            }

            if (definition.FadesOut)
            {
                StartCoroutine(FadeAudioOut(definition.FadeOutBeginPercentage, definition.Volume));
            }

            if (definition.Loops)
            {
                StartCoroutine(ReinitializeFadeOnLoop(definition));
            }
        }
Example #5
0
        public void PlayDefinition(AudioEffectDefinition definition)
        {
            InitializeIfNeeded();

            audioSource.clip         = definition.Clip;
            audioSource.pitch        = definition.Pitch;
            audioSource.loop         = definition.Loops;
            audioSource.spatialBlend = definition.Is3DSound ? 1f : 0f;

            if (definition.AutokillOnEnd)
            {
                InvalidateIfNeeded(definition);
                Destroy(gameObject, audioSource.clip.length);
            }
            else
            {
                StartCoroutine(InvalidateDelayed(definition));
            }

            audioSource.Play();
            DidBeginPlaying(this, new AudioPlayerEventArgs(definition, transform.position));

            FadeAudioIfNeeded(definition);
        }
Example #6
0
 public AudioPlayerEventArgs(AudioEffectDefinition definition, Vector3 position)
 {
     this.definition = definition;
     this.position   = position;
 }
Example #7
0
 public AudioPlayerBehavior Play(AudioEffectDefinition definition, AudioPriority priority = AudioPriority.Normal)
 {
     return(Play(definition, Vector3.zero, priority));
 }
        private IEnumerator InvalidateDelayed(AudioEffectDefinition definition)
        {
            yield return(new WaitForSeconds(definition.Clip.length));

            InvalidateIfNeeded(definition);
        }