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.
using UnityEngine;
using System.Collections;
public class PanelFader : MonoBehaviour {
UIWidget[] fadingWidgets = null;
float[] originalAlpha = null;
float fade = 0;
float fadeDir = 0;
public float fadeSpeed = 1.0f;
public bool beginHidden = true;
float prevRealTime;
void Start()
{
fadingWidgets = GetComponentsInChildren<UIWidget>();
originalAlpha
= new float[fadingWidgets
.Length]; for (int i = 0; i < fadingWidgets.Length; i++)
{
originalAlpha[i] = fadingWidgets[i].alpha;
fadingWidgets[i].alpha = (beginHidden) ? 0 : 1;
}
enabled = false;
if (beginHidden) gameObject.SetActiveRecursively(false);
prevRealTime = Time.realtimeSinceStartup;
}
void Update()
{
float dt = Time.realtimeSinceStartup - prevRealTime;
prevRealTime = Time.realtimeSinceStartup;
fade += fadeDir * dt * fadeSpeed;
fade = Mathf.Clamp01(fade);
for (int i = 0; i < fadingWidgets.Length; i++)
{
fadingWidgets[i].alpha = originalAlpha[i] * fade;
}
if (fadeDir > 0 && fade >= 1)
{
enabled = false;
}
else if (fadeDir < 0 && fade <= 0)
{
enabled = false;
gameObject.SetActiveRecursively(false);
}
}
}
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?