Author Topic: Sprite > TweenAlpha?  (Read 12315 times)

Adajos

  • Guest
Sprite > TweenAlpha?
« on: September 05, 2012, 05:40:34 AM »
I need to fade-in or -out a sprite with alpha. Is there a way to do that? can't find a TweenAlpha script for it. Fading the whole panel is not my goal. Just want to tween the alpha from a specific sprite. Thnx in forward!!!!
« Last Edit: September 05, 2012, 08:31:32 AM by Adajos »

PhilipC

  • Guest
Re: Sprite > TweenAlpha?
« Reply #1 on: September 05, 2012, 08:56:26 AM »
Every sprite has a Color Tint property you should be able to write a script which will effect only the alpha of this property. Or if you wish you can use TweenColor assigning the To and From colors to be the same but with different alphas.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sprite > TweenAlpha?
« Reply #2 on: September 05, 2012, 03:51:18 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.  
  21.         /// <summary>
  22.         /// Current alpha.
  23.         /// </summary>
  24.  
  25.         public float alpha { get { return mWidget.alpha; } set { mWidget.alpha = value; } }
  26.  
  27.         /// <summary>
  28.         /// Find all needed components.
  29.         /// </summary>
  30.  
  31.         void Awake () { mWidget = GetComponentInChildren<UIWidget>(); }
  32.  
  33.         /// <summary>
  34.         /// Interpolate and update the alpha.
  35.         /// </summary>
  36.  
  37.         override protected void OnUpdate (float factor, bool isFinished) { alpha = Mathf.Lerp(from, to, factor); }
  38.  
  39.         /// <summary>
  40.         /// Start the tweening operation.
  41.         /// </summary>
  42.  
  43.         static public TweenAlpha Begin (GameObject go, float duration, float alpha)
  44.         {
  45.                 TweenAlpha comp = UITweener.Begin<TweenAlpha>(go, duration);
  46.                 comp.from = comp.alpha;
  47.                 comp.to = alpha;
  48.                 return comp;
  49.         }
  50. }

Adajos

  • Guest
Re: Sprite > TweenAlpha?
« Reply #3 on: September 06, 2012, 04:22:22 AM »
Thnx for the great support! Suggest to pack it in to the next release. It helped me alot! Thnx again!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sprite > TweenAlpha?
« Reply #4 on: September 06, 2012, 06:56:25 AM »
Yup, you will find it in the next release as well.

mikechurch

  • Guest
Re: Sprite > TweenAlpha?
« Reply #5 on: September 17, 2012, 07:07:41 PM »
Little problem there, but I changed:

override protected void OnUpdate (float factor, bool isFinished) { alpha = Mathf.Lerp(from, to, factor); }

Which generated:

Assets/NGUI/Tweening/TweenAlpha.cs(37,29): error CS0115: `TweenAlpha.OnUpdate(float, bool)' is marked as an override but no suitable method found to override


to (remove bool isFinished):

override protected void OnUpdate (float factor) { alpha = Mathf.Lerp(from, to, factor); }

And it worked.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sprite > TweenAlpha?
« Reply #6 on: September 18, 2012, 05:21:59 AM »
Are you using an old version of NGUI?