- Home /
making a fadeout with the new GUI in 4.6?
I figured that making a fadeout transition with the new GUI system would be easy, my plan was: create a black panel (Image component) that fills the whole screen, control the alpha with a Mathf.Lerp and there! fadeout accomplished!.... the problem is, I have no idea how to access the alpha of the Image Component... is there a way to do that? or maybe an even easier way to do fadeouts/fadeins with this new Gui system? thanks in advance!
Answer by Stardog · Dec 20, 2014 at 12:22 AM
Use a CanvasGroup component on the parent panel (not the Image), and fade the Alpha of that.
using UnityEngine;
using System.Collections;
public class GUIFadeScript : MonoBehaviour {
public CanvasGroup fadeCanvasGroup;
public IEnumerator FadeToBlack(float speed)
{
while (fadeCanvasGroup.alpha < 1f)
{
fadeCanvasGroup.alpha += speed * Time.deltaTime;
yield return null;
}
}
}
Adding CanvasGroup component to any parent UO container and manipulating its alpha seems to be the way to go for fading in/out, or swapping objects in general
Answer by kacyesp · Aug 31, 2014 at 03:39 AM
You should use the gui element's color field. color.a represents the alpha value, so that's what you should change.
$$anonymous$$acyesp beat me to the punch. I will just add one more point.
I would suggest using a canvas group to do fading since the alpha of an image does not always animate well.
Regards,
Answer by el-RERS · Aug 31, 2014 at 04:00 AM
Thanks a lot! but when using this code:
myImage.color = new Color(0.0f, 0.0f, 0.0f, Mathf.Lerp(myImage.color.a, 0.0f, 255f * Time.deltaTime));
I'm getting this error: "Object Reference not set to an instance of an object" what's up with that? :(
You have to have an image. I just used myImage as an example. You can get the image component by going
Image myImage = GetComponent<Image>();
myImage.color = new Color(0.0f, 0.0f, 0.0f, $$anonymous$$athf.Lerp(myImage.color.a, 0.0f, Time.deltaTime));
Also colours are between 0 - 1 not 0 - 255. Just a heads up :)
well, I did put this:
void Awake(){ myImage = GetComponent ();
}
is that what you meant? thanks in advance.
Your answer
Follow this Question
Related Questions
fade in out multi images then go to next scene 1 Answer
Unity 4.7 - OnGUI prevent click/touch through 0 Answers
4.6 GUI Button inside a scrolling Text Box 0 Answers
Fade In GUI on proximity 1 Answer
IPointerClickHandler error 2 Answers