Hello Unity guy!
I learned a lot reading all your questions and answers, untill now I never had the need to write here… but the time is come. ![]()
I’m writing a script for fade in and out an NGUI panel. There’s a script UIPanelAlpha that is working great inside the NGUI package and I want to create an handle script to animate a fade in/out.
Following the script that maybe is useful for someone else:
public class FadePanel : MonoBehaviour
{
public bool isFadeOut = false;
public float timeToFade = 1f;
public float startAlpha = 0f;
public float endAlpha = 1f;
bool isFading = false;
void OnEnable(){
StartCoroutine ( Fade( isFadeOut, timeToFade, startAlpha, endAlpha ));
}
public IEnumerator Fade( bool isFadeOut, float timeToFade, float startAlpha, float endAlpha)
{
float mStart = Time.realtimeSinceStartup;
if (this.gameObject.GetComponent<UIPanel>() != null)
{
UIPanelAlpha panelAlpha = this.GetComponent<UIPanelAlpha>();
if (panelAlpha == null){
panelAlpha = this.gameObject.AddComponent<UIPanelAlpha>();
panelAlpha.alpha = startAlpha;
}
isFading = true;
float alpha = startAlpha;
while (isFading){
if (isFadeOut){
alpha = (timeToFade > 0f) ? 1f - Mathf.Clamp01((Time.realtimeSinceStartup - mStart) / timeToFade) : 0f;
if ((alpha < endAlpha)||(alpha < 0)) {
isFading = false;
}
}
else {
alpha = (timeToFade > 0f) ? 0f + Mathf.Clamp01((Time.realtimeSinceStartup - mStart) / timeToFade) : 1f;
if ((alpha > endAlpha)||(alpha > 1)){
isFading = false;
}
}
panelAlpha.alpha = alpha;
yield return null;
}
Destroy(panelAlpha);
}
Destroy(this);
}
}
In my logic all is working… but something is wrong with the Destroy part.
Seems that the script don’t Destroy the panelAlpha nor itself.
Why?
I was crashing my head through the wall untill I thought that maybe someone of you could help me in this stupid thing…
Thanks!
Ivano