• 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
Question by Firedan1176 · Jun 07, 2014 at 07:02 PM · guiguitexturecontainsgui texturegui.drawtexture

Best way to handle clicking on a GUI.DrawTexture?

As simple as the title says - I'm trying to make a clickable button with GUI.DrawTexture, and it works perfectly fine. But OnGUI is called at least twice, so when I try the following:

 void OnGUI() {
 
 Rect someRect3 = new Rect (xpos, ypos, xsize, ysize);
                                 if (someRect3.Contains (Event.current.mousePosition))
                                         GUI.DrawTexture (xpos, ypos, xsize, ysize), buttonTexture6);
                                                 else
                                                         GUI.DrawTexture (xpos, ypos, xsize, ysize), buttonTexture5);


Basically, it just has an image that lights up (highlighted) when the user has their mouse over it. That works. Only thing is, when I try if (Input.GetMouseButtonDown(0)) , it will be called twice. Other than that, it works perfectly. So if it's called twice, will that make a big difference if it opens up a "gui menu"? Or is there an easier way to check if I click on that GUITexture?

Comment

People who like this

0 Show 0
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
Best Answer Wiki

Answer by Krishna Chittur · Jul 02, 2014 at 11:23 AM

I get that this answer is like a month late, but here goes:

As far as I know, there's no way to directly check whether someone is clicking on the GUI.DrawTexture. You can, as you've done, just check if they're in the rectangle. I would change GUI.DrawTexture((xpos, ypos, xsize, ysize), buttonTexture5) to simply GUI.DrawTexture(someRect3, currentTex); in the OnGUI() function. Then declare a new texture called currentTex as a Texture2D, and when they hover or whatever, just change the value of currentTex in the Update function. No need to do all the checking work in the OnGUI() function, it'll just set the texture to the latest value of currentTex.

Alternatively, you could just use the GUITexture directly instead of GUI.DrawTexture, but then you wouldn't be able to scale the size of the texture to the screen size, a problem if you're targeting multiple platforms. If that's the case, you can use the OnMouseEnter() and OnMouseExit() functions easily.

Another option is to just use GUI.Button(someRect,currentTex). You can check it out at the scripting API for it. The hovering and pressing is all automatic, so it saves a lot of headache, and it's scalable if you want.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Load a sprite as a texture from. 1 Answer

Change Gui Textre to another overtime? and reset script when player passes through trigger 1 Answer

GUI.DrawTexture() into a Layer? 1 Answer

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

Activate GuiTexture When Clicking on BoxCollider c# 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