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
7
Question by Lucas Meijer 1 · Oct 18, 2009 at 01:17 PM · guiinput3d

If I have a button over my 3d world, how can I detect if a mouseClick was in the world or not?

NOTE...

This QA is extremely out of date. Regarding this annoying disaster in Unity, for 2015...

http://answers.unity3d.com/questions/784617/how-do-i-block-touch-events-from-propagating-throu.html#answer-885898


My game is a 3d world. From time to time I want to display buttons ontop of the 3d world. How can I detect mosueclicks in the 3D world? If I use Input.GetMouseDown(), I also react on buttonclicks, which I don't want.

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 cregox · Feb 23, 2011 at 05:27 PM 0
Share

So you want a GUI-like interface which will be transparent to mouse clicks, like a chat box, right? Nowadays that's not difficult to do at all. :)

avatar image SisterKy · Jul 22, 2011 at 12:31 AM 0
Share

cross-reference
http://answers.unity3d.com/questions/16774/preventing-mouse-clicks-from-passing-through-gui-c.html
http://answers.unity3d.com/questions/16587/gui-click-through.html

5 Replies

· Add your reply
  • Sort: 
avatar image
13

Answer by AngryAnt · Oct 21, 2009 at 09:13 AM

GUI receives mouse events one frame before Input does - you can use this fact to detect whether or not a mouse event was consumed by GUI when you're checking for mouse events in Input.

if (!GUIDidConsumeMouseDown (0) && Input.GetMouseDown (0))
{
    Debug.Log ("Click in the 3D world");
}

And the pseudocode logic to detect GUI event consumption:

  • OnGUI:
  • GUIDidGetMouseDown (Event.button) = Event.current.Type == EventType.MouseDown;
  • // The rest of your GUI //
  • GUIDidConsumeMouseDown (Event.button) = Event.current.Type == EventType.Used ? GUIDidGetMouseDown (Event.button) : false;

MonoBehaviour.OnMouseDown and friends automatically handles this - if you only rely on this functionality, you do not need to track GUI event consumption.

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
avatar image
1

Answer by Ent · Jun 11, 2010 at 10:21 AM

Hi, been reading through some other postings and gathered some interesting things together that may help some other people reading this and needing a more complex solution for different reasons and using a window below the GUI.

// Boolean for when mouse is over window

var onMouseOverMenu : boolean = true;

// Creation point of window

var windowRect : Rect = Rect (20, 20, 200, 300);

// Rect of window. Can also be windowRect if the creation point is not needed anymore
// after the creation of the window

var windowRectBox : Rect;

function Update(){

//if you have multiple GUIs you could use something like: // //Dont forget to change onMouseOverMenu to a boolean Array and to refer to this script. // var isOnMouseOverMenu : boolean = false; // for (var oMOMPtr : int in onMouseOverMenu) // if(onMouseOverMenu[oMOMPtr]==true)isOnMouseOverMenu = true; // if(isOnMouseOverMenu){ // //... enter script here // }

if (onMouseOverMenu) { // ... enter script here }

if (!onMouseOverMenu) { // ... enter script here }

}

function OnGUI () { windowRectBox = GUI.Window (0, windowRect, WindowFunction, "My Window"); }

function WindowFunction (windowID : int) { // Draw any Controls inside the window here

// Sets a Rect to the size of this window. var curBox : Rect = Rect(0,0,windowRectBox.width,windowRectBox.height);

// Checks if the mouse is inside of this Rect. // Due to a Window setting the Event.current.mousePosition to the position refering //to this window, our Rect starts at 0,0 and not at the creation point of this window. // Using Input.mousePosition is also a problem due to the creation of a GUI Element being // refered to from a corner point of the screen (default top left), even if created with // a Rect and that a Rect or Input always starts from the bottom left corner. // So if using xyzRect.contains(Input.mousePosition){} and creating a window at xyzRect, // you will notice that that the xyzRect.contains is not the same position as the window // created in xyzRect, even thou these are the same Objects. For this to work, // you would have to create all GUI elements from the bottom left corner. if(curBox.Contains(Event.current.mousePosition)){ Debug.Log("Mouse in Menu at " + Event.current.mousePosition); onMouseOverMenu = true; } else if (onMouseOverMenu == true) { onMouseOverMenu = false; }

// ... enter GUI here

}

e.g. if you have an onScreenDisplay wanting to show the distance to an object inside this screen: // place in Update()

//Where the mouse clicks var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

if (onMouseOverMenu) { // ... enter script here // ... only cast on gameObjects behind this window (or Rect)

     //Do a raycast from camera in direction of ray,
     //with a distance 100.0 and return hit.
 if (Physics.Raycast (ray, hit, 100.0)) {
     //hit.distance is the distance to the first collider.
     distanceToGround = hit.distance;
     //This draws a line, that shows the ray, in the editor.
     Debug.DrawLine (ray.origin, hit.point);
 }   

}

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
avatar image
0

Answer by Veehmot · May 30, 2012 at 10:08 PM

Use GUIUtility.hotControl.

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 Emery-Monzerol · Nov 15, 2012 at 04:51 PM 0
Share

Worked like a charm for me!

I used something like: if (GUIUtility.hotControl == 0) { //click is not on gui }

avatar image Novack · Sep 13, 2013 at 11:50 PM 0
Share

This does not work with certain GUI controls (Ie: GUI.Box).

avatar image
0

Answer by gnoblin · Oct 20, 2009 at 05:33 PM

Use OnMouseDown() function in a script hanging on your GameObjects.

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
avatar image
0
Wiki

Answer by ben · Mar 15, 2011 at 12:04 PM

use events such as mouse position and click count inside GUI button

var gu : GUIText; var aa = 0 ; var aaa : String;

function OnGUI(){

var e : Event = Event.current; Debug.Log(e.mousePosition);

if(aa==1){

 GUI.Button(Rect(20,100,50,50),aaa);

}

if(GUI.Button(Rect(20,30,100,50),"Button")){

  if(e.mousePosition.x <= 120){

     var ee : int = e.clickCount;        
     var eee : String = ee.ToString();

         aaa = eee;      
         print(eee);
         aa =1;

         gu.guiText.text = eee;

  }

}

}

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Input not registering while TextField is selected 3 Answers

2D Arrow pointing at 3D Position 1 Answer

GUI.TextField - Android: Unfocusing with click instead of Ok-button causes critical error 0 Answers

Clicking Trigger 1 Answer

Paper, Rock, Scissor 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