• 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
0
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
Add comment
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
1

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
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 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 : $$anonymous$$onoBehaviour {
 
     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 Enter$$anonymous$$ouse()
     {
         anim.SetBool("isUp", true);
 
         if(fade != null)
         {
             StopCoroutine(fade);
             fade = null;
         }
 
         fadingIn = true;
     }
 
     public void Exit$$anonymous$$ouse()
     {
         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 ins$$anonymous$$d of moving down the value smoothly. Do you think it has anything to do with working on an image component ins$$anonymous$$d of a sprite renderer component?

avatar image
1

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

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

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

Black UI when building to android 1 Answer

Need help with shield bar enable and disable. 0 Answers

How can I make Image border not change a size? 1 Answer

Bug with change Image.sprite via script in 2019 0 Answers

How do I use anchors in UI? 0 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