Author Topic: Detecting hits in NGUI  (Read 8069 times)

Excal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Detecting hits in NGUI
« Reply #15 on: May 09, 2014, 07:35:31 AM »
Your example works perfectly. Do I need to have this line:

  1. UICamera.genericEventHandler = gameObject;

in the Start() of all my objects?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Detecting hits in NGUI
« Reply #16 on: May 09, 2014, 08:49:12 PM »
No... there can be only one generic event handler. It means that the object you're choosing is going to get a copy of all the events that NGUI sends out. I meant that script as a tool for debugging, to see which object is getting the event. If you are seeing the correct object come up, then the collider you're selecting should already be getting the same exact event. You can also enable "Debug" on the UICamera to see what NGUI is hovering over.

Excal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Detecting hits in NGUI
« Reply #17 on: May 09, 2014, 09:02:06 PM »
If I remove that line, nothing seems to show up as a selected object for me. In debug mode, I see feedback indicating the raycasts are hitting things. However, this line of code is in every object and my debug log constantly shows null:

  1.         void OnSelect(bool isSelected)
  2.         {
  3.                 if (isSelected)
  4.                 {
  5.                         Debug.Log("Selected " + UICamera.selectedObject.transform.root.name);
  6.                 }
  7.                 else Debug.Log("Deselected");
  8.         }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Detecting hits in NGUI
« Reply #18 on: May 09, 2014, 11:36:27 PM »
There is no magic here. If the Debug mode shows you hovering over objects, those objects will receive events. You can try using simple functions like "void OnClick () { Debug.Log("Clicked!"); }" as well. Remember, Select event is only sent out once. Repeated clicks on the same object won't send a Select event.

Also not sure why "UICamera.selectedObject.transform.root.name" is there. If this function is on a script attached to the object with a collider, then Debug.Log("Selected " + name); is enough.

Excal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Detecting hits in NGUI
« Reply #19 on: May 10, 2014, 01:18:34 AM »
I have several objects that combine to create a single object. For example, two cubes that represent the first floor and second floor of a building. These are parented to a single game object that has the name of the object as a whole, such as 'building'.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Detecting hits in NGUI
« Reply #20 on: May 10, 2014, 01:33:58 AM »
Well, the event will go to the object with the collider on it.

Excal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Detecting hits in NGUI
« Reply #21 on: May 10, 2014, 02:07:55 AM »
Should I remove the colliders for the individual objects that make up the overall object and just have a single collider? When I click, I get stuff like 'floor1' and 'floor2' when both are part of 'building', even though 'building' also has a collider on it.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Detecting hits in NGUI
« Reply #22 on: May 10, 2014, 11:53:10 PM »
That, or have a script on the "floor" that would do something else, like select the building.