Author Topic: Problem with NGUI and GameObject touch  (Read 295 times)

Kafar

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 24
    • View Profile
Problem with NGUI and GameObject touch
« on: February 17, 2017, 05:46:09 AM »
Hi,

I have a 3d model in a GameObject who handle a mouse down event doing something and a NGUI button over this. When I tap the button the gameobject handle the touch event but the button not.

I want when I tap the button only this handle the event and not the gameobject under the button. And I want when tap the gameobject only this handle the event and not the NGUI button.

In NGUI Camera I set the EventMask to NGUI only but with no result.

Can how to do this?

Thanks

-Kafar

Kafar

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 24
    • View Profile
Re: Problem with NGUI and GameObject touch
« Reply #1 on: February 18, 2017, 02:28:01 PM »
UPDATE:

I tried to put a EventTrigger script on the GameObject and set it to my MouseDown function. I use NGUI with Vuforia and all it was run pefectly. I use the Vuforia main camera for AR (then to view the 3d models) and UIcamera for my NGUI buttons.

When I run the app I have a NGUI button, pressing it run correctly. When I move the phone camera on a marker and my app shows a 3d model and the button is over it and tap on it the button not reply but reply the MouseDown event in script attached on GameObject (3d model).

Can how to disable the event on game object for handle the button event? Please help.

Thanks

-Kafar

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 297
  • -Receive: 1136
  • Posts: 21,937
  • Toronto, Canada
    • View Profile
Re: Problem with NGUI and GameObject touch
« Reply #2 on: February 18, 2017, 07:37:44 PM »
OnMouseDown is not an NGUI event. It's a Unity event and has nothing to do with NGUI.

You want NGUI equivalent -- void OnPress(bool isPressed).

Go back to NGUI's basic tutorials -- all objects with colliders are capable of receiving events provided the camera that draws them has a UICamera script attached. All you need to do is use NGUI's event callback functions, not Unity's. This means OnPress instead of OnMouseDown, OnClick instead of OnMouseClick, etc.

Kafar

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 24
    • View Profile
Re: Problem with NGUI and GameObject touch
« Reply #3 on: February 19, 2017, 06:22:55 AM »
Hi ArenMook,

Thanks for reply.

I changed the OnMouseDown event in script on GameObject with OnPress(bool isPressed) event. There is a MeshCollider on GameObject, on UICamera I set Event type 3D UI, Event Mask to Everything.

Now the result is that when the button is over the GameObject and tap on button the event triggers but if I tap on GameObject the OnPress event not trigger and I dont understand why.

What's I'm wrong?

-Kafar


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 297
  • -Receive: 1136
  • Posts: 21,937
  • Toronto, Canada
    • View Profile
Re: Problem with NGUI and GameObject touch
« Reply #4 on: February 21, 2017, 12:31:11 PM »
3D UI means events will go to colliders. If you are trying to click on an objects in the world, the event type should be set to 3D World instead, as it will cause events to go to rigidbodies. Don't mix UI events with world interaction.

You should have two UICameras. One for the game camera (3D World), and another for your UI camera (3D UI).

rafinha

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Problem with NGUI and GameObject touch
« Reply #5 on: February 23, 2017, 12:06:58 PM »
Hi,

I'm having the same issue too, and I really try to figure out what to do, but I still cant make it work.

I have a button and and object over my button as you can see in my picture.

When I hover the button in the very right, outside of the object works (red line),
Inside the object (blue line), I'm not able to click, the click goes to the object.

I note when I hover the button in the very corner the button hover effects works, but not when is inside the blue line.
I tough this was something related with the Deph but is not since one is an game object and the other the ui (correct)?

I create a simple script for my button click event
  1. using UnityEngine;
  2.  
  3. public class Menu : MonoBehaviour
  4. {
  5.         private UIButton MyButton;
  6.        
  7.         private void Awake()
  8.     {
  9.         MyButton = GameObject.Find("MyButton").GetComponent<UIButton>();
  10.         }
  11.        
  12.         private void Start()
  13.     {
  14.         EventDelegate.Set(MyButton.onClick, delegate () { Test(); });
  15.     }
  16.        
  17.         private void Test()
  18.         {
  19.                 Debug.Log("test button action");
  20.         }
  21. }
  22.  

And one for my object:

  1. using UnityEngine;
  2.  
  3. public class MyObject : MonoBehaviour
  4. {
  5.     void OnPress(bool isPressed)
  6.     {
  7.         if (isPressed)
  8.         {
  9.             Debug.Log("my object click");
  10.         }
  11.     }
  12. }
  13.  

Anybody have any idea what is the problem?

Thanks :)

rafinha

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Problem with NGUI and GameObject touch
« Reply #6 on: February 23, 2017, 02:01:19 PM »
My cameras config:

MainCamera -> UICamera (component)
    - Event type = 2d world

UI Root -> UICamera
    - Event type = 3d ui

rafinha

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Problem with NGUI and GameObject touch
« Reply #7 on: February 23, 2017, 03:01:44 PM »
Found it, I set the main camera depth to -1 and now works ;)