Created
February 15, 2018 16:03
-
-
Save andykorth/189d2f2bb80930dd191c7f0f2e13c37f to your computer and use it in GitHub Desktop.
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
#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