push-pop / Unity-MVVM

Lightweight MVVM Framework for Unity3D
MIT License
323 stars 27 forks source link

AnimationDefaults should provide the ability to use unscaled time #87

Open B-Erolskiy opened 3 years ago

B-Erolskiy commented 3 years ago

While developing my project using your framework, I encountered the following problem: when I want to stop the game using the code Time.timeScale = 0;, the user interface of some CanvasView objects should continue working. However, this does not happen, and the display animation does not work.

I suggest adding an enumeration similar to this:

public enum AnimationTimeMode
    {
        ScaledTime,
        UnscaledTime,
        ...
    }

And use it in the FadeRoutine method of the AnimationDefaults class as follows:

static IEnumerator FadeRoutine(this ViewBase vb, float target, float time, Action callback, AnimationTimeMode animationTimeMode)
        {
            float start = vb.Alpha;

            for (float t = 0f; t < time; t += animationTimeMode switch {
                AnimationTimeMode.ScaledTime => Time.deltaTime,
                AnimationTimeMode.UnscaledTime => Time.unscaledDeltaTime,
                _ => Time.deltaTime
            })
            {
                float normalizedTime = t / time;
                //right here, you can now use normalizedTime as the third parameter in any Lerp from start to end
                vb.Alpha = Mathf.Lerp(start, target, normalizedTime);
                yield return null;
            }
            vb.Alpha = target;

            callback?.Invoke();
        }

Of course, I also suggest putting this setting in the ViewBase object as follows:

[SerializeField]
protected AnimationTimeMode _animationTimeMode = AnimationDefaults.AnimationTimeMode;

What do you think about this improvement?

push-pop commented 3 years ago

Hi @B-Erolskiy thanks for making this issue.

I think that makes a lot of sense. I don't typically adjust the timescale on my projects which is why I have never run into the issue.

If you have a branch that implements this feel free to make a PR and I will take a look. Otherwise I will implement something for this in the future.

B-Erolskiy commented 3 years ago

Hi @push-pop

Thanks for your quick response. Ok. In the next few days I will finalize it and make a pull request.