Author Topic: Hit order for overlapping UIs?  (Read 9171 times)

apparition

  • Guest
Hit order for overlapping UIs?
« on: June 23, 2012, 03:55:59 PM »
Let's say I have two NGUI UIs on different layers.  If both UIs have a button in the same position and I move the mouse over the two buttons, how does NGUI determine which button is hit?  Is it arbitrary?  I've tried tweaking camera depths, panel Z positions, and collider Z positions, but none of it seems to have any impact on the order.

Essentially, I have a large panel that I've set up with a UIDragCamera so the player can drag it left and right, and I'd like to add Left and Right buttons on top as an alternate scrolling option.  However, when I set this up the mouse always seems to hit the collider on the draggable panel and the Left and Right buttons on top can't be hit.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Hit order for overlapping UIs?
« Reply #1 on: June 23, 2012, 04:06:51 PM »
It's determined by Unity, not NGUI. Whichever collider is in front gets the event.

apparition

  • Guest
Re: Hit order for overlapping UIs?
« Reply #2 on: June 23, 2012, 04:20:24 PM »
What if the UIs have different cameras?  I forgot to mention this detail above.

Here's what I mean:



I moved the collider for "button 1" back, but it's still getting the event.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Hit order for overlapping UIs?
« Reply #3 on: June 23, 2012, 04:31:54 PM »
Then the first camera to draw (highest depth) is always first.

apparition

  • Guest
Re: Hit order for overlapping UIs?
« Reply #4 on: June 23, 2012, 04:46:46 PM »
Thanks for the quick replies, Aren!

I don't know if I'm doing something wrong but the camera depth has no effect for me.  :(  I mean, it does affect the drawing order, naturally, but it doesn't affect which button is hit.  I always hit "button 1".

EDIT: I take it back!  Setting the camera depths DOES work if I set everything up before starting the game.  I was doing it during runtime before.  Still need to play with this a bit more though...

EDIT 2: Ok, there it is in the Awake() method in UICamera.  The cameras are sorted by depth for event handling.  So as long as you set up the camera depths before starting the game, the buttons get events in the proper order.  Hurray for NGUI!  :D
« Last Edit: June 23, 2012, 05:10:10 PM by apparition »

cowlinator

  • Guest
Re: Hit order for overlapping UIs?
« Reply #5 on: February 05, 2013, 05:40:31 PM »
After trying to get this to work for a while, I've solved my issues.
I'm posting this in case anybody else has similar issues.

When using 2 or more UIRoots (multiple cameras), all of the cameras except the one with the lowest depth (farthest back) must have their Clear Flags set to Depth Only or Don't Clear.  Otherwise only the camera with the highest depth will show anything.
(Also, when using a custom layer, be sure to set the Event Receiver Mask on the camera.)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Hit order for overlapping UIs?
« Reply #6 on: February 06, 2013, 12:01:20 PM »
That holds true for all multi-camera setups, cowlinator -- not just NGUI.

regeter

  • Guest
Re: Hit order for overlapping UIs?
« Reply #7 on: August 25, 2013, 04:28:45 PM »
I cannot get this to work for my little project. I am fairly new to nGUI any unity itself too.
I tried the 2 camera setup but it still triggers my larger button each time.
Here is my setup:
https://docs.google.com/file/d/0B4K_bJVrt0Kia2laMHdhNkFfOXc/edit?usp=sharing
Essentially two sphere collider on top of each other, anchored to the corner so only 1/4 is visible each.
Every mouse click always triggers the effect for the larger (light green) button, I want to get the system so that clicking into the smaller sphere only triggers that button.
« Last Edit: August 25, 2013, 04:37:36 PM by regeter »

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: Hit order for overlapping UIs?
« Reply #8 on: August 26, 2013, 06:14:42 AM »
Using spheres for square objects is madness. Remember that a sphere is round and a box is flat, it is not a circle. In other words, if you have a massive object, the height also will be massive and you will need to move the second object higher than that of the width of the other object too. You might have more luck with capsule colliders, but I would stick with box, otherwise you are just asking for trouble.

regeter

  • Guest
Re: Hit order for overlapping UIs?
« Reply #9 on: August 26, 2013, 02:49:44 PM »
well my bitmap is round, and this is what I want the button to be. I guess the other solution is to create a mesh collider? I thought the sphere collider was a good solution, it works well with my other round buttons, I just cannot get the kind of "wheel" effect I'd like.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Hit order for overlapping UIs?
« Reply #10 on: August 27, 2013, 02:40:15 AM »
If you have two colliders you must position them accordingly -- smaller one in front by adjusting its Z position.

regeter

  • Guest
Re: Hit order for overlapping UIs?
« Reply #11 on: August 27, 2013, 02:04:04 PM »
Thank you. Changing the Z value for the collider did it. I had done this earlier but not sufficiently large values.
Do I even still need 2 cameras setup now? I will do some tests with just one and report back.