Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Andrey Rykov · Oct 17, 2013 at 06:50 PM · c#guifade

How to fade out a GUI.Label in GUI.Window?

I have a label in my GUI.Window and I need it to fade in/out. I've found many examples how to fade labels that are simply created in OnGUI() function. But they don't solve my problem. So, is there any solution how to fade in/out a GUI.Label in GUI.Window? Here is the code I tried to use:

 private Color myColor;
 private float alpha = 1f;
     
 void Update() {
     if (Input.GetKeyUp(KeyCode.RightArrow)) {
     while (alpha >= 0) {
     alpha -= Time.deltaTime*0.3f;
 }
 
 void OnGUI () {
     window = GUI.Window (0, window, pauseWindow, "");
 }
 
 void pauseWindow (int windowID) {
     myColor = GUI.color;
     myColor.a = alpha;
     GUI.color = myColor;
     GUI.label = (new Rect(100,100,100,100), "Fading Text");
     myColor.a = 1f;
     GUI.color = myColor;
     GUI.label (new Rect (100,200,100,100), "Simple Text");
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by karljj1 · Oct 17, 2013 at 07:24 PM

Your fading code will cause the alpha to reach 0 in a single frame. You start a loop that fades the alpha value however you never actually break out of the loop until the value is 0. You are blocking unity from continuing until you exit your Update loop.

Try this:

 public class Fade : MonoBehaviour
 {
     private Color myColor;
     private float alpha = 1f;
     private float speed = 1;
 
     private float targetAlpha = 1;
  
     void Update()
     {
         if( Input.GetKeyDown( KeyCode.RightArrow ) )
         {
             Debug.Log( "Fading Out" );
             targetAlpha = 0;
         }
 
         if( Input.GetKeyDown( KeyCode.LeftArrow ) )
         {
             Debug.Log( "Fading In" );
             targetAlpha = 1;
         }
         
         if( !Mathf.Approximately( alpha, targetAlpha ) )
         {
             alpha = Mathf.MoveTowards( alpha, targetAlpha, speed * Time.deltaTime );
         }
     }
  
     void OnGUI ()
     {
         GUI.Window(0, new Rect( 10, 10, 300, 300 ), pauseWindow, "");
     }
 
     void pauseWindow (int windowID) 
     {
         myColor = GUI.color;
         myColor.a = alpha;
         GUI.color = myColor;
         GUI.Label(new Rect(100,100,100,100), "Fading Text");
         myColor.a = 1f;
         GUI.color = myColor;
         GUI.Label(new Rect (100,200,100,100), "Simple Text");
     }
 }
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 Andrey Rykov · Oct 18, 2013 at 10:13 AM 0
Share

Thank You! You helped me a lot.

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.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Is there anyway to fade in/out a GUI image without using an addon/asset? 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Erroe when resizing GUITextures in C# file 1 Answer

Editor Scripting Input Problem OnGUI() 1 Answer


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