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 daviddickball · Jul 17, 2014 at 05:59 PM · guitextureguistyleguibutton

What's the easiest way to make a texture button with a hover state?

The UI elements in Unity are doing my head in. I want a 2D texture button to change to a different texture when hovered over with the mouse but this seems complicated.

The positioning using rect is way more confusing than positioning a GUITexture object, and swapping texture seems to require a GUIstyle. Is this the best way?

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

3 Replies

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

Answer by ShadyProductions · Jul 21, 2014 at 07:17 PM

Try something like

 var normalGUI : Texture2D; // change this on inspector
 var hoverGUI : Texture2D; //change this on inspector
 
 function OnMouseEnter() {
 guiTexture.texture = hoverGUI;
 
 }
 
 function OnMouseExit() {
 guiTexture.texture = normalGUI;
 
 }
 
 function OnMouseDown() {
 //do something when clicked on the button
 }

Although, GUI's can't be assigned to objects I think, they are only on screen.

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 daviddickball · Jul 24, 2014 at 01:45 PM 0
Share

Thanks! Not the most ideal solution as it means every GUI element has to have its own script. I'll have to live with that though!

avatar image
0

Answer by Tony_T · Jul 21, 2014 at 04:35 PM

I'm atually using a quad with the texture of a button. Then i just use OnMouseEnter() and OnMouseExit() to change the material. Not sure if that's what you want.

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 daviddickball · Jul 21, 2014 at 06:15 PM 1
Share

I know what you mean, I've been using that method so far. But the issue is that the image looks stretched because it's not pixel-perfect, which is why I wanted to use the GUI texture.

avatar image
0

Answer by robertbu · Jul 22, 2014 at 02:39 AM

Here is a solution that stumbled on. I'm giving it to you with some caviots. First I could not find documentation for it anywhere. I use EZGUI for my interface work, so I don't have much depth with GUI. But in looking at the reference and many UA questions, I did not see it used. So there may be issues, and/or it may work today and not work in some future build. Second, it appears to make a permanent change to the project. Run once, you can disable the script and the hover still works. I don't know what would happen if you switched platforms, or if doing a build will pull the texture in without a reference.

 #pragma strict
 
 var hoverTexture : Texture;
 private var init = false;
 function OnGUI() {
 
     if (!init) {
         GUI.skin.button.hover.background = hoverTexture;
         init = true;
     }
 }

Put this on one game object and it changes the hover texture everywhere. If you want to return to the default texture you, set the texture to null;

 GUI.skin.button.hover.background = hoverTexture;
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

22 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

Related Questions

guitexture to act as a button on the keyboard or mouse 0 Answers

How-to-make-GUI-Buttons-on-GUI-Texture-resolution-independent 0 Answers

GUI Button text color not changing on hover 2 Answers

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

Transferring Color from script on another GameObject to a GUI.Box 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