• 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 pazzesco · May 15, 2013 at 08:13 PM · guiinputmousehud

Is there a built-in function to determine if any GUI item has been clicked?

What I need to figure out if there is a built-in method to determine if any GUI item has been clicked. For instance, I have a button in the HUD that you can click to open a status menu, but I'd like to be able to restrain other areas from acting on this click, such as the character firing their weapon, by making a call to the supposed built-in function to check for this click. Now, I know I could throw some sort of method within the block of code that handles button clicks to flag some boolean, but I would like to lean towards built-in methods if at all possible. So, to iterate, is there a built-in function to determine if any GUI item has been clicked?

Comment
Nercoe

People who like this

1 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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Aeron0 · May 15, 2013 at 09:59 PM

enter code hereDid you look at the Event class in Unity?

var button : int Description: Which mouse button was pressed.

0 means left mouse button, 1 means right mouse button, 2 means middle mouse button. Used in EventType.MouseDown, EventType.MouseUp events.

You could maybe use this with

 Rect customRect;
 // Custom rectangle for the GUI area that you want clickable
 void Start () {
     customRect.x = 200;
     customRect.y = 200;
     customRect.width = 200;
     customRect.height = 200;
 
 }
 
 
 
 void OnGUI()
 {
    GUI.Box(customRect,"Box");

             // Check if mouse coordinates are in the rectangle
     if (customRect.Contains(Event.current.mousePosition))
     {
         Event e = Event.current;

                     // Mouse is in the rectangle so check for clicks
         if (e.button == 0 && e.isMouse){ Debug.Log("Left Click"); }
         else if (e.button == 1) { Debug.Log("Right Click");}
         else if (e.button == 2){ Debug.Log("Middle Click"); }
         else if (e.button > 2){ Debug.Log("Another button in the mouse clicked"); }
     }
 
 }
 
 

}

Comment
pazzesco

People who like this

1 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 pazzesco · May 21, 2013 at 03:58 AM 0
Share

Not exactly what I was hoping for (seems a little bulky), but it is indeed a built-in to determine if a GUI item has been clicked. Thank you.

avatar image

Answer by $$anonymous$$ · May 15, 2013 at 09:10 PM

You might be able to whip up something assigning GUI.tooltip (you dont have to actually display the tooltip) to the button in question and checking if its null where you fire a weapon, but there must be a better way to do this

Comment
pazzesco

People who like this

1 Show 2 · 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 pazzesco · May 15, 2013 at 09:17 PM 0
Share

What I'm looking for is a built-in function to check for this, if it exists. The code would look something like...

 //Here, GUI.clicked is a hypothetical property that determines if a GUI element was clicked or not. I need something of this nature, if it's out there.
 if (Input.GetButtonDown("Fire1") && !GUI.clicked) {
     //code for firing weapon
 }


Ultimately, I'm looking to test for element clicks at the code that handles gunfire without having to write additional within each button or element that turns on a flag saying that the GUI has been clicked.

avatar image $$anonymous$$ · May 16, 2013 at 08:12 PM 0
Share
 GUI.Button(new Rect(10, 10, 100, 20), new GUIContent("Button", "Test"));
 
 //Here, GUI.clicked is a hypothetical property that determines if a GUI element was clicked or not. I need something of this nature, if it's out there.
 if (Input.GetButtonDown("Fire1") && GUI.tooltip != "Test") // for some reason GUI.tooltip == null always returns false for me, so I check it against a string
 {
     //code for firing weapon
 }

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

How to get mouse position with ray from camera??? 1 Answer

How to disable mouse click detection in game when clicking exit menu button? 1 Answer

Look Stick for FPS Games on Mobile Platforms 1 Answer

GUI text Main Menu 1 Answer

EventType.MouseDrag not working in WebPlayer 0 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