Last active
May 8, 2021 08:46
-
-
Save MattRix/7360d98bc4ff8ed4afcedbb2390aacd6 to your computer and use it in GitHub Desktop.
RT - Simple coroutine tweens
This file contains 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 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