Author Topic: Unity Raycasting + NGUI Button  (Read 5887 times)

Chrimson

  • Guest
Unity Raycasting + NGUI Button
« on: August 12, 2013, 08:29:21 PM »
I have a couple of meshcolliders in a Unity scene, as well as an NGUI Button.

When I click the mouse, I call a Physics.Raycast aimed using `Camera.main.ScreenPointToRay(Input.mousePosition);`. This works as expected.

When I click the NGUI button, and there is a mesh collider "behind" the button, it triggers both the button & and the meshcollider -- that is to say, the NGUIButton does not stop the raycast, and the result is that both the meshcollider is clicked AND the button is triggered.

This combination is resulting in erroneous behaviour.

I am looking for help / suggestions on how to fix this -- right now NGUI is not under the hierarchy of the main camera (since the main camera moves, and the GUI doesn't), but moving the anchors under it did not seem to help. Any thoughts or suggestions?


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Unity Raycasting + NGUI Button
« Reply #1 on: August 13, 2013, 08:26:08 PM »
You need to use NGUI's events, not do your own raycasting. UICamera.lastHit already gives you the result of the last raycast.

Chrimson

  • Guest
Re: Unity Raycasting + NGUI Button
« Reply #2 on: August 13, 2013, 10:46:39 PM »
You need to use NGUI's events, not do your own raycasting. UICamera.lastHit already gives you the result of the last raycast.

My apologies, I wasn't clear; The raycast is not intended to check for the button press.The NGUIButton script is working fine in that respect.

The problem I'd trying to solve is how to *not* trigger my, unrelated, raycast when the cursor is over the button, since the point of the raycast is to pick a GameObject target for the actual NGUI button, and I don't want to accidentally change targets when trying to trigger the button.

galuodo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 65
    • View Profile
Re: Unity Raycasting + NGUI Button
« Reply #3 on: August 15, 2013, 03:38:29 AM »
you can try to delete the code which set mask to 0 in UICamera

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Unity Raycasting + NGUI Button
« Reply #4 on: August 15, 2013, 06:59:01 AM »
if (UICamera.hoveredObject != null) <do your thing>

Chrimson

  • Guest
Re: Unity Raycasting + NGUI Button
« Reply #5 on: August 30, 2013, 04:04:23 PM »
if (UICamera.hoveredObject != null) <do your thing>

Awesome. Worked like a charm!