- Home /
Fading a UI Image alpha to zero only works once.
After looking at some previous answers I came up with this co routine hoping to have a UI white screen "flash" for the appropriate of time. And it works as expected once, but will not work again;
public IEnumerator LerpWhiteScreen (float duration)
{
Color temp = whiteScreen.color;
temp.a = 1f;
whiteScreen.color = temp;
for (int i = 0; i < duration; i++)
{
whiteScreen.CrossFadeAlpha (0, duration, false);
yield return null;
}
}
note since I can't directly affect the image.color.a, I had to create a "temp color" and effect it's alpha, then assign that value to my image. This is to have the white screen turn completely white, and then fade to 0.
SOLVED
So after doing some research and stumbling across this answer;
http://answers.unity3d.com/questions/1004041/textcrossfadealpha-not-working.html
I have solved this problem by added the line suggested to reset the alpha back to 1;
whiteScreen.canvasRenderer.SetAlpha (1f);
Another thing I have learned here is that the object's alpha needs to be set at 1, and then you can shut it off at runtime in Start ().
So now my code looks like this;
void Start ()
{
whiteScreen.canvasRenderer.SetAlpha (0f);
}
void LerpWhiteScreen (float duration)
{
whiteScreen.canvasRenderer.SetAlpha (1f);
whiteScreen.CrossFadeAlpha (0.0f, duration, true);
}
This now works as expected! Thanks everyone who helped me get to the solution!
(PS, a much easier way could possibly be the following) Color col = whiteScreen.color; //Since its going to get the colors RGBA anyway, we dont need to set anything more
while(col.a > 0.1f){
col.a -= 0.1f;
whiteScreen.color = col;
}
The reason I think why it might only run once is due to where you are calling it? I only know about Update() running automatically per frame, so regardless its being called once per frame, if you have some sort of condition, for example, in a function whenever someone presses a specific button, it then runs that bit of code/function, it should happen every time.
The other cause is that you may never be resetting the alpha?
$$anonymous$$aybe try a if statement like:
if(whiteScreen.color.a < 1f){
Color temp = whiteScreen.color;
temp.a = 1f;
whiteScreen.color = temp;
}
Or in the example before:
Color col = whiteScreen.color;
//Since its going to get the colors RGBA anyway, we dont need to set anything more
if(col.a < 1f){
col.a = 1f;
whiteScreeen.color = col;
}
while(col.a > 0.1f){
col.a -= 0.1f;
whiteScreen.color = col;
}
I'm calling the coroutine in Update ()
So;
void Update() {
if (Input.Get$$anonymous$$eyDown("space"))
StartCorroutine (LerpWhiteScreen (.15));
}
Few things to add as I was trouble shooting this last night;
the object's alpha value doesn't change at all before or after the first successful call of this coroutine. (it starts off at a value of 0 and stays at 0 even if I pause it during the fade out)
The second thing that's weird is if I put a print command print ("we are done with the lerp"); or whatever at the end of the coroutine to showcase it's ran it's course, I get that to print out the first time, and not any time after. It's like something breaks after the first go around.
Hi. The information here helped, thank you.
You don't need to set your object's alpha in Start () Just set the value in the editor that you want at the start of runtime. This is because the scripts don't work with the RGBA values between 0 - 255, but rather a float number 0 - 1f representation of them, where 1f = 255.
Answer by CodeArtist_MX · Dec 04, 2018 at 04:26 AM
Its giving you trouble beacuse there is actually 2 transparency values in UI elements.
There is the BaseColor that you access in the unity inspector and there is an internal one accessed only through the CanvasRenderer.
If you want to use CrossFadeAlpha or CrossFadeColor you will have to change the UI element properties using the CanvasRender component.
whiteScreen.canvasRenderer.SetAlpha (1f);
whiteScreen.canvasRenderer.SetColor(Color.clear);
Answer by TobyKaos · Feb 03, 2016 at 09:22 AM
Do not use a coroutine.
just
img.color = new Vector4(1f,1f,1f,1f);
img.CrossFadeAlpha(0.0f, 1.0f, true);
CrossFadeAlpha do the work. It is a tween. @Rick74
I'll try this when I get home, thanks. I prefer coroutines though, it would be nice if this ran fine inside of one.
Ok I did this and theirs a weird bug going on here that I can't explain. The bug is, that whatever value I place as the finishing alpha value in CrossFadeAlpha, becomes the $$anonymous$$AXI$$anonymous$$U$$anonymous$$ value that the image alpha can now be.
That is if I set;
img.CrossFadeAlpha(0.1f, 1.0f, true);
.1f is now the maximum that the alpha of that image can be. It won't allow itself to be reset to a value of 1. Inserted a screenshot to showcase this behavior at runtime.
[1]: /storage/temp/63231-bug.jpg
entire code;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CameraController : $$anonymous$$onoBehaviour {
public Image whiteScreen;
void Update ()
{
if (Input.Get$$anonymous$$eyDown ("space"))
LerpWhiteScreen(.15f);
}
void LerpWhiteScreen (float duration)
{
print ("hit space");
whiteScreen.color = new Vector4 (1f, 1f, 1f, 1f);
whiteScreen.CrossFadeAlpha (0.1f, duration, true);
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
RawImage to world 2 Answers
Retrieve screenshot image 0 Answers