Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 NeMewSys · Aug 20, 2012 at 01:01 AM · guiobjectmouseclickoutside

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!

Comment
Add comment · Show 2
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 NeMewSys · Aug 20, 2012 at 09:20 AM 0
Share

Still struggling with this, it was cool to have an OnClickOutside() method.

avatar image Grug16 · Aug 20, 2012 at 09:48 AM 0
Share

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

4 Replies

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

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
 }
Comment
Add comment · Show 5 · 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 NeMewSys · Aug 20, 2012 at 11:51 AM 0
Share

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...

avatar image NeMewSys · Aug 20, 2012 at 11:58 AM 0
Share

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).

avatar image NeMewSys · Aug 20, 2012 at 12:01 PM 0
Share

Damn it doesn't work, the On$$anonymous$$ouseEnter and On$$anonymous$$ouseExit are being applied to the GameObject and not the GUI itself....

avatar image Bunny83 · Aug 20, 2012 at 12:22 PM 0
Share

Uhmm, this example is written in C#...I've put a comment above that says C# ;) I can add the same example in UnityScript...

avatar image Bunny83 · Aug 20, 2012 at 12:28 PM 0
Share

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 / ...)

avatar image
2

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);

http://docs.unity3d.com/es/current/ScriptReference/RectTransformUtility.RectangleContainsScreenPoint.html

Comment
Add comment · Show 2 · 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 $$anonymous$$ · Jul 20, 2015 at 05:26 AM 0
Share

WOW Incredible. After 3 hours searching about this. Finally found this. Thank you so much!

avatar image Daviiid · Sep 18, 2021 at 04:47 PM 0
Share

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); } }

avatar image
0

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

Comment
Add comment · Show 2 · 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 Bunny83 · Aug 20, 2012 at 12:30 PM 0
Share

Can you post the relevant part of your GUI? What "area" do you have in your GUI?

avatar image NeMewSys · Aug 20, 2012 at 01:19 PM 0
Share

$$anonymous$$y GUI's code is this: http://pastebin.com/H1AV3iW4 The relevant methods for this effect are Update and $$anonymous$$ouseOver.

avatar image
0

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;
 }

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

13 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

Related Questions

GUI on click 2 Answers

How to make object clickable when user enter collider ? 0 Answers

Detecting a GUI element under the mouse? 1 Answer

Clicks from a track pad works, but not from a mouse 0 Answers

Disable Selection on Left button Mouse Click 3 Answers


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