Author Topic: How to detect if mouse is clicking on NGUI area?  (Read 54018 times)

jjobby

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
How to detect if mouse is clicking on NGUI area?
« on: April 22, 2012, 04:42:50 AM »
Hi, I have a scene that player can rotate camera by holding left click and moving the mouse. The script which is used to rotate camera has its own input detection. But I don't want camera to rotate if the mouse is on NGUI area. How can I check if the mouse is clicking on NGUI area?

loopyllama

  • Guest
Re: How to detect if mouse is clicking on NGUI area?
« Reply #1 on: April 22, 2012, 02:02:16 PM »
You are processing your mouse movements in a script. NGUI processes mouse movements too. You want to know in your mouse movement code if NGUI has processed the mouse movement. Use Unity script evaluation order to ensure your code gets processed later than Update meaning your code will be evaluated after NGUI. Now you only need to attach a script to your NGUI widget, object, background, whatever, that sets a public static bool to true in an NGUI Event like OnPress. So your attached script would have an event listener for OnPress in Awake, and a method that sets a public static bool to true if the bool passed to you from OnPress is true, or you could use OnHover or OnDrag or whatever best suits your needs. Then in your mouse movement code you can check the value of the public static bool and respond accordingly. Don't forget to reset the bool back to false!
The NGUI code is adequately documented. Check out UICamera for events you can listen for.
« Last Edit: April 22, 2012, 11:25:59 PM by loopyllama »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 297
  • -Receive: 1136
  • Posts: 21,937
  • Toronto, Canada
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #2 on: April 22, 2012, 03:18:20 PM »
I'd just like to add that changing your own input detection to use the NGUI's event system will be far, far easier. :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 297
  • -Receive: 1136
  • Posts: 21,937
  • Toronto, Canada
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #3 on: April 22, 2012, 03:20:28 PM »
Btw, what I did in Windward is simply attach a UICamera script to the main game camera, and set its event mask to "nothing". I then set the UICamera.fallThrough to my "game event listener" script, and all the events that don't get handled by the UI simply go to that script.

milali

  • Guest
Re: How to detect if mouse is clicking on NGUI area?
« Reply #4 on: April 23, 2012, 06:27:12 AM »
I'd just like to add that changing your own input detection to use the NGUI's event system will be far, far easier. :)

so your saying make a panel over the main screen play and when you touch it. move the camera as you want to, rather than handle the on touch events manually?

loopyllama

  • Guest
Re: How to detect if mouse is clicking on NGUI area?
« Reply #5 on: April 23, 2012, 07:29:49 AM »
I think he is saying do what he did in windward. otherwise you'd have to go the route I mentioned and make your touches and NGUIs touches talk to each other, which would make for code that is not pretty.  ;)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 297
  • -Receive: 1136
  • Posts: 21,937
  • Toronto, Canada
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #6 on: April 23, 2012, 01:24:27 PM »
What I did in Windward is the ideal approach for handling events that fall through the UI onto your game.

milali

  • Guest
Re: How to detect if mouse is clicking on NGUI area?
« Reply #7 on: April 24, 2012, 04:59:25 AM »
Btw, what I did in Windward is simply attach a UICamera script to the main game camera, and set its event mask to "nothing". I then set the UICamera.fallThrough to my "game event listener" script, and all the events that don't get handled by the UI simply go to that script.

sorry for the numpty question, what do you mean by game event listener script?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 297
  • -Receive: 1136
  • Posts: 21,937
  • Toronto, Canada
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #8 on: April 24, 2012, 02:07:27 PM »
Just a script you write attached to a game object you designate as UICamera.fallThrough. Implement functions inside: OnClick, OnPress, etc -- and these will only be called if the UI doesn't handle them.

milali

  • Guest
Re: How to detect if mouse is clicking on NGUI area?
« Reply #9 on: April 25, 2012, 04:37:03 AM »
nice! will do!

milali

  • Guest
Re: How to detect if mouse is clicking on NGUI area?
« Reply #10 on: April 26, 2012, 04:15:02 AM »
one question:

how do you handle multiple touches (for zoom and rotation) at the same time with ngui interface for on mousedown etc

TericDragon

  • Guest
Re: How to detect if mouse is clicking on NGUI area?
« Reply #11 on: April 26, 2012, 11:32:43 AM »
We ran into this problem ourselves with our game project.  Since the input controller for our game was already well established by the time we decided to investigate NGUI, it was not practical to replace our current working input system with NGUI input handling.

In order to solve the problem of input fall-through, I made sure that all NGUI elements are on the UI layer, and I implemented the following in our game's main input controller:

  1. // This grabs the camera attached to the NGUI UI_Root object.
  2. Camera nguiCam = GameController.Instance.GameUI.NGUI_Manager.GetComponentInChildren<Camera>();
  3.  
  4. if( nguiCam != null )
  5. {
  6.         // pos is the Vector3 representing the screen position of the input
  7.         Ray inputRay = nguiCam.ScreenPointToRay( pos );    
  8.         RaycastHit hit;
  9.  
  10.         if( Physics.Raycast( inputRay.origin, inputRay.direction, out hit, Mathf.Infinity, LayerMask.NameToLayer( "UI" ) ) )
  11.         {
  12.                 // UI was hit, so don't allow this input to fall through to the gameplay input handler
  13.         }
  14. }
  15.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 297
  • -Receive: 1136
  • Posts: 21,937
  • Toronto, Canada
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #12 on: April 26, 2012, 03:18:59 PM »
one question:

how do you handle multiple touches (for zoom and rotation) at the same time with ngui interface for on mousedown etc
UICamera.lastTouchID tells you the ID of the touch / mouse / controller event that triggered OnClick, OnPress, etc function that you're in.

milali

  • Guest
Re: How to detect if mouse is clicking on NGUI area?
« Reply #13 on: April 28, 2012, 10:14:15 PM »
Thanks for the replies, I will put more time into this

theHost

  • Guest
Re: How to detect if mouse is clicking on NGUI area?
« Reply #14 on: May 12, 2012, 06:52:41 PM »
Sorry to resurrect this old thread, but I thought it be best to post a followup question here than a new thread.

I am trying out Aren's suggestion of attaching my input handler object by setting UICamera.fallThrough to it. There are at least 2 issues I am coming across.
The OnKey only starts firing once I press the mouse on to the screen after hitting play (in the editor). It's almost like it's not active until the mouse activates the game screen. Normally Unity will take key input as soon as you hit play as long as the editor window is active.
I only seem to get OnPress (and other events) once when I initially do the press down. Is there a way to get continuos callbacks when the touch is down? Maybe I am missing something here?