Skip to content

Instantly share code, notes, and snippets.

@Bradshaw
Created August 6, 2018 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Bradshaw/fe3afade858d2483ec45579c37da413f to your computer and use it in GitHub Desktop.
Save Bradshaw/fe3afade858d2483ec45579c37da413f to your computer and use it in GitHub Desktop.
Start of a tween system
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