Пример #1
0
    void UpdateSoundInstance(SoundInstance sfx)
    {
        if (!sfx.sound.loop && sfx.normTime >= 1.0f)
        {
            RemoveSound(sfx, wasDestroyed: false);
            return;
        }

        float fadeInMix  = 1.0f;
        float fadeOutMix = 1.0f;

        if (sfx.sound.loop)
        {
            // TODO: Not sure what to use for delta time. Think normal one makes most sense
            if (sfx.loopingData.fadingOut)
            {
                sfx.loopingData.fadeOutTimer += Time.deltaTime;

                float fadeOutAlpha = sfx.loopingData.fadeOutTimer / sfx.sound.fadeOutTime;
                fadeOutMix = 1.0f - Mathf.Clamp01(fadeOutAlpha);

                if (sfx.loopingData.fadeOutTimer >= sfx.sound.fadeOutTime)
                {
                    RemoveSound(sfx, wasDestroyed: false);
                    return;
                }
            }

            if (sfx.sound.fadeInTime > 0.0f)
            {
                sfx.loopingData.fadeInTimer += Time.deltaTime;

                float fadeInAlpha = sfx.loopingData.fadeInTimer / sfx.sound.fadeInTime;
                fadeInMix = Mathf.Clamp01(fadeInAlpha);
            }
        }
        else
        {
            float fadeOutStart = sfx.oneShotData.startedFadingOutAt ?? sfx.length - sfx.sound.fadeOutTime;
            if (sfx.time >= fadeOutStart)
            {
                if (sfx.oneShotData.startedFadingOutAt == null)
                {
                    sfx.oneShotData.startedFadingOutAt = sfx.time;
                }
                float fadeOutAlpha = Mathf.InverseLerp(
                    fadeOutStart,
                    fadeOutStart + sfx.sound.fadeOutTime,
                    sfx.time
                    );
                fadeOutMix = 1.0f - Mathf.Clamp01(fadeOutAlpha);
                if (fadeOutMix <= 0.0f)
                {
                    RemoveSound(sfx, wasDestroyed: false);
                    return;
                }
            }

            float fadeInAlpha = sfx.time / sfx.sound.fadeInTime;
            fadeInMix = Mathf.Clamp01(fadeInAlpha);
        }

        sfx.fadeVolume = fadeInMix * fadeOutMix;
        //DbgValues.Set(sfx, "fade in mix", fadeInMix);
        //DbgValues.Set(sfx, "fade out mix", fadeOutMix);
        //DbgValues.Set(sfx, "fade volume", sfx.fadeVolume);
        sfx.RefreshSourceVolume();
    }