Advertisement
infinite_ammo

Tween.cs

Mar 11th, 2013
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.53 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class TweenBase : MonoBehaviour
  5. {
  6. }
  7.  
  8. public class TweenMove : TweenBase
  9. {
  10.     public Vector3 start;
  11.     public Vector3 end;
  12.     public float time;
  13.     public Tween.EaseType easeType;
  14.     public bool isLocal;
  15.    
  16.     private float timer;
  17.    
  18.     void Update()
  19.     {
  20.         timer += Time.deltaTime;
  21.         if (timer > time)
  22.             timer = time;
  23.         if (isLocal)
  24.             transform.localPosition = start + (end - start) * Tween.EaseValue(easeType, timer/time);
  25.         else
  26.             transform.position = start + (end - start) * Tween.EaseValue(easeType, timer/time);
  27.         if (timer >= time)
  28.             Destroy(this);
  29.     }
  30. }
  31.  
  32. public class TweenScale : TweenBase
  33. {
  34.     public Vector3 start;
  35.     public Vector3 end;
  36.     public float time;
  37.     public Tween.EaseType easeType;
  38.    
  39.     private float timer;
  40.    
  41.     void Update()
  42.     {
  43.         timer += Time.deltaTime;
  44.         if (timer > time)
  45.             timer = time;
  46.         transform.localScale = start + (end - start) * Tween.EaseValue(easeType, timer/time);
  47.         if (timer >= time)
  48.             Destroy(this);
  49.     }
  50. }
  51.  
  52. public class TweenRotate : TweenBase
  53. {
  54.     public Quaternion start;
  55.     public Quaternion end;
  56.     public float time;
  57.     public Tween.EaseType easeType;
  58.     public bool isLocal;
  59.    
  60.     private float timer;
  61.    
  62.     void Update()
  63.     {
  64.         timer += Time.deltaTime;
  65.         if (timer > time)
  66.             timer = time;
  67.         if (isLocal)
  68.             transform.localRotation = Quaternion.Slerp(start, end, Tween.EaseValue(easeType, timer/time));
  69.         else
  70.             transform.rotation = Quaternion.Slerp(start, end, Tween.EaseValue(easeType, timer/time));
  71.         if (timer >= time)
  72.             Destroy(this);
  73.     }
  74. }
  75.  
  76. public class TweenSpin : TweenBase
  77. {
  78.     public float maxAngularSpeed;
  79.     public float angularSpeed;
  80.     public float acceleration;
  81.     public float deceleration;
  82.     public float time;
  83.     public Vector3 axis;
  84.    
  85.     private int state;
  86.    
  87.     void Update()
  88.     {
  89.         if (state == 0)
  90.         {
  91.             angularSpeed += Time.deltaTime * acceleration;
  92.             if (angularSpeed > maxAngularSpeed)
  93.             {
  94.                 angularSpeed = maxAngularSpeed;
  95.                 state = 1;
  96.             }
  97.         }
  98.         else if (state == 1)
  99.         {
  100.             time -= Time.deltaTime;
  101.             if (time < 0f)
  102.                 state = 2;
  103.         }
  104.         else if (state == 2)
  105.         {
  106.             angularSpeed -= Time.deltaTime * deceleration;
  107.             if (angularSpeed < 0f)
  108.                 Destroy(this);
  109.         }
  110.         gameObject.transform.localEulerAngles += angularSpeed * axis * Time.deltaTime;
  111.     }  
  112. }
  113.  
  114. public class Tween : MonoBehaviour
  115. {
  116.     public enum EaseType
  117.     {
  118.         Linear,
  119.         BackIn,
  120.         BackOut,
  121.         BackInOut,
  122.         SineIn,
  123.         SineOut,
  124.         SineInOut,
  125.         CubeIn,
  126.         CubeOut,
  127.         CubeInOut,
  128.         ElasticIn,
  129.         ElasticOut,
  130.         ElasticInOut,
  131.         Max
  132.     }
  133.    
  134.     public static TweenScale ScaleFrom(GameObject gameObject, Vector3 scale, float time, EaseType easeType)
  135.     {
  136.         TweenScale tweenScale = gameObject.AddComponent<TweenScale>();
  137.         tweenScale.start = scale;
  138.         tweenScale.end = gameObject.transform.localScale;
  139.         tweenScale.time = time;
  140.         tweenScale.easeType = easeType;
  141.         return tweenScale;
  142.     }
  143.    
  144.     public static TweenScale ScaleTo(GameObject gameObject, Vector3 scale, float time, EaseType easeType)
  145.     {
  146.         TweenScale tweenScale = (TweenScale)gameObject.AddComponent<TweenScale>();
  147.         tweenScale.start = gameObject.transform.localScale;
  148.         tweenScale.end = scale;
  149.         tweenScale.time = time;
  150.         tweenScale.easeType = easeType;
  151.         return tweenScale;
  152.     }
  153.    
  154.     public static TweenMove MoveFrom(GameObject gameObject, Vector3 position, float time, EaseType easeType, bool isLocal)
  155.     {
  156.         TweenMove tweenMove = gameObject.AddComponent<TweenMove>();
  157.         tweenMove.start = position;
  158.         tweenMove.end = isLocal ? gameObject.transform.localPosition : gameObject.transform.position;
  159.         tweenMove.time = time;
  160.         tweenMove.easeType = easeType;
  161.         tweenMove.isLocal = isLocal;
  162.         return tweenMove;
  163.     }
  164.    
  165.     public static TweenMove MoveTo(GameObject gameObject, Vector3 position, float time, EaseType easeType, bool isLocal)
  166.     {
  167.         TweenMove tweenMove = gameObject.AddComponent<TweenMove>();
  168.         tweenMove.start = isLocal ? gameObject.transform.localPosition : gameObject.transform.position;
  169.         tweenMove.end = position;
  170.         tweenMove.time = time;
  171.         tweenMove.easeType = easeType;
  172.         tweenMove.isLocal = isLocal;
  173.         return tweenMove;
  174.     }
  175.    
  176.     public static TweenRotate RotateFrom(GameObject gameObject, Vector3 eulerAngles, float time, EaseType easeType, bool isLocal)
  177.     {
  178.         TweenRotate tweenRotate = gameObject.AddComponent<TweenRotate>();
  179.         tweenRotate.start = Quaternion.Euler(eulerAngles);
  180.         tweenRotate.end = isLocal ? gameObject.transform.localRotation : gameObject.transform.rotation;
  181.         tweenRotate.time = time;
  182.         tweenRotate.easeType = easeType;
  183.         tweenRotate.isLocal = isLocal;
  184.         return tweenRotate;
  185.     }
  186.    
  187.     public static TweenRotate RotateTo(GameObject gameObject, Vector3 eulerAngles, float time, EaseType easeType, bool isLocal)
  188.     {
  189.         TweenRotate tweenRotate = gameObject.AddComponent<TweenRotate>();
  190.         tweenRotate.start = isLocal ? gameObject.transform.localRotation : gameObject.transform.rotation;
  191.         tweenRotate.end = Quaternion.Euler(eulerAngles);
  192.         tweenRotate.time = time;
  193.         tweenRotate.easeType = easeType;
  194.         tweenRotate.isLocal = isLocal;
  195.         return tweenRotate;
  196.     }
  197.    
  198.     public static TweenSpin Spin(GameObject gameObject, float maxAngularSpeed, float accelerationTime, float decelerationTime, float time, Vector3 axis)
  199.     {
  200.         TweenSpin tweenSpin = gameObject.AddComponent<TweenSpin>();
  201.         tweenSpin.maxAngularSpeed = maxAngularSpeed;
  202.         tweenSpin.acceleration = maxAngularSpeed / accelerationTime;
  203.         tweenSpin.deceleration = maxAngularSpeed / decelerationTime;
  204.         tweenSpin.time = time;
  205.         tweenSpin.axis = axis;
  206.         return tweenSpin;
  207.     }
  208.    
  209.     public static void Stop(GameObject gameObject)
  210.     {
  211.         foreach (TweenBase tweenBase in gameObject.GetComponents<TweenBase>())
  212.         {
  213.             Destroy(tweenBase);
  214.         }
  215.     }
  216.    
  217.     public static int Count(GameObject gameObject)
  218.     {
  219.         return gameObject.GetComponents<TweenBase>().Length;
  220.     }
  221.    
  222.     public static float EaseValue(EaseType easeType, float t)
  223.     {
  224.         switch (easeType)
  225.         {
  226.         case EaseType.BackIn:
  227.             return Ease.BackIn(t);
  228.         case EaseType.BackOut:
  229.             return Ease.BackOut(t);
  230.         case EaseType.BackInOut:
  231.             return Ease.BackInOut(t);
  232.         case EaseType.SineIn:
  233.             return Ease.SineIn(t);
  234.         case EaseType.SineOut:
  235.             return Ease.SineOut(t);
  236.         case EaseType.SineInOut:
  237.             return Ease.SineInOut(t);
  238.         case EaseType.CubeIn:
  239.             return Ease.CubeIn(t);
  240.         case EaseType.CubeOut:
  241.             return Ease.CubeOut(t);
  242.         case EaseType.CubeInOut:
  243.             return Ease.CubeInOut(t);
  244.            
  245.         case EaseType.ElasticIn:
  246.             return Ease.ElasticIn(t, 1f);
  247.         case EaseType.ElasticOut:
  248.             return Ease.ElasticOut(t, 1f);
  249.         case EaseType.ElasticInOut:
  250.             return Ease.ElasticInOut(t, 1f);
  251.         }
  252.         return t;
  253.     }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement