Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by chchrist · Mar 09, 2010 at 07:47 PM · guigui-button

How to toggle the color of a button?

Hello,

I wonder if it is possible to make a button toggle its color. Any ideas how to do it?

Thank you in advance.

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

Answer by duck · Mar 09, 2010 at 08:53 PM

If you're doing this with the new OnGUI system, and you specifically want it to look like a Button, and not a Toggle, you'll need to make a GUI.Toggle, but tell it to use the 'button' style from your custom GUISkin. It would end up looking something like this...

In C#

public GUISkin guiSkin; public bool toggleBool = false; Rect toggleRect = new Rect(10, 10, 150, 100);

void OnGUI() { toggleBool = GUI.Toggle(toggleRect , toggleBool, "Hi!", guiSkin.button);
}

Or in Javascript

var guiSkin : GUISkin; var toggleBool = false; private var toggleRect = Rect(10, 10, 150, 100);

function OnGUI() { toggleBool = GUI.Toggle(toggleRect , toggleBool, "Hi!", guiSkin.button);
}

And if you want your buttons to change colour when active, you'll need to use a custom GUISkin. In it, customize the "Normal", "Hover" & "Active" button states to show your inactive colour, and customize the "On Normal", "On Hover", and "On Active" button states to show your active colour.

You can only assign textures as far as I can tell, so the only way to assign a colour is by creating a texture of the desired colour, and using that.

Hope this is enough to get your toggle working as you want it!

Comment
Add comment · Show 4 · 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 chchrist · Mar 09, 2010 at 09:26 PM 0
Share

Thanks a lot. Works great!!!

avatar image Cyclops · Mar 19, 2010 at 02:30 AM 0
Share

@Duck, yes, it's a good answer :) But I have to say, so far the GUI system is bugging me. There's no way to set a Button.enable property, for instance. So if I want to have exactly one button of three active, I need a Boolean array and change it based on which button is clicked. Ah well...

avatar image HeywoodFloyd · Jun 14, 2010 at 08:52 PM 0
Share

I'm doing this for multiple toggle buttons, and the last one retains the OnHover texture, whether it's supposed to be on or off and no matter where the cursor is.

avatar image moreyes · Jul 30, 2013 at 01:44 PM 0
Share

The C# example gives a NullReferenceException. Just telling because you asked in your profile. ;)

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

Move the text in a GUI Button/Box 1 Answer

button pressed twice / Conversation text skip 5 Answers

How to create a gui button inside an if statement? 1 Answer

How can you do a custom GUIStyle image position? 1 Answer

Change button texture when its clicked 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