Author Topic: Add a Tween method to UIPanelAlpha  (Read 7979 times)

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Add a Tween method to UIPanelAlpha
« on: November 20, 2012, 09:54:19 AM »
Hi there !

I suppose almost everybody use UIPanelAlpha with a script or something to decrease the alpha of a panel and not just set a hard alpha value.

So my question is, can you add a simple tweening method to this class to change the opacity from a value to another one ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Add a Tween method to UIPanelAlpha
« Reply #1 on: November 20, 2012, 03:43:17 PM »
That script was written for use with Unity animations, however I can add support to TweenAlpha as well.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Add a Tween method to UIPanelAlpha
« Reply #2 on: November 20, 2012, 03:45:53 PM »
  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 alpha.
  10. /// </summary>
  11.  
  12. [AddComponentMenu("NGUI/Tween/Alpha")]
  13. public class TweenAlpha : UITweener
  14. {
  15.         public float from = 1f;
  16.         public float to = 1f;
  17.  
  18.         Transform mTrans;
  19.         UIWidget mWidget;
  20.         UIPanelAlpha mPanelAlpha;
  21.  
  22.         /// <summary>
  23.         /// Current alpha.
  24.         /// </summary>
  25.  
  26.         public float alpha
  27.         {
  28.                 get
  29.                 {
  30.                         if (mWidget != null) return mWidget.alpha;
  31.                         if (mPanelAlpha != null) return mPanelAlpha.alpha;
  32.                         return 0f;
  33.                 }
  34.                 set
  35.                 {
  36.                         if (mWidget != null) mWidget.alpha = value;
  37.                         else if (mPanelAlpha != null) mPanelAlpha.alpha = value;
  38.                 }
  39.         }
  40.  
  41.         /// <summary>
  42.         /// Find all needed components.
  43.         /// </summary>
  44.  
  45.         void Awake ()
  46.         {
  47.                 mPanelAlpha = GetComponent<UIPanelAlpha>();
  48.                 if (mPanelAlpha == null) mWidget = GetComponentInChildren<UIWidget>();
  49.         }
  50.  
  51.         /// <summary>
  52.         /// Interpolate and update the alpha.
  53.         /// </summary>
  54.  
  55.         override protected void OnUpdate (float factor, bool isFinished) { alpha = Mathf.Lerp(from, to, factor); }
  56.  
  57.         /// <summary>
  58.         /// Start the tweening operation.
  59.         /// </summary>
  60.  
  61.         static public TweenAlpha Begin (GameObject go, float duration, float alpha)
  62.         {
  63.                 TweenAlpha comp = UITweener.Begin<TweenAlpha>(go, duration);
  64.                 comp.from = comp.alpha;
  65.                 comp.to = alpha;
  66.  
  67.                 if (duration <= 0f)
  68.                 {
  69.                         comp.Sample(1f, true);
  70.                         comp.enabled = false;
  71.                 }
  72.                 return comp;
  73.         }
  74. }

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Re: Add a Tween method to UIPanelAlpha
« Reply #3 on: November 21, 2012, 11:27:00 AM »
Thanks a lot !

oriam

  • Guest
Re: Add a Tween method to UIPanelAlpha
« Reply #4 on: January 05, 2013, 06:58:36 PM »
I was using TweenAlpha without UIPanelAlpha (I'm new to NGUI). Even though the Tween script was in the panel, it only fade the "fade button", not the entire panel. This fix is quite useful. Hopefully it will make it to a public release :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Add a Tween method to UIPanelAlpha
« Reply #5 on: January 05, 2013, 07:05:59 PM »
It has. :P

oriam

  • Guest
Re: Add a Tween method to UIPanelAlpha
« Reply #6 on: January 05, 2013, 07:13:28 PM »
Oh nice :) just updated and checked it out, thank you  ;)