• 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 /
  • Help Room /
avatar image
Question by Dinosaurstic · Jan 14, 2018 at 11:50 PM · uiimageenumerate

[EDIT] How do I slowly make a color turn transparent?

I have some UI which uses an image that I want to turn slowly invisible after being ignored for a while. I tried creating an Enumerator but I can't get it to work right.

     private IEnumerator fadeOut()
     {
         int i = 0;
         bool finished = false;
 
         while (finished == false)
         {
             for (i = 0; i < 5; i++)
             {
                 yield return new WaitForSeconds(1f);
             }
 
             img.color = new Color(img.color.r, img.color.g, img.color.b, img.color.a - 1);
 
             finished = img.color.a < 1 ? false : true;
 
             yield return new WaitForSeconds(0.2f);
 
         }
     }


After a revision, I've edited my code but have the same problem-- the transition isn't smooth, in one frame my alpha levels go from 255 to -1 (rounded up to 0). I'm using an event trigger which uses my functions base on if the mouse goes over my object or leaves it's box.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class boxController : MonoBehaviour {
 
     Animator anim;
     Image img;
     
     Coroutine fade;
     Coroutine show;
 
     void Start () {
         anim = GetComponent<Animator>();
         img = GetComponent<Image>();
         fade = StartCoroutine(Wait());
     }
 
     public void EnterMouse()
     {
         anim.SetBool("isUp", true);
 
         if(fade != null)
         {
             StopCoroutine(fade);
             fade = null;
         }
 
         show = StartCoroutine(FadeIn());
     }
 
     public void ExitMouse()
     {
         anim.SetBool("isUp", false);
 
         if(show != null)
         {
             StopCoroutine(show);
             show = null;
         }
 
         fade = StartCoroutine(Wait());
     }
 
 
     private IEnumerator Wait()
     {
         for (int i = 0; i < 5; i++)
         {
             yield return new WaitForSeconds(1f);
         }
         fade = StartCoroutine(FadeOut());
     }
  
     private IEnumerator FadeOut()
     {
         for (int i = (int)img.color.a - 1; i < 1; i--)
         {
             img.color = new Color(255, 255, 255, i);
             yield return new WaitForSeconds(0.01f);
         }
     }
 
     private IEnumerator FadeIn()
     {
         for(int i = (int)img.color.a + 1; i > 254; i++)
         {
             img.color = new Color(255, 255, 255, i);
             yield return new WaitForSeconds(0.005f);
         }
     } 
 }
 
Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Rugbug_Redfern · Jan 15, 2018 at 12:01 AM

Here's my code: (I made a whole class)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class FadeOut : MonoBehaviour {
 
     public bool fade = false;
     Image img;
     float speed;
 
     void Start() {
         img = GetComponent<Image>();
         FadeOutImage(0.5f);
     }
 
     void FadeOutImage(float _speed) {
         speed = _speed;
         fade = true;
     }
 
     void Update() {
         if(fade) {
             img.color = new Color(img.color.r, img.color.b, img.color.g, img.color.a - speed * Time.deltaTime);
         }
     }
 }

Just call FadeOutImage(float speed) in the place where you want the image to start fading out.

I just try to always stay away from IEnumerators.

Comment
Dinosaurstic

People who like this

1 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 Dinosaurstic · Jan 15, 2018 at 02:56 AM 0
Share

I tried something like this...

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class boxController : MonoBehaviour {
 
     public int fInSpeed = 150;
     public int fOutSpeed = 80;
 
     bool fadingIn = false;
     bool fadingOut = false;
 
     Animator anim;
     Image img;
     
     Coroutine fade;
 
     void Start () {
         anim = GetComponent<Animator>();
         img = GetComponent<Image>();
         fade = StartCoroutine(Wait());
     }
 
     public void EnterMouse()
     {
         anim.SetBool("isUp", true);
 
         if(fade != null)
         {
             StopCoroutine(fade);
             fade = null;
         }
 
         fadingIn = true;
     }
 
     public void ExitMouse()
     {
         anim.SetBool("isUp", false);
 
         fadingIn = false;
         
         fade = StartCoroutine(Wait());
     }
 
     private void Update()
     {
         if (fadingIn && img.color.a < 255)
         {
             img.color = new Color(255, 255, 255, img.color.a + fInSpeed * Time.deltaTime);
             fadingIn = img.color.a < 255 ? true : false;
         }
 
         if (fadingOut && img.color.a > 0)
         {
             img.color = new Color(255, 255, 255, img.color.a - fOutSpeed * Time.deltaTime);
             fadingOut = img.color.a > 0 ? true : false;
         }
 
     }
 
     private IEnumerator Wait()
     {
         for (int i = 0; i < 5; i++)
         {
             yield return new WaitForSeconds(1f);
         }
         fadingOut = true;
     }
 }
 

...but it did not end up working, and roughly set the alpha level to 0 instead of moving down the value smoothly. Do you think it has anything to do with working on an image component instead of a sprite renderer component?

avatar image

Answer by Murddock · Jan 15, 2018 at 01:38 AM

(Srry for my bad english )

You should put the color change inside in the for loop ( i think While doesn't was necessary ), your While loop could stop in alpha 0.9 cause its less than 1 and still be completely visible. (alpha goes from 1 to 0 )

I show you an example of how I had make something like that:

I hope that helps you :)


     IEnumerator ChangeColor () {
         
 
         for (float f = 0f; f <= 4; f += 0.1f) {
             yield return new WaitForSeconds (0.02f);
             Color colorTemp = rend.material.color;
             colorTemp.a -= 0.035f;
             rend.material.color = colorTemporal;
 
         
         }
         yield return null;
 
 
         }
     }
Comment
Dinosaurstic

People who like this

1 Show 0 · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

126 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 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 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

Feathering an edge of an image in the UI? 0 Answers

Working on a character selection screen and trying to get a shader/mask over the characters for a locked look. Looking for help on the approach. 0 Answers

how to change the background image of all the scenes on single click of a button? 0 Answers

Canvas Image UV change on play mode (Shader Lab) 0 Answers

How to have get button display a raw image? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges