使用
示例用法 (将此脚本附加到一个游戏物体):
数组,它将显示您的对象,如 Positions/Rotations/Alphas有 5 的参数,这种工作方式:
Total Time:此操作将需要多长时间。
Delay:此操作将等多久才能启动。(如果此操作有一个或多个操作在它之前,你可能想添加所用总的时间)
Ease:你想要对此"tween"什么样的行为呢
Tween Value:最后Translation/Rotation/Alpha 的元素。
Loop Array:将它循环吗?
UnityTween.cs (多次使用了foreach在实际使用应该改为for)
using UnityEngine;
using System.Collections;
//Tween position object class
[System.Serializable]
public class TweenPositionObject : BaseTweenObject
{
public Vector3 tweenValue;
private Vector3 _startValue;
public Vector3 startValue
{
set{_startValue = value;}
get{return _startValue;}
}
public TweenPositionObject ()
{
this.tweenType = TweenType.TweenPosition;
}
}
//Tween rotation object class
[System.Serializable]
public class TweenRotationObject : BaseTweenObject
{
public Vector3 tweenValue;
private Vector3 _startValue;
public Vector3 startValue
{
set{_startValue = value;}
get{return _startValue;}
}
public TweenRotationObject ()
{
this.tweenType = TweenType.TweenRotation;
}
}
//Tween Alpha object class
[System.Serializable]
public class TweenAlphaObject : BaseTweenObject
{
public float tweenValue;
private float _startValue;
public float startValue
{
set{_startValue = value;}
get{return _startValue;}
}
public TweenAlphaObject ()
{
this.tweenType = TweenType.TweenAlpha;
}
}
//Engine class
public class UnityTween : MonoBehaviour {
public TweenPositionObject[] positions = new TweenPositionObject[0];
public TweenRotationObject[] rotations = new TweenRotationObject[0];
public TweenAlphaObject[] alphas = new TweenAlphaObject[0];
public bool loopArray;
private ArrayList tweens;
void Start () {
this.tweens = new ArrayList();
this.AddTweens();
}
private void AddTweens ()
{
foreach(TweenPositionObject tween in positions)
{
TweenPosition(tween);
}
foreach(TweenRotationObject tween in rotations)
{
TweenRotation(tween);
}
foreach(TweenAlphaObject tween in alphas)
{
TweenAlpha(tween);
}
}
//Add Tween in arrayList
//Tween position
public void TweenPosition (TweenPositionObject obj)
{
TweenPositionObject tween = new TweenPositionObject();
tween.startTime = Time.time;
tween.CopyTween(obj);
tween.tweenValue = obj.tweenValue;
tween.Init();
this.tweens.Add(tween);
}
//Tween rotation
public void TweenRotation (TweenRotationObject obj)
{
TweenRotationObject tween = new TweenRotationObject();
tween.startTime = Time.time;
tween.CopyTween(obj);
tween.tweenValue = obj.tweenValue;
tween.Init();
this.tweens.Add(tween);
}
//Tween alpha
public void TweenAlpha (TweenAlphaObject obj)
{
TweenAlphaObject tween = new TweenAlphaObject();
tween.startTime = Time.time;
tween.CopyTween(obj);
tween.tweenValue = obj.tweenValue;
tween.Init();
this.tweens.Add(tween);
}
//Clear Tweens with the same type
private void ClearTweensSameType (BaseTweenObject obj)
{
foreach (BaseTweenObject tween in tweens)
{
if(tween.id != obj.id && tween.tweenType == obj.tweenType)
tween.ended = true;
}
}
//Updates
void Update ()
{
this.DetectDelay();
this.UpdateTween();
}
//Detect when delay was passed
private void DetectDelay ()
{
foreach (BaseTweenObject tween in tweens)
{
if(Time.time > tween.startTime + tween.delay && !tween.canStart)
{
if(tween.tweenType == TweenType.TweenPosition)
{
TweenPositionObject tweenPos = tween as TweenPositionObject;
tweenPos.startValue = this.transform.position;
}
else if(tween.tweenType == TweenType.TweenRotation)
{
TweenRotationObject tweenRot = tween as TweenRotationObject;
tweenRot.startValue = this.transform.rotation.eulerAngles;
}
else if(tween.tweenType == TweenType.TweenAlpha)
{
TweenAlphaObject tweenAlpha = tween as TweenAlphaObject;
if(GetComponent<GUITexture>() != null)
tweenAlpha.startValue = GetComponent<GUITexture>().color.a;
else
tweenAlpha.startValue = this.GetComponent<Renderer>().material.color.a;
}
this.ClearTweensSameType(tween);
tween.canStart = true;
}
}
}
//Update tween by type
private void UpdateTween ()
{
int tweenCompleted = 0;
foreach (BaseTweenObject tween in tweens)
{
if(tween.canStart && !tween.ended)
{
if(tween.tweenType == TweenType.TweenPosition)
UpdatePosition(tween as TweenPositionObject);
else if(tween.tweenType == TweenType.TweenRotation)
UpdateRotation(tween as TweenRotationObject);
else if(tween.tweenType == TweenType.TweenAlpha)
UpdateAlpha(tween as TweenAlphaObject);
}
if(tween.ended)
tweenCompleted++;
if(tweenCompleted == tweens.Count && loopArray)
this.MakeLoop ();
}
}
private void MakeLoop ()
{
foreach (BaseTweenObject tween in tweens)
{
tween.ended = false;
tween.canStart = false;
tween.startTime = Time.time;
}
}
//Update Position
private void UpdatePosition(TweenPositionObject tween)
{
Vector3 begin = tween.startValue;
Vector3 finish = tween.tweenValue;
Vector3 change = finish - begin;
float duration = tween.totalTime;
float currentTime = Time.time - (tween.startTime + tween.delay);
if(duration == 0)
{
this.EndTween(tween);
this.transform.position = finish;
return;
}
if(Time.time > tween.startTime + tween.delay + duration)
this.EndTween(tween);
this.transform.position = Equations.ChangeVector(currentTime, begin, change ,duration, tween.ease);
}
//Update Rotation
private void UpdateRotation(TweenRotationObject tween)
{
Vector3 begin = tween.startValue;
Vector3 finish = tween.tweenValue;
Vector3 change = finish - begin;
float duration = tween.totalTime;
float currentTime = Time.time - (tween.startTime + tween.delay);
if(duration == 0)
{
this.EndTween(tween);
this.transform.position = finish;
return;
}
if(Time.time > tween.startTime + tween.delay + duration)
this.EndTween(tween);
this.transform.rotation = Quaternion.Euler(Equations.ChangeVector(currentTime, begin, change ,duration, tween.ease));
}
//Update Alpha
private void UpdateAlpha(TweenAlphaObject tween)
{
float begin = tween.startValue;
float finish = tween.tweenValue;
float change = finish - begin;
float duration = tween.totalTime;
float currentTime = Time.time - (tween.startTime + tween.delay);
float alpha = Equations.ChangeFloat(currentTime, begin, change ,duration, tween.ease);
float redColor;
float redGreen;
float redBlue;
if(GetComponent<GUITexture>() != null)
{
redColor = GetComponent<GUITexture>().color.r;
redGreen = GetComponent<GUITexture>().color.g;
redBlue = GetComponent<GUITexture>().color.b;
GetComponent<GUITexture>().color = new Color(redColor,redGreen,redBlue,alpha);
if(duration == 0)
{
this.EndTween(tween);
GetComponent<GUITexture>().color = new Color(redColor,redGreen,redBlue,finish);
return;
}
}
else
{
redColor = this.GetComponent<Renderer>().material.color.r;
redGreen = this.GetComponent<Renderer>().material.color.g;
redBlue = this.GetComponent<Renderer>().material.color.b;
this.GetComponent<Renderer>().material.color = new Color(redColor,redGreen,redBlue,alpha);
if(duration == 0)
{
this.EndTween(tween);
this.GetComponent<Renderer>().material.color = new Color(redColor,redGreen,redBlue,finish);
return;
}
}
if(Time.time > tween.startTime + tween.delay + duration)
this.EndTween(tween);
}
private void EndTween (BaseTweenObject tween)
{
tween.ended = true;
}
}
Tween.cs
using UnityEngine;
using System.Collections;
public enum Ease {
Linear = 0,
EaseInQuad = 1,
EaseOutQuad = 2,
EaseInOutQuad = 3,
EaseOutInQuad = 4,
EaseInCubic = 5,
EaseOutCubic = 6,
EaseInOutCubic = 7,
EaseOutInCubic = 8,
EaseInQuart = 9,
EaseOutQuart = 10,
EaseInOutQuart = 11,
EaseOutInQuart = 12,
EaseInQuint = 13,
EaseOutQuint = 14,
EaseInOutQuint = 15,
EaseOutInQuint = 16,
EaseInSine = 17,
EaseOutSine = 18,
EaseInOutSine = 19,
EaseOutInSine = 20,
EaseInExpo = 21,
EaseOutExpo = 22,
EaseInOutExpo = 23,
EaseOutInExpo = 24,
EaseInCirc = 25,
EaseOutCirc = 26,
EaseInOutCirc = 27,
EaseOutInCirc = 28,
EaseInElastic = 29,
EaseOutElastic = 30,
EaseInOutElastic = 31,
EaseOutInElastic = 32,
EaseInBack = 33,
EaseOutBack = 34,
EaseInOutBack = 35,
EaseOutInBack = 36,
EaseInBounce = 37,
EaseOutBounce = 38,
EaseInOutBounce = 39,
EaseOutInBounce = 40
}
public class Equations {
// TWEENING EQUATIONS floats -----------------------------------------------------------------------------------------------------
// (the original equations are Robert Penner's work as mentioned on the disclaimer)
/**
* Easing equation float for a simple linear tweening, with no easing.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseNone (float t, float b, float c, float d) {
return c * t / d + b;
}
/**
* Easing equation float for a quadratic (t^2) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInQuad (float t, float b, float c, float d) {
return c * (t/=d) * t + b;
}
/**
* Easing equation float for a quadratic (t^2) easing out: decelerating to zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutQuad (float t, float b, float c, float d) {
return -c *(t/=d)*(t-2) + b;
}
/**
* Easing equation float for a quadratic (t^2) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInOutQuad (float t, float b, float c, float d) {
if ((t/=d/2) < 1) return c/2*t*t + b;
return -c/2 * ((--t)*(t-2) - 1) + b;
}
/**
* Easing equation float for a quadratic (t^2) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutInQuad (float t, float b, float c, float d) {
if (t < d/2) return EaseOutQuad (t*2, b, c/2, d);
return EaseInQuad((t*2)-d, b+c/2, c/2, d);
}
/**
* Easing equation float for a cubic (t^3) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInCubic (float t, float b, float c, float d) {
return c*(t/=d)*t*t + b;
}
/**
* Easing equation float for a cubic (t^3) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutCubic (float t, float b, float c, float d) {
return c*((t=t/d-1)*t*t + 1) + b;
}
/**
* Easing equation float for a cubic (t^3) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInOutCubic (float t, float b, float c, float d) {
if ((t/=d/2) < 1) return c/2*t*t*t + b;
return c/2*((t-=2)*t*t + 2) + b;
}
/**
* Easing equation float for a cubic (t^3) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutInCubic (float t, float b, float c, float d) {
if (t < d/2) return EaseOutCubic (t*2, b, c/2, d);
return EaseInCubic((t*2)-d, b+c/2, c/2, d);
}
/**
* Easing equation float for a quartic (t^4) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInQuart (float t, float b, float c, float d) {
return c*(t/=d)*t*t*t + b;
}
/**
* Easing equation float for a quartic (t^4) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutQuart (float t, float b, float c, float d) {
return -c * ((t=t/d-1)*t*t*t - 1) + b;
}
/**
* Easing equation float for a quartic (t^4) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInOutQuart (float t, float b, float c, float d) {
if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
return -c/2 * ((t-=2)*t*t*t - 2) + b;
}
/**
* Easing equation float for a quartic (t^4) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutInQuart (float t, float b, float c, float d) {
if (t < d/2) return EaseOutQuart (t*2, b, c/2, d);
return EaseInQuart((t*2)-d, b+c/2, c/2, d);
}
/**
* Easing equation float for a quintic (t^5) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInQuint (float t, float b, float c, float d) {
return c*(t/=d)*t*t*t*t + b;
}
/**
* Easing equation float for a quintic (t^5) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutQuint (float t, float b, float c, float d) {
return c*((t=t/d-1)*t*t*t*t + 1) + b;
}
/**
* Easing equation float for a quintic (t^5) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInOutQuint (float t, float b, float c, float d) {
if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
return c/2*((t-=2)*t*t*t*t + 2) + b;
}
/**
* Easing equation float for a quintic (t^5) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutInQuint (float t, float b, float c, float d) {
if (t < d/2) return EaseOutQuint (t*2, b, c/2, d);
return EaseInQuint((t*2)-d, b+c/2, c/2, d);
}
/**
* Easing equation float for a sinusoidal (sin(t)) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInSine (float t, float b, float c, float d) {
return -c * Mathf.Cos(t/d * (Mathf.PI/2)) + c + b;
}
/**
* Easing equation float for a sinusoidal (sin(t)) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutSine (float t, float b, float c, float d) {
return c * Mathf.Sin(t/d * (Mathf.PI/2)) + b;
}
/**
* Easing equation float for a sinusoidal (sin(t)) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInOutSine (float t, float b, float c, float d) {
return -c/2 * (Mathf.Cos(Mathf.PI*t/d) - 1) + b;
}
/**
* Easing equation float for a sinusoidal (sin(t)) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutInSine (float t, float b, float c, float d) {
if (t < d/2) return EaseOutSine (t*2, b, c/2, d);
return EaseInSine((t*2)-d, b+c/2, c/2, d);
}
/**
* Easing equation float for an exponential (2^t) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInExpo (float t, float b, float c, float d) {
return (t==0) ? b : c * Mathf.Pow(2, 10 * (t/d - 1)) + b - c * 0.001f;
}
/**
* Easing equation float for an exponential (2^t) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutExpo (float t, float b, float c, float d) {
return (t==d) ? b+c : c * 1.001f * (-Mathf.Pow(2, -10 * t/d) + 1) + b;
}
/**
* Easing equation float for an exponential (2^t) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInOutExpo (float t, float b, float c, float d) {
if (t==0) return b;
if (t==d) return b+c;
if ((t/=d/2) < 1) return c/2 * Mathf.Pow(2, 10 * (t - 1)) + b - c * 0.0005f;
return c/2 * 1.0005f * (-Mathf.Pow(2, -10 * --t) + 2) + b;
}
/**
* Easing equation float for an exponential (2^t) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutInExpo (float t, float b, float c, float d) {
if (t < d/2) return EaseOutExpo (t*2, b, c/2, d);
return EaseInExpo((t*2)-d, b+c/2, c/2, d);
}
/**
* Easing equation float for a circular (sqrt(1-t^2)) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInCirc (float t, float b, float c, float d) {
return -c * (Mathf.Sqrt(1 - (t/=d)*t) - 1) + b;
}
/**
* Easing equation float for a circular (sqrt(1-t^2)) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutCirc (float t, float b, float c, float d) {
return c * Mathf.Sqrt(1 - (t=t/d-1)*t) + b;
}
/**
* Easing equation float for a circular (sqrt(1-t^2)) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInOutCirc (float t, float b, float c, float d) {
if ((t/=d/2) < 1) return -c/2 * (Mathf.Sqrt(1 - t*t) - 1) + b;
return c/2 * (Mathf.Sqrt(1 - (t-=2)*t) + 1) + b;
}
/**
* Easing equation float for a circular (sqrt(1-t^2)) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutInCirc (float t, float b, float c, float d) {
if (t < d/2) return EaseOutCirc (t*2, b, c/2, d);
return EaseInCirc((t*2)-d, b+c/2, c/2, d);
}
/**
* Easing equation float for an elastic (exponentially decaying sine wave) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @param a Amplitude.
* @param p Period.
* @return The correct value.
*/
public static float EaseInElastic (float t, float b, float c, float d) {
if (t==0) return b;
if ((t/=d)==1) return b+c;
float p = d *.3f;
float s = 0;
float a = 0;
if (a == 0f || a < Mathf.Abs(c)) {
a = c;
s = p/4;
} else {
s = p/(2*Mathf.PI) * Mathf.Asin (c/a);
}
return -(a*Mathf.Pow(2,10*(t-=1)) * Mathf.Sin( (t*d-s)*(2*Mathf.PI)/p )) + b;
}
/**
* Easing equation float for an elastic (exponentially decaying sine wave) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @param a Amplitude.
* @param p Period.
* @return The correct value.
*/
public static float EaseOutElastic (float t, float b, float c, float d) {
if (t==0) return b;
if ((t/=d)==1) return b+c;
float p = d*.3f;
float s = 0;
float a = 0;
if (a == 0f || a < Mathf.Abs(c)) {
a = c;
s = p/4;
} else {
s = p/(2*Mathf.PI) * Mathf.Asin (c/a);
}
return (a*Mathf.Pow(2,-10*t) * Mathf.Sin( (t*d-s)*(2*Mathf.PI)/p ) + c + b);
}
/**
* Easing equation float for an elastic (exponentially decaying sine wave) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @param a Amplitude.
* @param p Period.
* @return The correct value.
*/
public static float EaseInOutElastic (float t, float b, float c, float d) {
if (t==0) return b;
if ((t/=d/2)==2) return b+c;
float p = d*(.3f*1.5f);
float s = 0;
float a = 0;
if (a == 0f || a < Mathf.Abs(c)) {
a = c;
s = p/4;
} else {
s = p/(2*Mathf.PI) * Mathf.Asin (c/a);
}
if (t < 1) return -.5f*(a*Mathf.Pow(2,10*(t-=1)) * Mathf.Sin( (t*d-s)*(2*Mathf.PI)/p )) + b;
return a*Mathf.Pow(2,-10*(t-=1)) * Mathf.Sin( (t*d-s)*(2*Mathf.PI)/p )*.5f + c + b;
}
/**
* Easing equation float for an elastic (exponentially decaying sine wave) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @param a Amplitude.
* @param p Period.
* @return The correct value.
*/
public static float EaseOutInElastic (float t, float b, float c, float d) {
if (t < d/2) return EaseOutElastic (t*2, b, c/2, d);
return EaseInElastic((t*2)-d, b+c/2, c/2, d);
}
/**
* Easing equation float for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @param s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
* @return The correct value.
*/
public static float EaseInBack (float t, float b, float c, float d) {
float s = 1.70158f;
return c*(t/=d)*t*((s+1)*t - s) + b;
}
/**
* Easing equation float for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @param s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
* @return The correct value.
*/
public static float EaseOutBack (float t, float b, float c, float d) {
float s = 1.70158f;
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
}
/**
* Easing equation float for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @param s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
* @return The correct value.
*/
public static float EaseInOutBack (float t, float b, float c, float d) {
float s = 1.70158f;
if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525f))+1)*t - s)) + b;
return c/2*((t-=2)*t*(((s*=(1.525f))+1)*t + s) + 2) + b;
}
/**
* Easing equation float for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @param s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
* @return The correct value.
*/
public static float EaseOutInBack (float t, float b, float c, float d) {
if (t < d/2) return EaseOutBack (t*2, b, c/2, d);
return EaseInBack((t*2)-d, b+c/2, c/2, d);
}
/**
* Easing equation float for a bounce (exponentially decaying parabolic bounce) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInBounce (float t, float b, float c, float d) {
return c - EaseOutBounce (d-t, 0, c, d) + b;
}
/**
* Easing equation float for a bounce (exponentially decaying parabolic bounce) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutBounce (float t, float b, float c, float d) {
if ((t/=d) < (1/2.75f)) {
return c*(7.5625f*t*t) + b;
} else if (t < (2/2.75f)) {
return c*(7.5625f*(t-=(1.5f/2.75f))*t + .75f) + b;
} else if (t < (2.5f/2.75f)) {
return c*(7.5625f*(t-=(2.25f/2.75f))*t + .9375f) + b;
} else {
return c*(7.5625f*(t-=(2.625f/2.75f))*t + .984375f) + b;
}
}
/**
* Easing equation float for a bounce (exponentially decaying parabolic bounce) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseInOutBounce (float t, float b, float c, float d) {
if (t < d/2) return EaseInBounce (t*2, 0, c, d) * .5f + b;
else return EaseOutBounce (t*2-d, 0, c, d) * .5f + c*.5f + b;
}
/**
* Easing equation float for a bounce (exponentially decaying parabolic bounce) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @param b Starting value.
* @param c Change needed in value.
* @param d Expected easing duration (in frames or seconds).
* @return The correct value.
*/
public static float EaseOutInBounce (float t, float b, float c, float d) {
if (t < d/2) return EaseOutBounce (t*2, b, c/2, d);
return EaseInBounce((t*2)-d, b+c/2, c/2, d);
}
public static Vector3 ChangeVector(float t , Vector3 b , Vector3 c , float d , Ease Ease)
{
float x = 0;
float y = 0;
float z = 0;
if(Ease == Ease.Linear)
{
x = EaseNone (t , b.x , c.x , d);
y = EaseNone (t , b.y , c.y , d);
z = EaseNone (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInQuad)
{
x = EaseInQuad (t , b.x , c.x , d);
y = EaseInQuad (t , b.y , c.y , d);
z = EaseInQuad (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutQuad)
{
x = EaseOutQuad (t , b.x , c.x , d);
y = EaseOutQuad (t , b.y , c.y , d);
z = EaseOutQuad (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInOutQuad)
{
x = EaseInOutQuad (t , b.x , c.x , d);
y = EaseInOutQuad (t , b.y , c.y , d);
z = EaseInOutQuad (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutInQuad)
{
x = EaseOutInQuad (t , b.x , c.x , d);
y = EaseOutInQuad (t , b.y , c.y , d);
z = EaseOutInQuad (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInCubic)
{
x = EaseInCubic (t , b.x , c.x , d);
y = EaseInCubic (t , b.y , c.y , d);
z = EaseInCubic (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutCubic)
{
x = EaseOutCubic (t , b.x , c.x , d);
y = EaseOutCubic (t , b.y , c.y , d);
z = EaseOutCubic (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInOutCubic)
{
x = EaseInOutCubic (t , b.x , c.x , d);
y = EaseInOutCubic (t , b.y , c.y , d);
z = EaseInOutCubic (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutInCubic)
{
x = EaseOutInCubic (t , b.x , c.x , d);
y = EaseOutInCubic (t , b.y , c.y , d);
z = EaseOutInCubic (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInQuart)
{
x = EaseInQuart (t , b.x , c.x , d);
y = EaseInQuart (t , b.y , c.y , d);
z = EaseInQuart (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutQuart)
{
x = EaseOutQuart (t , b.x , c.x , d);
y = EaseOutQuart (t , b.y , c.y , d);
z = EaseOutQuart (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInOutQuart)
{
x = EaseInOutQuart (t , b.x , c.x , d);
y = EaseInOutQuart (t , b.y , c.y , d);
z = EaseInOutQuart (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutInQuart)
{
x = EaseOutInQuart (t , b.x , c.x , d);
y = EaseOutInQuart (t , b.y , c.y , d);
z = EaseOutInQuart (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInQuint)
{
x = EaseInQuint (t , b.x , c.x , d);
y = EaseInQuint (t , b.y , c.y , d);
z = EaseInQuint (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutQuint)
{
x = EaseOutQuint (t , b.x , c.x , d);
y = EaseOutQuint (t , b.y , c.y , d);
z = EaseOutQuint (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInOutQuint)
{
x = EaseInOutQuint (t , b.x , c.x , d);
y = EaseInOutQuint (t , b.y , c.y , d);
z = EaseInOutQuint (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutInQuint)
{
x = EaseOutInQuint (t , b.x , c.x , d);
y = EaseOutInQuint (t , b.y , c.y , d);
z = EaseOutInQuint (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInSine)
{
x = EaseInSine (t , b.x , c.x , d);
y = EaseInSine (t , b.y , c.y , d);
z = EaseInSine (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutSine)
{
x = EaseOutSine (t , b.x , c.x , d);
y = EaseOutSine (t , b.y , c.y , d);
z = EaseOutSine (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInOutSine)
{
x = EaseInOutSine (t , b.x , c.x , d);
y = EaseInOutSine (t , b.y , c.y , d);
z = EaseInOutSine (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutInSine)
{
x = EaseOutInSine (t , b.x , c.x , d);
y = EaseOutInSine (t , b.y , c.y , d);
z = EaseOutInSine (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInExpo)
{
x = EaseInExpo (t , b.x , c.x , d);
y = EaseInExpo (t , b.y , c.y , d);
z = EaseInExpo (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutExpo)
{
x = EaseOutExpo (t , b.x , c.x , d);
y = EaseOutExpo (t , b.y , c.y , d);
z = EaseOutExpo (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInOutExpo)
{
x = EaseInOutExpo (t , b.x , c.x , d);
y = EaseInOutExpo (t , b.y , c.y , d);
z = EaseInOutExpo (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutInExpo)
{
x = EaseOutInExpo (t , b.x , c.x , d);
y = EaseOutInExpo (t , b.y , c.y , d);
z = EaseOutInExpo (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInCirc)
{
x = EaseInCirc (t , b.x , c.x , d);
y = EaseInCirc (t , b.y , c.y , d);
z = EaseInCirc (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutCirc)
{
x = EaseOutCirc (t , b.x , c.x , d);
y = EaseOutCirc (t , b.y , c.y , d);
z = EaseOutCirc (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInOutCirc)
{
x = EaseInOutCirc (t , b.x , c.x , d);
y = EaseInOutCirc (t , b.y , c.y , d);
z = EaseInOutCirc (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutInCirc)
{
x = EaseOutInCirc (t , b.x , c.x , d);
y = EaseOutInCirc (t , b.y , c.y , d);
z = EaseOutInCirc (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInElastic)
{
x = EaseInElastic (t , b.x , c.x , d);
y = EaseInElastic (t , b.y , c.y , d);
z = EaseInElastic (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutElastic)
{
x = EaseOutElastic (t , b.x , c.x , d);
y = EaseOutElastic (t , b.y , c.y , d);
z = EaseOutElastic (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInOutElastic)
{
x = EaseInOutElastic (t , b.x , c.x , d);
y = EaseInOutElastic (t , b.y , c.y , d);
z = EaseInOutElastic (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutInElastic)
{
x = EaseOutInElastic (t , b.x , c.x , d);
y = EaseOutInElastic (t , b.y , c.y , d);
z = EaseOutInElastic (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInBack)
{
x = EaseInBack (t , b.x , c.x , d);
y = EaseInBack (t , b.y , c.y , d);
z = EaseInBack (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutBack)
{
x = EaseOutBack (t , b.x , c.x , d);
y = EaseOutBack (t , b.y , c.y , d);
z = EaseOutBack (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInOutBack)
{
x = EaseInOutBack (t , b.x , c.x , d);
y = EaseInOutBack (t , b.y , c.y , d);
z = EaseInOutBack (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutInBack)
{
x = EaseOutInBack (t , b.x , c.x , d);
y = EaseOutInBack (t , b.y , c.y , d);
z = EaseOutInBack (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInBounce)
{
x = EaseInBounce (t , b.x , c.x , d);
y = EaseInBounce (t , b.y , c.y , d);
z = EaseInBounce (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutBounce)
{
x = EaseOutBounce (t , b.x , c.x , d);
y = EaseOutBounce (t , b.y , c.y , d);
z = EaseOutBounce (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseInOutBounce)
{
x = EaseInOutBounce (t , b.x , c.x , d);
y = EaseInOutBounce (t , b.y , c.y , d);
z = EaseInOutBounce (t , b.z , c.z , d);
}
else if(Ease == Ease.EaseOutInBounce)
{
x = EaseOutInBounce (t , b.x , c.x , d);
y = EaseOutInBounce (t , b.y , c.y , d);
z = EaseOutInBounce (t , b.z , c.z , d);
}
return new Vector3(x,y,z);
}
public static float ChangeFloat(float t , float b , float c , float d , Ease Ease)
{
float value = 0;
if(Ease == Ease.Linear)
value = EaseNone (t , b , c , d);
else if(Ease == Ease.EaseInQuad)
value = EaseInQuad (t , b , c , d);
else if(Ease == Ease.EaseOutQuad)
value = EaseOutQuad (t , b , c , d);
else if(Ease == Ease.EaseInOutQuad)
value = EaseInOutQuad (t , b , c , d);
else if(Ease == Ease.EaseOutInQuad)
value = EaseOutInQuad (t , b , c , d);
else if(Ease == Ease.EaseInCubic)
value = EaseInCubic (t , b , c , d);
else if(Ease == Ease.EaseOutCubic)
value = EaseOutCubic (t , b , c , d);
else if(Ease == Ease.EaseInOutCubic)
value = EaseInOutCubic (t , b , c , d);
else if(Ease == Ease.EaseOutInCubic)
value = EaseOutInCubic (t , b , c , d);
else if(Ease == Ease.EaseInQuart)
value = EaseInQuart (t , b , c , d);
else if(Ease == Ease.EaseOutQuart)
value = EaseOutQuart (t , b , c , d);
else if(Ease == Ease.EaseInOutQuart)
value = EaseInOutQuart (t , b , c , d);
else if(Ease == Ease.EaseOutInQuart)
value = EaseOutInQuart (t , b , c , d);
else if(Ease == Ease.EaseInQuint)
value = EaseInQuint (t , b , c , d);
else if(Ease == Ease.EaseOutQuint)
value = EaseOutQuint (t , b , c , d);
else if(Ease == Ease.EaseInOutQuint)
value = EaseInOutQuint (t , b , c , d);
else if(Ease == Ease.EaseOutInQuint)
value = EaseOutInQuint (t , b , c , d);
else if(Ease == Ease.EaseInSine)
value = EaseInSine (t , b , c , d);
else if(Ease == Ease.EaseOutSine)
value = EaseOutSine (t , b , c , d);
else if(Ease == Ease.EaseInOutSine)
value = EaseInOutSine (t , b , c , d);
else if(Ease == Ease.EaseOutInSine)
value = EaseOutInSine (t , b , c , d);
else if(Ease == Ease.EaseInExpo)
value = EaseInExpo (t , b , c , d);
else if(Ease == Ease.EaseOutExpo)
value = EaseOutExpo (t , b , c , d);
else if(Ease == Ease.EaseInOutExpo)
value = EaseInOutExpo (t , b , c , d);
else if(Ease == Ease.EaseOutInExpo)
value = EaseOutInExpo (t , b , c , d);
else if(Ease == Ease.EaseInCirc)
value = EaseInCirc (t , b , c , d);
else if(Ease == Ease.EaseOutCirc)
value = EaseOutCirc (t , b , c , d);
else if(Ease == Ease.EaseInOutCirc)
value = EaseInOutCirc (t , b , c , d);
else if(Ease == Ease.EaseOutInCirc)
value = EaseOutInCirc (t , b , c , d);
else if(Ease == Ease.EaseInElastic)
value = EaseInElastic (t , b , c , d);
else if(Ease == Ease.EaseOutElastic)
value = EaseOutElastic (t , b , c , d);
else if(Ease == Ease.EaseInOutElastic)
value = EaseInOutElastic (t , b , c , d);
else if(Ease == Ease.EaseOutInElastic)
value = EaseOutInElastic (t , b , c , d);
else if(Ease == Ease.EaseInBack)
value = EaseInBack (t , b , c , d);
else if(Ease == Ease.EaseOutBack)
value = EaseOutBack (t , b , c , d);
else if(Ease == Ease.EaseInOutBack)
value = EaseInOutBack (t , b , c , d);
else if(Ease == Ease.EaseOutInBack)
value = EaseOutInBack (t , b , c , d);
else if(Ease == Ease.EaseInBounce)
value = EaseInBounce (t , b , c , d);
else if(Ease == Ease.EaseOutBounce)
value = EaseOutBounce (t , b , c , d);
else if(Ease == Ease.EaseInOutBounce)
value = EaseInOutBounce (t , b , c , d);
else if(Ease == Ease.EaseOutInBounce)
value = EaseOutInBounce (t , b , c , d);
return value;
}
}
BaseTweenObject.cs
using UnityEngine;
using System.Collections;
public enum TweenType {
TweenPosition = 0,
TweenRotation = 1,
TweenAlpha = 2
}
public class BaseTweenObject {
public float totalTime = 0;
public float delay = 0;
public Ease ease = Ease.Linear;
private TweenType _tweenType;
public TweenType tweenType
{
set{_tweenType = value;}
get{return _tweenType;}
}
private float _startTime;
public float startTime
{
set{_startTime = value;}
get{return _startTime;}
}
private bool _ended = false;
public bool ended
{
set{_ended = value;}
get{return _ended;}
}
private bool _canStart = false;
public bool canStart
{
set{_canStart = value;}
get{return _canStart;}
}
private string _id = "";
public string id
{
set{_id = value;}
get{return _id;}
}
public BaseTweenObject ()
{
}
public void Init()
{
this.id = "tween" + Time.time.ToString();
}
public void CopyTween (BaseTweenObject tween)
{
this.totalTime = tween.totalTime;
this.delay = tween.delay;
this.ease = tween.ease;
this.tweenType = tween.tweenType;
}
}