Author Topic: Keeping Clicking NGUI widget from going through  (Read 8957 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Keeping Clicking NGUI widget from going through
« Reply #15 on: February 21, 2014, 10:57:36 PM »
You need to switch UICamera to Unity2D event type. You either do 3D raycasts, or 2D, can't do both.

Lautaro

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 44
    • View Profile
Re: Keeping Clicking NGUI widget from going through
« Reply #16 on: February 22, 2014, 07:34:35 AM »
Yes i tried that before writing the last message. Still nothing happens. This is how its setup now.
  • I have deactivated everything except the test objects and the Ngui UI hierarchy.
  • I have the standard NGUI default camera set to UI Event type and Ortographic Projection.   
  • The main camera is also Ortographic. It has UI Camera and is set up like this http://i.imgur.com/vrYPmK7.png
  • My test object has a sprite renderer, a Collider2D and a particle system. I also have a script with the following: http://pastebin.com/guzf8VXQ

No events are raised.

Lautaro

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 44
    • View Profile
Re: Keeping Clicking NGUI widget from going through
« Reply #17 on: February 22, 2014, 10:43:38 AM »
Now i started a totally new project to try the NGUI events for 2D Colliders.
Here is the setup:
http://i.imgur.com/EvKXZb8.png
http://i.imgur.com/BeythoC.png

and the script: http://pastebin.com/1ZSM54bS

Still no events.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Keeping Clicking NGUI widget from going through
« Reply #18 on: February 22, 2014, 11:18:42 AM »
I see no reason for that. I just did a quick test:

1. New scene.
2. Dragged in a 2D sprite.
3. Added a box2D collider to it.
4. Attached UICamera to the camera.
5. Changed the event type to Unity2D.
6. Attached this script to the sprite:
  1. using UnityEngine;
  2.  
  3. public class Test2 : MonoBehaviour
  4. {
  5.         void OnClick () { Debug.Log("Click"); }
  6. }
7. Hit play, click, debug log shows up.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Keeping Clicking NGUI widget from going through
« Reply #19 on: February 22, 2014, 11:20:50 AM »
Attendum: I'm guessing its your camera setting. You have it set to Orthographic, with Near of 0.3 and far of 1000. Sprite is likely at a position where no events can be received.

Set near to -100, far to 100. In my case I kept the default perspective setting, and it works as expected.

Lautaro

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 44
    • View Profile
Re: Keeping Clicking NGUI widget from going through
« Reply #20 on: February 22, 2014, 12:12:41 PM »
Thanks for your help. I have done exactly as you with 2 new projects, the last one with near and far as you stated. But it doesnt work. However now i got something to happen. Events are triggered but the collider hit area is not where it is put. In the inspector Its the same as the sprite bounding box as default, but to get mouse events  when running i need to move the mouse to the left screen. Here you can see, green shows collider2D and red shows what area triggers mouse events.

http://i.imgur.com/QK1flPg.png

I have rechecked that i have NGUI 351 and i do.

Lautaro

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 44
    • View Profile
Re: Keeping Clicking NGUI widget from going through
« Reply #21 on: February 22, 2014, 03:11:48 PM »
It works perfectly if i change the collider to a normal 3d collider and change tu UICamera event type to "Normal".

I also just deleted NGUI from the roaming asset folder and re-imported it from the asset store, making sure its v 351.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Keeping Clicking NGUI widget from going through
« Reply #22 on: February 23, 2014, 12:08:38 PM »
My fault. I assumed the OverlapPoint takes a screen position... turns out it needs a world position. Uh. Yeah.

In UICamera.Raycast function around line 675 there is this code:
  1.                         else if (cam.eventType == EventType.Unity2D)
  2.                         {
  3.                                 Collider2D c2d = Physics2D.OverlapPoint(pos, mask);
  4.  
  5.                                 if (c2d)
  6.                                 {
  7.                                         hit = lastHit;
  8.                                         hit.point = pos;
  9.                                         hoveredObject = c2d.gameObject;
  10.                                         return true;
  11.                                 }
  12.                                 continue;
  13.                         }
It should be:
  1.                         else if (cam.eventType == EventType.Unity2D)
  2.                         {
  3.                                 pos = currentCamera.ScreenToWorldPoint(inPos); // <-- this line is missing
  4.                                 Collider2D c2d = Physics2D.OverlapPoint(pos, mask);
  5.  
  6.                                 if (c2d)
  7.                                 {
  8.                                         hit = lastHit;
  9.                                         hit.point = pos;
  10.                                         hoveredObject = c2d.gameObject;
  11.                                         return true;
  12.                                 }
  13.                                 continue;
  14.                         }
P.S. You will also need to use an orthographic camera positioned at Z of 0. In my case I used Near plane of -10, Far of +10.

Lautaro

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 44
    • View Profile
Re: Keeping Clicking NGUI widget from going through
« Reply #23 on: February 23, 2014, 12:42:14 PM »
OK!Thanks! Hey dont feel bad about it. Im just happy we sort it out. I thought I was going crazy. So your reply is a blessing. Cheers!