- Home /
GUI Mouse lock improvements
I've been researching how to prevent key clicks to world clicks when the user intends to click a GUI. My movement works on mouse move, and mouse release, so usual methods don't seem to work appropriately. Using the GUIUtility.hotControl works by default when you click an interactive element like an input box, or button control. This doesn't seem to work for non-interactive elements that you wish to intercept. I've gotten around this a little bit with
if (window.Contains(Input.mousePosition)) {
Debug.Log("<System>: <color=white>Mouse over Debugger</color>");
GUIUtility.hotControl = 1;
}
This forces hotControl in scenarios I wish to manage. That being said, I have some complication when window is a handle for a GUI.Window interface. From various debugging, I've determined that window contains the mouse in a virtual rect aligning to the top-left.
Question here, is how do I need to modify my code snippet to adjust for window positions?
[EDIT] window.Contains() does not register x and y rect positions. Modifying the mouse positions via rect X/Y doesn't seem to be accurate enough. Few pixels lower than the window. Still looking for solutions.
if (Adjust.FitToScreen(window).Contains(new Vector3(Input.mousePosition.x+Adjust.FitToScreen(window).x, Input.mousePosition.y+Adjust.FitToScreen(window).y, Input.mousePosition.z))) {
Debug.Log("<System>: <color=white>$$anonymous$$ouse over Debugger</color>");
//Debug.Log("<System>: <color=yellow>Window: "+window.x+","+window.y+":"+window.x$$anonymous$$in+","+window.y$$anonymous$$in+"</color>");
GUIUtility.hotControl = windowID;
} else {
if (GUIUtility.hotControl == windowID)
GUIUtility.hotControl = 0; // release
}
Is better, but window contains is registerring window.x$$anonymous$$in left, and window.y$$anonymous$$in down from where it should be. If I don't adjust the position, window constains registers at 0, 0.
Answer by Komak57 · Jun 28, 2014 at 05:32 AM
First complication, the mouse Y coordinate needs to be inverted by the screen height. Next up, was having trouble with input controls using hotControl, so I wrote a little flag script to maintain multiple GUI's.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MouseControl : MonoBehaviour {
List<Window> list = new List<Window>();
public bool Locked {
get {
bool locked = false;
foreach(Window w in list) {
if (w.Locked)
locked = true;
}
return locked;
}
}
public void Lock(int WindowID) {
Window w = new Window(WindowID, true);
int i = -1;
if ((i = Contains(w)) != -1) {
if (!list[i].Locked)
Debug.Log("<System>: <color=white>Window["+WindowID+"] Locked</color>");
list[i].Locked = true;
} else {
list.Add(w);
Debug.Log("<System>: <color=white>Window["+WindowID+"] Locked</color>");
}
}
public void Unlock(int WindowID) {
Window w = new Window(WindowID, false);
int i = -1;
if ((i = Contains(w)) != -1) {
if (list[i].Locked)
Debug.Log("<System>: <color=white>Window["+WindowID+"] Unlocked</color>");
list[i].Locked = false;
} else {
list.Add(w);
Debug.Log("<System>: <color=white>Window["+WindowID+"] Unlocked</color>");
}
//Debug.Log("<System>: <color=white>Window["+WindowID+"] Unlocked</color>");
}
public int Contains(Window a) {
for(int i = 0; i < list.Count; i++) {
if (a.ID == list[i].ID)
return i;
}
return -1;
}
}
public class Window {
public int ID;
public bool Locked;
public Window(int ID, bool Locked) {
this.ID = ID;
this.Locked = Locked;
}
}
Usage:
if (Adjust.FitToScreen(window).Contains(new Vector3(Input.mousePosition.x, Screen.height-Input.mousePosition.y, 0))) {
//Debug.Log("<System>: <color=white>Mouse over Debugger</color>");
//Debug.Log("<System>: <color=yellow>Window: "+window.x+","+window.y+":"+window.xMin+","+window.yMin+"</color>");
GameObject.Find("System").GetComponent<MouseControl>().Lock(windowID);
} else {
GameObject.Find("System").GetComponent<MouseControl>().Unlock(windowID);
}
My Adjust.FitToScreen is a small formula to scale GUI elements based on a preferred size. That just lets me program everything for one resolution and scale for others.
Your answer
![](https://koobas.hobune.stream/wayback/20220613152614im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
If mouse is hovering over any GUI element 0 Answers
detect mouseover with grid buttons? 1 Answer
How do i create an object with a GUI Button 1 Answer
Ensuring Correct Call Order 0 Answers
Change GUI on mouse over 1 Answer