Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: nkls on November 19, 2012, 06:32:17 AM

Title: Tweening relative offset in anchor
Post by: nkls on November 19, 2012, 06:32:17 AM
Hi,
I have made a custom script that tweens the relative offset in an anchor. I want to animate widgets from the left of my screen to a relative position, say 75%. The reason why I am doing it this way, is because I want the animation to be independent of screen size.  The tween-position doesnt work if the width of the screen changes to much.

My script works fine, and everything seems to be ok. My hierarchy looks like this:

UIRoot
  - UIAnchor (this object also has a UIAnchorTween-script and a Panel script attached.)
          -Widgets
 
Is this the right way to do this? Or is there a better way?
Title: Re: Tweening relative offset in anchor
Post by: ArenMook on November 19, 2012, 05:25:01 PM
That's perfectly fine.
Title: Re: Tweening relative offset in anchor
Post by: renardmf on March 31, 2013, 06:19:50 PM
I was trying to accomplish the same thing. Would you mind sharing this script? Or, is there another way to accomplish the same thing now in NGUI?

Thanks in advance.
Title: Re: Tweening relative offset in anchor
Post by: renardmf on March 31, 2013, 08:18:19 PM
I figured it out. I just used the default NGUI tween scripts as a reference and created a TweenOffset Script. I have included it in this post in case anyone finds it useful.

Cheers!

  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2012 Tasharen Entertainment
  4. //----------------------------------------------
  5.  
  6. using UnityEngine;
  7.  
  8. /// <summary>
  9. /// Tween the anchors's relative offset.
  10. /// </summary>
  11.  
  12. [AddComponentMenu("NGUI/Tween/Offset")]
  13. public class TweenOffset : UITweener
  14. {
  15.         public Vector2 from;
  16.         public Vector2 to;
  17.  
  18.         Transform mTrans;
  19.         UIAnchor mAnchor;
  20.  
  21.         /// <summary>
  22.         /// Current offset.
  23.         /// </summary>
  24.  
  25.         public Vector2 offset
  26.         {
  27.                 get
  28.                 {
  29.                         if (mAnchor != null) return mAnchor.relativeOffset;
  30.                         return Vector2.zero;
  31.                 }
  32.                 set
  33.                 {
  34.                         if (mAnchor != null) mAnchor.relativeOffset = value;
  35.                 }
  36.         }
  37.        
  38.         /// <summary>
  39.         /// Find all needed components.
  40.         /// </summary>
  41.  
  42.         void Awake ()
  43.         {
  44.                 mAnchor = GetComponent<UIAnchor>();
  45.         }
  46.  
  47.         /// <summary>
  48.         /// Interpolate and update the offset.
  49.         /// </summary>
  50.  
  51.         override protected void OnUpdate (float factor, bool isFinished) { offset = from * (1f - factor) + to * factor;  }
  52.  
  53.         /// <summary>
  54.         /// Start the tweening operation.
  55.         /// </summary>
  56.  
  57.         static public TweenOffset Begin (GameObject go, float duration, Vector2 offset)
  58.         {
  59.                 TweenOffset comp = UITweener.Begin<TweenOffset>(go, duration);
  60.                 comp.from = comp.offset;
  61.                 comp.to = offset;
  62.  
  63.                 if (duration <= 0f)
  64.                 {
  65.                         comp.Sample(1f, true);
  66.                         comp.enabled = false;
  67.                 }
  68.                 return comp;
  69.         }
  70. }
  71.  
Title: Re: Tweening relative offset in anchor
Post by: nkls on April 02, 2013, 05:11:27 AM
sorry for answering late, glad you fixed it! :)
Title: Re: Tweening relative offset in anchor
Post by: whitedrow on March 05, 2014, 12:15:06 PM
Hi,

the Script ist exactly what I need but I can't get it to work.

Can you help me on how do I use the script?

Greetings
Title: Re: Tweening relative offset in anchor
Post by: zyap on May 25, 2015, 04:00:23 PM
Anyone else got the TweenOffset to work for UIPanel yet?
Tried this, no luck at all.