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
1
Question by paco morales · Jan 23, 2013 at 12:55 AM · guibuttonalpha

How to change the Alpha channel of a GUI button?

Hello everyone,

I'm trying to change the Alpha channel of one of my GUI buttons, so when I hit the button the alpha changes the amount to "0" and disappears. Also I want to change the speed of it.

Please take a look to my script, any advice is welcome.

 var Skin_buttonOne : GUISkin;
 var Skin_buttonTwo : GUISkin;
 var beep : AudioClip;
     
 function OnGUI(){
  
     GUI.color.a = 1;
     
       GUI.skin = Skin_buttonOne;
     if (GUI.Button(Rect(870,470,80,39),"")){
     audio.PlayOneShot(beep);     
     GUI.color.a = 0;
       
     }
 
     GUI.skin = Skin_buttonTwo;
     if (GUI.Button(Rect(870,512,80,80),"")){
     audio.PlayOneShot(beep);
  
     }
 }
 
 @script RequireComponent(AudioSource)
 



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

Answer by aldonaletto · Jan 23, 2013 at 01:54 AM

You should have a boolean flag to enable alpha decrement: set it to true when the button is pressed, so that the button starts vanishing after being pressed. Alpha must be decremented in Update, since Time.deltaTime must not be used inside OnGUI (it shows the time between frames, not between OnGUI calls). Additionally, you should stop drawing the button when it's invisible to avoid embarrasing clicks!

 var Skin_buttonOne : GUISkin;
 var Skin_buttonTwo : GUISkin;
 var beep : AudioClip;
 var duration : float = 1.5; // time taken to disappear
 private var alpha: float = 1; // alpha starts at 1
 private var vanish = false;
 
 function Update(){
   if (vanish){ // only decrement alpha after button pressed
     alpha -= Time.deltaTime/duration;
   }
 }
 
 function OnGUI(){
   GUI.color.a = alpha; // set alpha for this button
   if (alpha > 0){ // only draw the button while it's visible
     GUI.skin = Skin_buttonOne;
     if (GUI.Button(Rect(870,470,80,39),"")){
       audio.PlayOneShot(beep);
       vanish = true; // enable alpha decrement
     }
   }
   GUI.color.a = 1; // restore alpha for other GUI items
   GUI.skin = Skin_buttonTwo;
   if (GUI.Button(Rect(870,512,80,80),"")){
     audio.PlayOneShot(beep);
   }
 }

 @script RequireComponent(AudioSource)
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 paco morales · Jan 29, 2013 at 09:39 AM 0
Share

Thanks Aldo works Perfectly :)

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

10 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

Related Questions

Can I use a movie texture with alpha as a GUI object? 1 Answer

GUI button alpha images 1 Answer

guiTexture color half alpha White? 1 Answer

Move GUI elements. 0 Answers

Using GUI and check what button was pressed 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