Author Topic: NGUI Button click vs. click on the underlying mesh  (Read 6364 times)

cdevl

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
NGUI Button click vs. click on the underlying mesh
« 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
« Last Edit: June 20, 2013, 10:30:14 AM by cdevl »

MrTact

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 32
    • View Profile
Re: NGUI Button click vs. click on the underlying mesh
« Reply #1 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;
}

cdevl

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: NGUI Button click vs. click on the underlying mesh
« Reply #2 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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI Button click vs. click on the underlying mesh
« Reply #3 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.

cdevl

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: NGUI Button click vs. click on the underlying mesh
« Reply #4 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?

cdevl

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: NGUI Button click vs. click on the underlying mesh
« Reply #5 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.

cdevl

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: NGUI Button click vs. click on the underlying mesh
« Reply #6 on: June 20, 2013, 02:17:59 PM »
ArenMook - thank you for quick reply and solution.

roberto_sc

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: NGUI Button click vs. click on the underlying mesh
« Reply #7 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?