Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: NineTails on January 01, 2014, 12:11:07 PM

Title: Fade out a sprite
Post by: NineTails 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?
Title: Re: Fade out a sprite
Post by: Nicki 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.  
Title: Re: Fade out a sprite
Post by: ArenMook 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.