Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: jz87 on July 28, 2012, 03:20:14 AM

Title: Controlling the opacity of a panel
Post by: jz87 on July 28, 2012, 03:20:14 AM
I would like to be able to fade in/out a panel (and all of its contents). I'm not familiar with how NGUI does drawing under the covers, so I'm wondering if there is some shader I can modify to control the opacity of NGUI objects.
Title: Re: Controlling the opacity of a panel
Post by: reptilebeats on July 28, 2012, 05:52:37 AM
you could always change the alpha, in colour under the widget. i don't know if this helps but it will make it transparent. you could always then make a script which pulls the alpha down from 255 to 0 and back up again during runtime. then you could say if alpha is below 100 or something then disable what ever components you want
Title: Re: Controlling the opacity of a panel
Post by: ArenMook on July 28, 2012, 11:46:51 AM
Just a note, NGUI uses "Color", which uses floating point values, so 0-1 not 0-255.

There are multiple posts on the forums that explains how to fade an entire panel -- and give code to do just that. I'll repost here, for your convenience.

  1. using UnityEngine;
  2.  
  3. public class FadePanel : MonoBehaviour
  4. {
  5.     public float duration = 1f;
  6.  
  7.     float mStart = 0f;
  8.     UIWidget[] mWidgets;
  9.  
  10.     void Start ()
  11.     {
  12.         mStart = Time.realtimeSinceStartup;
  13.         mWidgets = GetComponentsInChildren<UIWidget>();
  14.     }
  15.  
  16.     void Update ()
  17.     {
  18.         float alpha = (duration > 0f) ? 1f - Mathf.Clamp01((Time.realtimeSinceStartup - mStart) / duration) : 0f;
  19.  
  20.         foreach (UIWidget w in mWidgets)
  21.         {
  22.             Color c = w.color;
  23.             c.a = alpha;
  24.             w.color = c;
  25.         }
  26.         if (alpha == 0f) Destroy(this);
  27.     }
  28. }
Title: Re: Controlling the opacity of a panel
Post by: reptilebeats on July 29, 2012, 11:12:46 AM
yh i changed mine so that i can use 0 - 255 just to make my life easier, personal preference