Author Topic: Tweening relative offset in anchor  (Read 9468 times)

nkls

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 29
    • View Profile
Tweening relative offset in anchor
« 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?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Tweening relative offset in anchor
« Reply #1 on: November 19, 2012, 05:25:01 PM »
That's perfectly fine.

renardmf

  • Guest
Re: Tweening relative offset in anchor
« Reply #2 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.

renardmf

  • Guest
Re: Tweening relative offset in anchor
« Reply #3 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.  

nkls

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Tweening relative offset in anchor
« Reply #4 on: April 02, 2013, 05:11:27 AM »
sorry for answering late, glad you fixed it! :)

whitedrow

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Tweening relative offset in anchor
« Reply #5 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

zyap

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Tweening relative offset in anchor
« Reply #6 on: May 25, 2015, 04:00:23 PM »
Anyone else got the TweenOffset to work for UIPanel yet?
Tried this, no luck at all.