- Home /
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....
Personally, I would just put your GUI on a "GUI" layer and use a layer mask when casting your ray.
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."
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.
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".
It worked like a charm!!! Thank you so much. I ended up only having to use the Rect.Contains.
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!
Answer by hieudev · Aug 25, 2014 at 04:44 AM
if that do not works for you, try restricting Input.GetMouseButtonDown.position
Your answer
![](https://koobas.hobune.stream/wayback/20220613081245im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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