• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by Mogrinnar · Jan 21, 2015 at 02:45 AM · alphaopaque

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 !

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nonabonasonadona · Jan 26, 2015 at 04:22 AM 0
Share

I am having the same issue, fade out works fine, but trying to fade in doesn't. Were you able to find a solution?

avatar image Mogrinnar · Jan 26, 2015 at 04:33 PM 0
Share

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;
             }
         }
     }
 }
avatar image nanom · Nov 23, 2017 at 03:53 AM 0
Share

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.

10 Replies

· Add your reply
  • Sort: 
avatar image
14

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 );
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JiMMaR · Mar 17, 2015 at 04:44 PM 1
Share

Note that I had to put my target alpha to max in the unity3d editor [to 255] for this to work

avatar image DimitriUK · May 05, 2015 at 01:25 AM 0
Share

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.

avatar image
9

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image giorashc · May 03, 2016 at 08:17 PM 1
Share

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

avatar image liju_techtreeit · Dec 27, 2016 at 07:30 AM 0
Share

Thanks, worked for me

avatar image andrew_2992 · Nov 17, 2017 at 09:58 AM 0
Share

Thanks, worked for me too.

avatar image WanderingSumo · Apr 29, 2018 at 06:31 AM 0
Share

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);
avatar image
2

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 :)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image knickerbocker · Sep 28, 2015 at 05:00 AM 0
Share

noting worked till i tried setting the middle value as an extreme; 1000.

Thanks!!

avatar image WoozyBytes knickerbocker · Oct 01, 2015 at 11:44 AM 0
Share

No problem ! Regards :)

avatar image TooManySugar · Oct 11, 2015 at 10:07 PM 0
Share

O$$anonymous$$G this trick works!

avatar image N-Dream-AG · May 06, 2016 at 08:22 AM 0
Share

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.

avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by eonyanov · Jun 09, 2016 at 12:03 PM

CrossFadeAlpha() not working with Image.color !!! It is working with

"the alpha of the CanvasRenderer color associated with this Graphic".

So, first you need to do: image.CrossFadeAlpha(0f, 0f, true); or image.canvasRenderer.SetAlpha( 0.0f );
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ben-rasooli · May 01, 2017 at 01:23 AM 0
Share

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.

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

46 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dirt / Dust Shader, Light Interaction 0 Answers

Make an object transparent when below 1.0A, and completely opaque at 1.0A? 3 Answers

Opaque to Transparent animation effect on a textured object (Lerpz)? 1 Answer

How do I use Movie Texture with alpha channel? 4 Answers

Acces the secondary colors in the shaders. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges