IEnumerator Fade(float targetVolume, float duration)
    {
        FadeStart.Invoke();
        float timer      = 0;
        float baseVolume = audioSource.volume;

        do
        {
            if (duration == 0)
            {
                audioSource.volume = targetVolume;
                FadeEnd.Invoke();
                yield break;
            }

            timer += Time.deltaTime;
            audioSource.volume = Mathf.Lerp(baseVolume, targetVolume, timer / duration);
            yield return(null);
        }while (timer <= duration);

        // In case the lerping makes timer go past duration.
        audioSource.volume = targetVolume;

        // Signify that the fading is done.
        fadeCoroutine = null;
        FadeEnd.Invoke();
    }
Beispiel #2
0
 protected virtual void OnFadeStart(FadeEventArgs e)
 {
     if (FadeStart != null)
     {
         FadeStart.Invoke(this, e);
     }
 }