- Home /
Detect a click outside a GUI/object
Hello!
Is it possible to detect when the user clicks with the mouse outside a GUI's area? I wanted to call my GUI's close method when that happens but I have no idea how to detect such event in the correct way.
Thanks!
Still struggling with this, it was cool to have an OnClickOutside() method.
Not sure if this is what you want, but some initial investigation from me came up with Gui.changed http://docs.unity3d.com/Documentation/Components/gui-Controls.html at the bottom. may help
Answer by Bunny83 · Aug 20, 2012 at 10:25 AM
Well there's no built in way to detect this. You have to check it manually. When you use a GUI window or group / area, you can use its Rect to check if the mouse is inside.
// C#
Rect windowPos = new Rect(10,10,200,150);
void OnGUI()
{
Event e = Event.current;
windowPos = GUI.Window(0, windowPos, drawWindow, "MyWindow");
if (e.type == EventType.MouseDown && !windowPos.Contains(e.mousePosition))
{
// Click was outside of the GUI window
}
}
void drawWindow(int aID)
{
// draw the window content
}
Here's the same thing in UnityScript.
// UnityScript
var windowPos = new Rect(10,10,200,150);
function OnGUI()
{
var e = Event.current;
windowPos = GUI.Window(0, windowPos, drawWindow, "MyWindow");
if (e.type == EventType.MouseDown && !windowPos.Contains(e.mousePosition))
{
// Click was outside of the GUI window
}
}
function drawWindow(ID : int)
{
// draw the window content
}
i'm getting a strange error here: Event e = Event.current;
the error says Assets/$$anonymous$$yCustomScripts/GUI/Window.js(18,22): UCE0001: ';' expected. Insert a semicolon at the end.
But there is a ; there... what the...
This is the most efficient way I found of doing this: public function Update() {
if(Input.Get$$anonymous$$ouseButtonDown(0) && !mouseOver) {
Close();
}
}
public function On$$anonymous$$ouseEnter() {
mouseOver = true;
}
public function On$$anonymous$$ouseExit() {
mouseOver = false;
}
actually I don't know what's the difference between Event e = Event.current; and Input.Get$$anonymous$$ouseButtonDown(0).
Damn it doesn't work, the On$$anonymous$$ouseEnter and On$$anonymous$$ouseExit are being applied to the GameObject and not the GUI itself....
Uhmm, this example is written in C#...I've put a comment above that says C# ;) I can add the same example in UnityScript...
OnGUI is a very special callback. The Event class is tightly connected to OnGUI. All GUI elements are using the current Event state to draw themself or to react to all kind of events (keyboard / mouse / ...)
Answer by anisabboud · May 08, 2015 at 12:37 AM
I wrote a function:
private void HideIfClickedOutside(GameObject panel) {
if (Input.GetMouseButton(0) && panel.activeSelf &&
!RectTransformUtility.RectangleContainsScreenPoint(
panel.GetComponent<RectTransform>(),
Input.mousePosition,
Camera.main)) {
panel.SetActive(false);
}
}
which I call in Update()
and it hides the panel when the user clicks outside it:
HideIfClickedOutside(HelpPanel);
WOW Incredible. After 3 hours searching about this. Finally found this. Thank you so much!
This works great after you delete , Camera.main from the if statement. So: private void HideIfClickedOutside(GameObject panel) { if (Input.GetMouseButton(0) && panel.activeSelf && !RectTransformUtility.RectangleContainsScreenPoint( panel.GetComponent<RectTransform>(), Input.mousePosition)) { panel.SetActive(false); } }
Answer by NeMewSys · Aug 20, 2012 at 12:14 PM
This is the closest as it gets so far:
> }public function Update() {
if(Input.GetMouseButtonDown(0) && !MouseOver()) {
Close();
public function MouseOver() { var x = Input.mousePosition.x; var y = Screen.height - Input.mousePosition.y; if(x >= SizeAndPos[0] && x <= (SizeAndPos[0] + SizeAndPos[2]) && y >= SizeAndPos[1] && y <= (SizeAndPos[1] + SizeAndPos[3])) return true; return false; } I get the mouse coordinates X and Y, and check if they are in the GUI's boundaries, I guess this is the most efficient as it gets in GUIs. I wasn't able to use}
windowPos.Contains(e.mousePosition)
because of that error i commented above.
Hope this helps someone with the same question in the future.
Thanks
Can you post the relevant part of your GUI? What "area" do you have in your GUI?
$$anonymous$$y GUI's code is this: http://pastebin.com/H1AV3iW4 The relevant methods for this effect are Update and $$anonymous$$ouseOver.
Answer by tster123 · Jul 20, 2015 at 04:53 AM
The other answers didn't seem to work for me. Not sure if it's because I'm using Unity5 or because I did something wrong or what.
Anyways, this seems to work for me, although I want to add a caveat that I am a total Unity noob, so this might not work for all cases.
private void OnGUI()
{
HideIfClickedOutside(TheGuiGameObjectToCheck, Event.current);
}
private bool HideIfClickedOutside(GameObject panel, Event e)
{
// do easy checks first.
if (e.type == EventType.MouseDown && panel.activeSelf)
{
// get the transform of the GUI item you are checking
RectTransform tform = panel.GetComponent<RectTransform>();
//use the transform position and size to construct a Rect.
// the position is the center of the GUI element, so move it back to the corner
Vector2 location = new Vector2(
tform.position.x - tform.rect.size.x / 2,
tform.position.y - tform.rect.size.y / 2);
Rect toCheck = new Rect(location, tform.rect.size);
// invert the y coordinate of the mouse to conform to the GUI coordinates
if (!toCheck.Contains(new Vector2(e.mousePosition.x, Screen.height - e.mousePosition.y)))
{
panel.SetActive(false);
return true;
}
}
return false;
}