Skip to content

Instantly share code, notes, and snippets.

@andykorth
Created February 15, 2018 16:03
Show Gist options
  • Save andykorth/189d2f2bb80930dd191c7f0f2e13c37f to your computer and use it in GitHub Desktop.
Save andykorth/189d2f2bb80930dd191c7f0f2e13c37f to your computer and use it in GitHub Desktop.
#pragma warning disable 0649
using UnityEngine;
using System.Collections;
public class Script : MonoBehaviour
{
// magic coroutine maker
public delegate void AnimationDelegate(float alpha);
public Coroutine AddAnimation(float duration, AnimationDelegate d){
return StartCoroutine(AnimationHelper(duration, d, null));
}
public Coroutine AddAnimation(float duration, AnimationDelegate d, float wait ){
return StartCoroutine(AnimationHelper(duration, d, new WaitForSeconds(wait)));
}
public Coroutine AddAnimation(float duration, AnimationDelegate d, Coroutine wait ){
return StartCoroutine(AnimationHelper(duration, d, wait));
}
private IEnumerator AnimationHelper(float duration, AnimationDelegate d, YieldInstruction wait){
if(wait != null) yield return wait;
float startTime = Time.time;
for(float elapsed = 0.0f; elapsed < duration; elapsed = Time.time - startTime){
d(elapsed / duration);
yield return null;
}
d(1.0f);
}
public Coroutine AddDelayed(float wait, System.Action delayed ){
return StartCoroutine(DelayedHelper(new WaitForSeconds(wait), delayed));
}
public Coroutine AddDelayed(Coroutine wait, System.Action delayed ){
return StartCoroutine(DelayedHelper(wait, delayed));
}
private IEnumerator DelayedHelper(YieldInstruction wait, System.Action delayed){
yield return wait;
delayed();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment