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 Kourosh · Oct 05, 2010 at 04:16 PM · guifade

FadeIn/FadeOut the GUI

I'm trying to fade In my GUI on start and fade out when a button is clicked. I've tried to call my fade in and fade out functions in the both OnGUI and Update. What happens is the fade In works good, but when the button which to fade out the GUI is pressed the GUI suddenly disappears than means directly from 1 to 0 and not gradually.

Here is my code:

var showGUI:boolean = true; var alpha:float;

function Update(){ if(showGUI){ FadeIn(); } else { FadeOut(); } } function FadeOut(){ alpha = Mathf.Lerp(1,0,Time.time*0.5); }

function FadeIn(){ alpha = Mathf.Lerp(0,1,Time.time*0.5); }

function OnGUI(){ GUI.color.a = alpha; if(GUI.Button(Rect(0,0,100,20),"FadeOutGUI"){ showGUI=false; } }

I cant figure out where is the glitch?anyone? thanks,

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

Answer by Jesse Anders · Oct 05, 2010 at 04:44 PM

The t argument to Mathf.Lerp() is clamped to the range [0, 1], and Time.time is the time in seconds since the game was started.

I assume you're observing the fade-in immediately after starting the game. If so, the fade-in is working because over the first two seconds of the game, Time.time falls within the range [0, 2] and therefore t falls within the range [0, 1], and so the effect is as expected.

However, Time.time will continue to increase steadily after that; after 2 seconds, Time.time*0.5 will always be greater than 1, and therefore the t argument will always be 1. As such, Mathf.Lerp(1,0,Time.time*0.5) will generally always return 0, which means the fade out will be instant.

To get the right effect, you'll want to use some other timing method. You could handle the timing manually, or you could use coroutines, I imagine. Also, if you adjust the alpha by applying a velocity (by which I simply mean a signed delta) over time, it might better handle the case where the user clicks on the 'fade out' button before the fade-in has completed. (In this case, ideally, the fade-out would begin at the current alpha rather than at alpha = 1.)

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 Kourosh · Oct 06, 2010 at 07:52 AM 0
Share

Yep, true, here is what i tweaked:

function FadeOut(){ alpha = $$anonymous$$athf.Lerp(alpha,0,Time.deltaTime*5); }

and it works just fine. thanks a lot!

avatar image
1

Answer by dentedpixel · Apr 26, 2013 at 11:40 AM

I added alpha support to my animation engine LeanTween to cut down on that mess of functions you have to write to achieve something as simple as fading a button.

Here is the same result with much less code:

 var buttonRect3:LTRect = new LTRect(0.0, 0.0, 200, 100 );
 
 function OnGUI(){
     if(GUI.Button(buttonRect3.rect, "Alpha")){
         LeanTween.alpha( buttonRect3, 0.0, 1.0, ["ease",LeanTweenType.easeOutQuad] );
     }
 }


[2]: http://dentedpixel.com/developer-diary/rotate-and-fade-gui-elements-in-unity3d-with-leantween/

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.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

making a fadeout with the new GUI in 4.6? 3 Answers

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

fade between scenes? 1 Answer

Fade in from black 4 Answers

How to make 3D Text fade in and out? 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