Skip to content

Instantly share code, notes, and snippets.

@MattRix
Last active May 8, 2021 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattRix/7360d98bc4ff8ed4afcedbb2390aacd6 to your computer and use it in GitHub Desktop.
Save MattRix/7360d98bc4ff8ed4afcedbb2390aacd6 to your computer and use it in GitHub Desktop.
RT - Simple coroutine tweens
using System;
using System.Collections;
using UnityEngine;
public static class RT
{
static public IEnumerator Tween(float duration, Action<float> task, Func<float,float> easeFunc = null, float delay = 0)
{
while(delay > 0)
{
delay -= Time.deltaTime;
yield return null;
}
if(duration <= 0)
{
task(1f);
}
else
{
float t = -delay; //at this point delay can only be zero or negative. if negative it means we overshot it a bit, so start the tween using that amount
while(t < 1f)
{
t = t+Time.deltaTime/duration;
if(t > 1f) t = 1f;
if(easeFunc == null) task(t); else task(easeFunc(t));
yield return null;
}
}
}
static public IEnumerator AddCallback(IEnumerator routine, Action callback)
{
while(routine.MoveNext()) yield return routine.Current;
callback();
}
//keep doing task every frame until it returns false
static public IEnumerator Do(Func<bool> task)
{
while(task()) yield return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment