Skip to content

Instantly share code, notes, and snippets.

@aczw
Last active April 15, 2025 02:53
Show Gist options
  • Select an option

  • Save aczw/d509039eef59e18bfa495bd1158bcfbf to your computer and use it in GitHub Desktop.

Select an option

Save aczw/d509039eef59e18bfa495bd1158bcfbf to your computer and use it in GitHub Desktop.
Simple tweening utility
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