0
\$\begingroup\$

Something that I can accomplish in 5 minutes in libGDX is taking me forever in Unity. I have a Canvas with a button that covers my entire screen. When the button is clicked, I want the button to fade out and then for the underlying UI to be clickable (if the button is just faded out but it's still on top, you can't click anything).

I tried to use crossFadeAlpha on the Image attached to the button, but the problem is that crossFadeAlpha is async, so I can't do image.enabled = false; when it finishes.

Is there any simple way to accomplish this task in Unity?

\$\endgroup\$
  • 1
    \$\begingroup\$ Honestly, I would get a tween engine like DoTween. Makes things simple. \$\endgroup\$ – Draco18s Jul 29 '17 at 3:06
  • \$\begingroup\$ I've tried using a tween engine (iTween and LeanTween). Their documentation is lacking, and I can't find anything online about fading using either of them. \$\endgroup\$ – Morgan Usley Jul 29 '17 at 4:02
  • \$\begingroup\$ Try DoTween. Its is free and pretty easy to use. Syntax isn't what I'm used to (AS3's Tweener library), but I figured it out in about 20 minutes. \$\endgroup\$ – Draco18s Jul 29 '17 at 5:11
  • \$\begingroup\$ Guess you can fade the button right ? Why not disable the button when its alpha reaches 0 ? \$\endgroup\$ – Thalthanas Jul 29 '17 at 6:41
  • \$\begingroup\$ @EmreE that's the problem. How do you execute a function when the alpha reaches 0, when crossFadeAlpha is an async operation. \$\endgroup\$ – Morgan Usley Jul 29 '17 at 19:46
2
\$\begingroup\$

Using a Tween engine may not be necessary here, a simple coroutines will do the job :

private IEnumerator FadeOut( float duration )
{
    Image image = GetComponent<Image>();
    Color color = image.color;
    float startAlpha = color.alpha;
    float endAlpha = 0;

    for ( float t = 0 ; t < duration ; t += Time.deltaTime )
    {
         color.alpha = Mathf.SmoothStep ( startAlpha, endAlpha, t / duration ) ;
         image.color = color;
         yield return null;
    }
    color.alpha = endAlpha;
    image.color = color;
    image.enabled = false;
}

// ....

StartCoroutine ( FadeOut ( 5f ) ) ;

I'm on my phone, I haven't tested and the formatting is bad, sorry.

\$\endgroup\$
  • \$\begingroup\$ There's a slight delay between when the alpha fades to 0 and when the enablement is set to false. Is it possible to execute that transition more seamlessly? \$\endgroup\$ – Morgan Usley Jul 30 '17 at 1:03
  • \$\begingroup\$ Are you sure you run the coroutine only once ? I have forgotten one line in my answer. I have updated it. \$\endgroup\$ – Hellium Jul 30 '17 at 10:22
0
\$\begingroup\$

Try the code below. The update method of the button's script:

private float alpha;

void Update(){
    //Apply fade process here
    if(alpha <= 0){
        GetComponent<Button>().enabled = false;
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.