Last active
April 15, 2025 02:53
-
-
Save aczw/d509039eef59e18bfa495bd1158bcfbf to your computer and use it in GitHub Desktop.
Simple tweening utility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections; | |
| using UnityEngine; | |
| public static class Tween | |
| { | |
| public static IEnumerator Animate(float animTime, Action<float> enumerate, bool useUnscaledDelta = false) { | |
| var elapsedTime = 0f; | |
| while (elapsedTime <= animTime) { | |
| var t = Mathf.Clamp01(elapsedTime / animTime); | |
| enumerate(t); | |
| elapsedTime += useUnscaledDelta ? Time.unscaledDeltaTime : Time.deltaTime; | |
| yield return null; | |
| } | |
| // Make sure animation finishes completely e.g. interpolates to 1 | |
| enumerate(1f); | |
| } | |
| public static IEnumerator FadeIn(float duration, CanvasGroup cg, bool useUnscaledDelta = false) { | |
| yield return Animate(duration, t => { | |
| cg.alpha = t; | |
| }, useUnscaledDelta); | |
| } | |
| public static IEnumerator FadeOut(float duration, CanvasGroup cg, bool useUnscaledDelta = false) { | |
| yield return Animate(duration, t => { | |
| cg.alpha = 1f - t; | |
| }, useUnscaledDelta); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment