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 vexe · Jun 24, 2013 at 11:40 AM · guimousecursordropdownguiutility

Help in GUI issue - Can't lose focus of a dropdown list when mouse clicks somewhere else?

I'm working on an inventory system. When the player right-clicks an item, it displays some dropdown list of options, "Use", "Equip", etc. Currently the inventory is a grid of buttons. When I right click on a button, the dropdown list appears and I start to use labels instead of actual buttons so that it appears as if the buttons are disabled. Now the issue is that, I want the dropdown to disappear when the player clicks somewhere else other than the list's buttons. Here's the dropdown list code - they're just a bunch of buttons wrapped with a box:

 public int Draw()
 {
     if (entries == null)
     {
         Debug.Log("Something wrong with entries...");
         return -1;
     }
  
     float totalHeight = GetTotalEntriesHeight();
     float x_offset = spaceBetweenButtons * 4;
     float y_offset = spaceBetweenButtons * 4;
  
     GUI.Box(new Rect(position.x, position.y, entryDimensions.x + x_offset, totalHeight + y_offset), "", defaultMenuBackgoundStyle);
  
     int entryIndex = -1;
     for (int i = 0; i < entries.Count; i++)
     {
         // offsetting x by spaceBetweenButtons and y by the height of the entry * i + the space between buttons
         bool entryButtonPressed = GUI.Button(new Rect(position.x + x_offset / 2, position.y + i * entryDimensions.y + y_offset / 2, entryDimensions.x, entryDimensions.y), entries[i], defaultEntryStyle);
  
         if (entryButtonPressed)
         {
             entryIndex = i;
             shouldBeShown = false;
             break;
         }
     }
     return entryIndex;
 }

I tried something like this:

 //...
 if(Input.GetMouseButtonDown(0) // and even GetMouseButton
 {
         shouldBeShown = false;
     if (entryButtonPressed)
     {
         entryIndex = i;    
         break;
     }
 //...


Now this does hide the list when the player clicks on somewhere else, but for some reason the entryButtonPressed is always false and I get some strange behavior when the player clicks on a button that's directly above the item. So this is definitely not working, also tried repositioning this check in more than one place, all showed the same issue...

I recently knew there's something called a GUIUtility but I don't know how to use it, that is, if I'm supposed to use it. Any help would be appreciated, thanks 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
1
Best Answer

Answer by vexe · Jun 25, 2013 at 01:49 AM

OK I solved the issue my self - just had to check if the mouse is within the list box area, if not, check for left/right clicks, if there was any, just hide the list, easy enough!

 // draw stuff...
 if (!IsMouseContainedWithinBoxArea())
 {
     if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
     {
         shouldBeShown = false; 
     }
 }
 // ...    

 private bool IsMouseContainedWithinBoxArea()
 {
     Vector2 mousePosition_InGUI = GUIUtility.ScreenToGUIPoint(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y));
     bool contained_horizontally = mousePosition_InGUI.x > boxRect.x && mousePosition_InGUI.x < boxRect.x + boxRect.width;
     bool contained_verticaally  = mousePosition_InGUI.y > boxRect.y && mousePosition_InGUI.y < boxRect.y + boxRect.height;
     return contained_horizontally && contained_verticaally;
 }
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

15 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

Related Questions

custom mouse changing when hovering over objects 1 Answer

Use OnMouseEnter/OnMouse Exit with center of screen 1 Answer

Locking cursor without changing position 1 Answer

Setting the mouse position to specific coordinates 5 Answers

"Click" a GUI Button using only joystick input 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