Author Topic: My fader script kills my button  (Read 3977 times)

angrypenguin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
My fader script kills my button
« on: June 01, 2012, 09:00:14 PM »
I just recently started using NGUI, so I expect I'm missing something here.

I've got a button in a panel, working exactly as it should.

I then wrote a panel fader script, which I intend to use to turn panels on and off for different parts of my GUI as they are needed. The code is below, minus some event management code.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PanelFader : MonoBehaviour {
  5.  
  6.         UIWidget[] fadingWidgets = null;
  7.         float[] originalAlpha = null;
  8.        
  9.         float fade = 0;
  10.         float fadeDir = 0;
  11.        
  12.         public float fadeSpeed = 1.0f;
  13.         public bool beginHidden = true;
  14.        
  15.         float prevRealTime;
  16.        
  17.         void Start()
  18.         {
  19.                 fadingWidgets = GetComponentsInChildren<UIWidget>();   
  20.                 originalAlpha = new float[fadingWidgets.Length];
  21.                 for (int i = 0; i < fadingWidgets.Length; i++)
  22.                 {
  23.                         originalAlpha[i] = fadingWidgets[i].alpha;
  24.                         fadingWidgets[i].alpha = (beginHidden) ? 0 : 1;
  25.                 }
  26.                
  27.                 enabled = false;
  28.                 if (beginHidden) gameObject.SetActiveRecursively(false);
  29.                
  30.                 prevRealTime = Time.realtimeSinceStartup;
  31.         }
  32.        
  33.         void Update()
  34.         {
  35.                 float dt = Time.realtimeSinceStartup - prevRealTime;
  36.                 prevRealTime = Time.realtimeSinceStartup;
  37.                
  38.                 fade += fadeDir * dt * fadeSpeed;
  39.                 fade = Mathf.Clamp01(fade);
  40.                 for (int i = 0; i < fadingWidgets.Length; i++)
  41.                 {
  42.                         fadingWidgets[i].alpha = originalAlpha[i] * fade;
  43.                 }
  44.                 if (fadeDir > 0 && fade >= 1)
  45.                 {
  46.                         enabled = false;
  47.                 }
  48.                 else if (fadeDir < 0 && fade <= 0)
  49.                 {
  50.                         enabled = false;
  51.                         gameObject.SetActiveRecursively(false);
  52.                 }
  53.         }
  54. }
  55.  

The panel fades in and out fine, except that it kills the button when "beginHidden" is true. When I mouse over the button it highlights fine, but when I move the mouse away instead of going back to its normal colour it fades out completely.

I'm guessing that what's happening is that my script is running before NGUI, causing NGUI to grab the completely faded out colour instead of the normal colour. But that's only a guess.

So, the hopefully quick question, is the above approach how I should look at making bits of a GUI appear and disappear?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: My fader script kills my button
« Reply #1 on: June 02, 2012, 12:23:15 AM »
You wrote your own panel fading? Did you see this post? http://www.tasharen.com/forum/index.php?topic=507.msg2460#msg2460

TweenColor grabs the current color when it's activated, whatever the current color may be. In your case it's the alpha=0 color it seems. Simple fix -- delay everything you do in Start() until Update().

MetaMythril

  • Guest
Re: My fader script kills my button
« Reply #2 on: June 02, 2012, 04:13:30 PM »
I had this same issue. An alternative is to have the object activate off screen fully visible. This will initialize the tween colors and such correctly then you can fade the panel in and out when needed. Since my panel fades in as it travels in from off screen, this worked fine enough for my case.