- Home /
UI.Graphic.CrossFadeAlpha only works for decrementing values ?
If I use UI.Graphic.CrossFadeAlpha to decrement a value, like this :
//image is a Unity.Engine.UI.Image
image.CrossFadeAlpha(0.0f, 5.0f, false); //we want to make the image completely transparent
the code will work as intented, the image will fade from it's original alpha to being transparent in a matter of 5.0 seconds.
However for some reason if my code is this :
//image is a Unity.Engine.UI.Image
image.CrossFadeAlpha(1.0f, 5.0f, false); //we want to make the image completely opaque
my image's alpha will not change. I have tried both cases with an alpha of 0.0f, 0.5f and 1.0f.
I don't understand what I am doing wrong in such a simple case.
Thank you for the help !
I am having the same issue, fade out works fine, but trying to fade in doesn't. Were you able to find a solution?
I was not able to get the function to work like I wanted to. I wrote my own code to fade in.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
[RequireComponent (typeof (Image))]
public class BlackScreen$$anonymous$$anager : $$anonymous$$onoBehaviour
{
private float _fadeTime;
private bool _isImageFading = false;
private float _initialAlpha;
private float _targetAlpha;
private float _startTime = 0.0f;
private Image _image;
void Start()
{
_image = GetComponent<Image>();
_initialAlpha = _image.color.a;
}
public void FadeBlackScreenToValue(float targetValue, float fadeTime)
{
gameObject.SetActive(true);
_targetAlpha = targetValue;
_fadeTime = fadeTime;
_isImageFading = true;
}
void Update()
{
if(_isImageFading)
{
float timeRatio = _startTime/_fadeTime;
float newAlpha = $$anonymous$$athf.Lerp(_initialAlpha,_targetAlpha,timeRatio);
Color newColor = new Color(0f,0f,0f,newAlpha);
_image.color = newColor;
_startTime += Time.deltaTime;
if(_startTime > _fadeTime)
{
_isImageFading = false;
}
}
}
}
Hey there.
If it looks like there is only 2 conditions of transparency (0 and 1), it is probably bad shader choice.
To use UnityEngine.UI elements, like SetAlpha or CrossFadeAlpha, you need to choose one of those UI shaders in materials you are using for UI elements.
Answer by daleth90 · Mar 09, 2015 at 11:38 AM
Try this! I'm not sure if it's good coding, but at least it works to me.
image.canvasRenderer.SetAlpha( 0.0f );
image.CrossFadeAlpha( 1.0f, duration, false );
Note that I had to put my target alpha to max in the unity3d editor [to 255] for this to work
This works perfectly. I also had to make sure my alpha value of my text was at full alpha. At the start of running the game, it will start from 0.
Answer by tezer86 · Mar 27, 2015 at 02:50 PM
Found out the answer, can't have the initial alpha value at 0, you have to set this to 1. It works fine then.
So make sure the image you want to fade in, has an alpha of 1.
Thanks, worked for me on Unity 5.x. I set the image alpha to 1 in the inspector and to 0 on Start() using image.canvasRenderer.SetAlpha( 0.0f );
as daleth90 mentioned
Yep, this seems to do the trick. If Alpha is at 0 it won't fade to 255. But if it's anything higher to start with it works fine, like so:
color.a = 0.01f; //cannot be totally 0 for CrossFadeAlpha()
image.color = color;
image.CrossFadeAlpha(255f, 10f, false);
Answer by WoozyBytes · Mar 21, 2015 at 06:52 PM
Hi guys i had this problem too after some testing i can say that it does work for me but not as expected.
What i did as a test for fading in was to set CrossFadeAlpha() "duration" parameter to more extreme values, i choose 1000 and it faded fine.
Like this:`_myImage.CrossFadeAlpha( 255 , 1000 , false ) ;`
When fading out it seems that the " duration" parameter works as described in the Unity Docs . So i set it like this : _myImage.CrossFadeAlpha( 0 , 1 , false ) ;
Hope it Helps :)
noting worked till i tried setting the middle value as an extreme; 1000.
Thanks!!
I had one CrossFadeAlpha working perfectly using image.CrossFadeAlpha (1f, 1.2f, true); But when I wanted to add two more UI elements behaving the same, it didn't work despite setting the exact same values & settings on the element. It now works perfectly for the new UI elements when using 255 ins$$anonymous$$d of 1. This seems really buggy.
Answer by brunoleos · Apr 19, 2016 at 04:18 PM
I'm having the same problem with fading in (incrementing the alpha), but none of the workarounds did the job for me. So I wrote a Coroutine for the fading, which works for UI Text:
IEnumerator FadeText(Text text, float target, float duration) {
float totalChange = target - text.color.a;
float changePerSecond = totalChange / duration;
float totalTime = 0;
while (totalTime < duration) {
totalTime += Time.deltaTime;
float increment = Time.deltaTime * changePerSecond;
text.color = new Color(text.color.r, text.color.g, text.color.b, text.color.a + increment);
yield return new WaitForEndOfFrame();
}
text.color = new Color(text.color.r, text.color.g, text.color.b, target);
yield break;
}
And It is called as StartCoroutine(FadeText(textObjectName, 0.25f, 0.5f));
. It could also be extended for other Graphics. Not extensively tested, but working for my cases.
Hope this can be useful, and any comments on improving it will be very appreciated.
Answer by eonyanov · Jun 09, 2016 at 12:03 PM
CrossFadeAlpha() not working with Image.color !!! It is working with
So, first you need to do:"the alpha of the CanvasRenderer color associated with this Graphic".
image.CrossFadeAlpha(0f, 0f, true);
or
image.canvasRenderer.SetAlpha( 0.0f );
Yeah, unfortunately, lots of developers think that this method changing the alpha value of the image.color, dismissing the fact that it has nothing to do with that. It changes the CanvaseRenderer.