Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: cdevl on June 20, 2013, 09:17:51 AM

Title: NGUI Button click vs. click on the underlying mesh
Post by: cdevl on June 20, 2013, 09:17:51 AM
(Latest NGUI version - 2.6.3). I have a HUD that contains NGUI buttons. Every time I click one of these buttons if there is a mesh (enemy ship ...) under that button and that mesh supposed to react to the mouse click in some way then that reaction gets triggered. Is there a way around that? Is there a way to tell NGUI/Unity to "consume" the click? Or what kind of logic I would need to implement to make sure that clicks intended for my NGUI HUD don't propagate to the underlying meshes?

This looks similar to (I use 2d GUI though):

http://www.tasharen.com/forum/index.php?topic=4713.0 (http://www.tasharen.com/forum/index.php?topic=4713.0)
Title: Re: NGUI Button click vs. click on the underlying mesh
Post by: MrTact on June 20, 2013, 12:24:24 PM
I believe that any colliders under the click will register the collision and report the click. If you have stacked UIs with overlapping colliders, what you need to do is turn off the colliders on the obscured elements temporarily --  you can do something like:

foreach (BoxCollider collider in GetComponentsInChildren<BoxCollider>())
{
    collider.enabled = false;
}
Title: Re: NGUI Button click vs. click on the underlying mesh
Post by: cdevl on June 20, 2013, 01:09:44 PM
No. I don't stack colliders under UI. It is just HUD is sitting on top of the (3d) playing field. Meshes move there (according to a separate game logic - nothing to do with UI/HUD). those meshes have colliders. Right now these colliders react to the click on UI. From my perspective - UI is the closest to the user so only the UI collider should react. It is got to be a relatively common issue.
Title: Re: NGUI Button click vs. click on the underlying mesh
Post by: ArenMook on June 20, 2013, 01:36:52 PM
You need to use NGUI events on both, and you won't run into this issue.

If you use Input or OnMouse* on your mesh, then you will run into issues. Change it to use NGUI's events -- such as OnClick / OnPress instead.
Title: Re: NGUI Button click vs. click on the underlying mesh
Post by: cdevl on June 20, 2013, 01:53:29 PM
Sorry if this is obvious but how do I make sure that NGUI events get propagated to my colliders attached to non-gui objects (seen by a different camera, on a different layer). Does it mean that I also have to attach UICamera to my main camera?
Title: Re: NGUI Button click vs. click on the underlying mesh
Post by: cdevl on June 20, 2013, 01:57:26 PM
Attached UICamera to my main camera. Change event to OnClick. It works. Only GUI gets the events.
Title: Re: NGUI Button click vs. click on the underlying mesh
Post by: cdevl on June 20, 2013, 02:17:59 PM
ArenMook - thank you for quick reply and solution.
Title: Re: NGUI Button click vs. click on the underlying mesh
Post by: roberto_sc on June 20, 2013, 03:11:00 PM
cdevl, will it really work?
The camera responsible for UI shouldn't be under a UIRoot object in order to be rescaled in different resolutions?