Created
August 6, 2018 15:26
-
-
Save Bradshaw/fe3afade858d2483ec45579c37da413f to your computer and use it in GitHub Desktop.
Start of a tween system
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 System.Collections.Generic; | |
using UnityEngine; | |
public delegate float TweenFunc(float t); | |
public static class TweenUtils { | |
public static float LinearTween(float t){ | |
return t; | |
} | |
public static float QuadTween(float t){ | |
return t*t; | |
} | |
public static void Linear(float start, float end, float time, Action<float> callback) { | |
GameObject obj = new GameObject(); | |
obj.AddComponent<TweenBehaviour>().StartCoroutine(DoTween(start, end, time, callback, LinearTween)); | |
} | |
public static void Quad(float start, float end, float time, Action<float> callback) { | |
GameObject obj = new GameObject(); | |
obj.AddComponent<TweenBehaviour>().StartCoroutine(DoTween(start, end, time, callback, QuadTween)); | |
} | |
public static void Free(float start, float end, float time, Action<float> callback, TweenFunc func) { | |
GameObject obj = new GameObject(); | |
obj.AddComponent<TweenBehaviour>().StartCoroutine(DoTween(start, end, time, callback, func)); | |
} | |
static IEnumerator DoTween(float start, float end, float time, Action<float> callback, TweenFunc func){ | |
float t = 0; | |
do { | |
callback(Mathf.Lerp(start, end, func(t / time))); | |
t += Time.deltaTime; | |
yield return new WaitForEndOfFrame(); | |
} while (t < time); | |
callback(Mathf.Lerp(start, end, 1)); | |
} | |
} | |
public class TweenBehaviour : MonoBehaviour { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment