Author Topic: New tween CirclePosition  (Read 4332 times)

lenneth78

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
New tween CirclePosition
« on: March 15, 2013, 04:59:32 AM »
Hi,

For my needs, I developed anew tween.It moves circularly. This is basic but nice.

  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2012 Tasharen Entertainment
  4. //----------------------------------------------
  5.  
  6. using UnityEngine;
  7.  
  8. /// <summary>
  9. /// Tween the object's position on circle.
  10. /// </summary>
  11.  
  12. [AddComponentMenu("NGUI/Tween/CirclePosition")]
  13. public class TweenCirclePosition : UITweener
  14. {
  15.     public Vector3 from;
  16.     public Vector3 to;
  17.     [Range(0,360)]
  18.     public float angleMax = 180;
  19.     Transform mTrans;
  20.  
  21.     public Transform cachedTransform { get { if (mTrans == null) mTrans = transform; return mTrans; } }
  22.     public Vector3 position { get { return cachedTransform.localPosition; } set { cachedTransform.localPosition = value; } }
  23.  
  24.     private Vector3 cachedFrom;
  25.     private Vector3 cachedTo;
  26.     private Vector3 cachedCenter;
  27.     private Vector3 cachedDirection;
  28.     private readonly Vector3 Z = new Vector3(0, 0, -1);
  29.  
  30.     override protected void OnUpdate(float factor, bool isFinished)
  31.     {
  32.         if (cachedTo != to || cachedFrom != from)
  33.         {
  34.             cachedDirection = (to - from) * 0.5F;
  35.             cachedCenter = from + cachedDirection; //center between from and to
  36.             cachedFrom = from;
  37.             cachedTo = to;
  38.             cachedDirection *= -1;  //inverse direction for start begin at From
  39.  
  40.         }
  41.        
  42.         cachedTransform.localPosition = (Quaternion.AngleAxis(((1F - factor) * angleMax), Z) * cachedDirection) + cachedCenter;
  43.     }
  44.  
  45.     public void setByRadius(Vector3 begin, Vector3 direction, float radius)
  46.     {
  47.         to = direction * 2 * radius + begin;
  48.         from = begin;
  49.     }
  50.  
  51.     public void setByPosition(Vector3 from,Vector3 to)
  52.     {
  53.         this.from = from;
  54.         this.to = to;
  55.     }
  56.  
  57.     public void setByCenter(Vector3 center, float radius)
  58.     {
  59.         from = center;
  60.         to = center;
  61.         from.y += radius;
  62.         to.y -= radius;
  63.     }
  64.  
  65.     /// <summary>
  66.     /// Start the tweening operation.
  67.     /// </summary>
  68.  
  69.     static public TweenCirclePosition Begin(GameObject go, float duration, Vector3 pos)
  70.     {
  71.         TweenCirclePosition comp = UITweener.Begin<TweenCirclePosition>(go, duration);
  72.         comp.from = comp.position;
  73.         comp.to = pos;
  74.  
  75.         if (duration <= 0f)
  76.         {
  77.             comp.Sample(1f, true);
  78.             comp.enabled = false;
  79.         }
  80.         return comp;
  81.     }
  82. }
« Last Edit: March 15, 2013, 08:43:25 AM by lenneth78 »

lenneth78

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: New tween CirclePosition
« Reply #1 on: March 15, 2013, 08:44:33 AM »
Update for added 3 new functions for comfort

lenneth78

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: New tween CirclePosition
« Reply #2 on: March 16, 2013, 05:05:06 PM »
For people who want rotate sprite on 360° with tweenrotation replace OnUpdate by :

  1.         override protected void OnUpdate (float factor, bool isFinished)
  2.         {
  3.         cachedTransform.localRotation = Quaternion.Euler(from + (to - from) * factor);
  4.         }