I wrote a method to animate a panel's widgets alpha:
public static UITweener AnimateAlpha(UIPanel panel, float animTime, float from, float to)
{
int len = panel.widgets.size;
if (len > 0)
{
for (int i = 0; i < len-1; i++)
{
var w = panel.widgets[i];
TweenAlpha.Begin(w.gameObject, animTime, to);
}
return TweenAlpha.Begin(panel.widgets[panel.widgets.size - 1].gameObject, animTime, to);
}
return null;
}
And this (which I give it something like myPanel.GetComponentsInChildren<UIPanel>() just to go recursively down):
public static UITweener AnimateAlpha(UIPanel[] panels, float animTime, float from, float to)
{
int len = panels.Length;
if (len > 0)
{
for (int i = 0; i < len - 1; i++)
{
var panel = panels[i];
AnimateAlpha(panel, animTime, @from, to);
}
return AnimateAlpha(panels[panels.Length - 1], animTime, @from, to);
}
return null;
}
I use it like this:
private void FadeIn()
{
float from = 0.01f, to = 1f;
inventoryPanel.gameObject.SetActive(true);
UIWidget.AnimateAlpha(inventoryPanel.GetComponentsInChildren<UIPanel>(), animTime, from, to);
}
private void FadeOut()
{
float from = 1f, to = 0.01f;
UIWidget.AnimateAlpha(inventoryPanel.GetComponentsInChildren<UIPanel>(), animTime, from, to).onFinished += OnFadeOutFinished;
}
private void OnFadeOutFinished(UITweener tweener)
{
inventoryPanel.gameObject.SetActive(false);
}
Now it works well when fading out, however when fading in things go a little bit funky, and strange, the panel gets active, I see the widgets alpha at full, but I don't see them. They become visible only when I hover over them (See attachment)
Any idea what's going on? - Is there a better way to fade in/out?
Thanks.