Author Topic: Fade out a sprite  (Read 4309 times)

NineTails

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Fade out a sprite
« on: January 01, 2014, 12:11:07 PM »
I'm trying to have some of my GUI elements fade out. However, I've spent the day trying to do this and I've gotten no where. I know its a matter of decressing the alpha value to fade the image out, but what I've been doing isn't working.

Has anyone ever managed to make an NGUI object fade out on the screen? If so, could you please inform me as to how you've done it?

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Fade out a sprite
« Reply #1 on: January 01, 2014, 12:28:57 PM »
  1. public UIWidget widgetReference;
  2.  
  3. void SomeFadeOutStarterMethod()
  4. {
  5.   StartCoroutine(FadeOuter(widgetReference, 0.5f));
  6. }
  7.  
  8.  
  9. IEnumerator FadeOuter(UIWidget w, float durationInSeconds)
  10. {
  11.   float startA = w.alpha;
  12.   float currentTime = 0f;
  13.   while(currentTime < durationInSeconds)
  14.   {
  15.     w.alpha = Mathf.Lerp(startA,0f, currentTime / durationInSeconds);
  16.     currentTime += Time.deltaTime;
  17.     yield return null;
  18.   }
  19.   w.alpha = 0f;
  20. }
  21.  
  22.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fade out a sprite
« Reply #2 on: January 01, 2014, 07:33:25 PM »
Fade out over 0.5 seconds: TweenAlpha.Begin(targetWidgetGameObject, 0.5f, 0f);
Fade in over 0.25 seconds: TweenAlpha.begin(targetWidgetGameObject, 0.25f, 1f);

Since alpha is cumulative (parent alpha affects children), all children of your "targetWidgetGameObject" will fade out automatically.