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 castor · Sep 20, 2012 at 07:49 AM · guiraycasting

How to use GUIUtility.hotControl

I'm trying to prevent mouse clicks from going over the UI and detecting what is behind (I guess technically I want to disable raycasting when the mouse is over the GUI).

I looked at this answer and tried to use the GUIUtility.hotControl but I cant figure out how it works....

My GUI is a small box that pops right in front of the selected object (kind of old point and click games). Here is my current UI Code

 function OnGUI () {
 
     openOrClosed = playerNavigation.objContainerProperties.containerIsOpen;        
     GUI.skin =  customSkin;
     GUI.depth = 0;
 
 
 //        GUI FOR WHEN THE PLAYER CLICKS ON A OBJECT        //
     if (playerNavigation.leftClickOnContainer == true && showGUIContainer == true){
         // Converting the selectedObject location to ScreenPoint
         var objScreenPoint = camera.main.WorldToScreenPoint(playerNavigation.selectedContainer.position);
         Debug.Log("objScreenPoint is: " + objScreenPoint);    
         // defining the values for X and Y for the UI using the selected Object ScreenPoint (so that the UI spawns where the object is)
         var mouseX = objScreenPoint.x;
         var mouseY = Screen.height - objScreenPoint.y;
 
         
         // Defines the Group that contains the GUI
         GUI.BeginGroup (Rect (mouseX - 90, mouseY -40, 180,160), "");
             // Frame around the gui with the object name.
             GUI.Box (Rect (0, 0, 180, 160), playerNavigation.objContainerProperties.objName);
             
             // LookAt
             if (GUI.Button (Rect (0, 60, 180, 32), "LOOK AT")) {
                 objInteractions.LookAtContainer ();
                 showGUIContainer = false;
             }
             // Open/Close
             if (openOrClosed == false){
                 if(GUI.Button (Rect (0, 90, 180, 32), "OPEN")) {        
                     objInteractions.OpenContainer ();
                     showGUIContainer = false;
                 }
             }
             else if (openOrClosed == true){
                 if(GUI.Button (Rect (0, 90, 180, 32), "CLOSE")) {        
                     objInteractions.CloseContainer ();
                     showGUIContainer = false;
                 }
             }
             // Look Inside
             if (GUI.Button (Rect (0, 120, 180, 32), "LOOK INSIDE")) {
                 //Application.LoadLevel (2);
             }
         GUI.EndGroup ();
     }

Any help would be really appreciated! I've been going round in circles trying to figure this out....

Comment
Add comment · Show 3
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 Khada · Sep 20, 2012 at 01:35 PM 0
Share

Personally, I would just put your GUI on a "GUI" layer and use a layer mask when casting your ray.

avatar image castor · Sep 20, 2012 at 02:59 PM 0
Share

Isnt GUI Layer only used with the old GUI Textures and GUI Texts? From the Unity wiki they say that "GUI Layers do not affect UnityGUI in any way."

avatar image Khada · Sep 20, 2012 at 05:18 PM 0
Share

It just means that it's independent of the GUI system. You can put GUI on any layer and then use that layer when ray casting.

2 Replies

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

Answer by Shrandis · Sep 20, 2012 at 01:34 PM

From unity script reference, GUIUtility.hotControl:

once the user mouseup's, the control sets hotControl to 0

This means that when mouse clicks a button, hotControl is not equal to 0. You simply need to check if hotControl is equal to 0 or not before your raycasting/mouse input code.

Simple example:

 void Update()
 {
 if (GUIUtility.hotControl == 0)
     UpdateMouseInput();
 }


However, if I recall correctly, hotControl is set to the controlID of the button only on a mousedown. Mouse over does not trigger it.

If you want to disable your raycasting when mouse cursor is over a button, you need to detect the mouseover yourself, which can be done using Rect's non-static method "Contains".

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 castor · Sep 20, 2012 at 05:00 PM 0
Share

It worked like a charm!!! Thank you so much. I ended up only having to use the Rect.Contains.

avatar image castor · Sep 20, 2012 at 05:02 PM 0
Share

Actually just to add for future users, it took me a while to get it to work since the Rect function inside 'OnGUI' calculates the rectangle from the top left while if we do it outside it will calculate from the bottom left!

avatar image M_Plot · Jul 09, 2013 at 02:10 PM 0
Share

Thank you!

avatar image Salazar · Jul 12, 2014 at 12:27 PM 0
Share

Thanks, It worked like a charm.

avatar image
0

Answer by hieudev · Aug 25, 2014 at 04:44 AM

if that do not works for you, try restricting Input.GetMouseButtonDown.position

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

12 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

Related Questions

Raycasting and GUI 0 Answers

Show GUI only when looking at an object? 1 Answer

Return BoxCollider Rect values in screen coordinates 0 Answers

GUI Button vs RayCasting as input for random animation. 0 Answers

How to make a Cube act as a button to increment a number on a screen overlay panel in an FPS? 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