Author Topic: How to check if touch/click is processed by NGUI or not?  (Read 6477 times)

xikky

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 46
    • View Profile
How to check if touch/click is processed by NGUI or not?
« on: June 17, 2013, 01:42:14 PM »
I am sorry for bringing up this problem again but I couldn't understand the proper solution (using UICamera.fallThrough) which I found in this post here: http://www.tasharen.com/forum/index.php?topic=138.0

I had problems such as:

Which UICamera? The UICamera child of UIRoot or another UICamera that we should at to the other MainCamera. And what if the other MainCamera is a 2dtkCamera? How do you go through the whole process to create a centralpoint for event distribution?

Would really appreciate if you write up a quick tutorial about this, and I am sure that it would also help many others!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to check if touch/click is processed by NGUI or not?
« Reply #1 on: June 17, 2013, 09:44:13 PM »
Which camera? I don't follow... UICamera.fallThrough is a static variable. It doesn't require an instance of UICamera.

xikky

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 46
    • View Profile
Re: How to check if touch/click is processed by NGUI or not?
« Reply #2 on: June 18, 2013, 05:25:38 AM »
Sorry for the miss information ...

I was mostly talking about your post here:

Btw, what I did in Windward is simply attach a UICamera script to the main game camera, and set its event mask to "nothing". I then set the UICamera.fallThrough to my "game event listener" script, and all the events that don't get handled by the UI simply go to that script.

I'm a total beginner with NGUI and I can't find out what should I do exactly ..

I tried another solution but I found another problem. I posted the problem here: http://www.tasharen.com/forum/index.php?topic=4693.0

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to check if touch/click is processed by NGUI or not?
« Reply #3 on: June 18, 2013, 12:07:52 PM »
Here is an example. Attach this script to an empty game object.
  1. using UnityEngine;
  2.  
  3. public class MyGenericHandler : MonoBehaviour
  4. {
  5.         void Start ()
  6.         {
  7.                 UICamera.fallThrough = gameObject;
  8.         }
  9.  
  10.         void OnClick ()
  11.         {
  12.                 Debug.Log("Clicked on nothing!");
  13.         }
  14. }
« Last Edit: June 18, 2013, 02:20:15 PM by ArenMook »

xikky

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 46
    • View Profile
Re: How to check if touch/click is processed by NGUI or not?
« Reply #4 on: June 18, 2013, 12:52:32 PM »
How would I determine if the touch is on an NGUI object or not? I tried:

  1. function Start ()
  2. {
  3.         UICamera.genericEventHandler = gameObject;
  4. }
  5.  
  6. function OnClick ()
  7. {
  8.         if (UICamera.hoveredObject == null)
  9.                 Debug.Log("Clicked on nothing!");
  10.         else
  11.                 Debug.Log("Clicked on something!");
  12. }
  13.  

In any case, "Clicked on something!" is always printed. The only difference is that when I touch on an NGUI object, the log is printed from the Notify function inside UICamera at line 651, but if a touch is done on a non NGUI object, the log is printed from the same source but at line 647.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to check if touch/click is processed by NGUI or not?
« Reply #5 on: June 18, 2013, 02:20:01 PM »
Dur. I mixed up things. It should have been UICamera.fallThrough not the genericEventhandler.

Just to clarify, the way this works is like so:
- If there is some object under the touch, that object will get the event.
- If there is nothing under the touch, UICamera.fallThrough will get the event.
- In both cases, UICamera.genericEventHandler will receive a copy of the event.

xikky

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 46
    • View Profile
Re: How to check if touch/click is processed by NGUI or not?
« Reply #6 on: June 18, 2013, 05:31:53 PM »
I think I cannot apply this solution for my script logic.

I have my Input scripting inside a FixedUpdate, taking care of began, moved, stationary and canceled touches to move a rigidbody around the screen.

I have no use of capturing UICamera.fallThrough state OnClick, since OnClick prints the result when touch is released. Hence if the player touches the screen and still holds his finger on the screen, my input script is still not triggered, unless he releases his finger. This isn't what I need for my situation.

Similarly, OnPress triggers UICamera.fallThrough state immediately when a touch occurs, and again when released. Cannot find a way to apply to my script too.


This is important to me. What I need is to be able to check whether a touch occurs on an NGUI object or not, before input script is executed inside FixedUpdate. Is this allowed?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to check if touch/click is processed by NGUI or not?
« Reply #7 on: June 19, 2013, 12:19:40 AM »
OnPress has a parameter -- a boolean. It tells you whether the touch started (true) or ended (false).
  1. using UnityEngine;
  2.  
  3. public class MyGenericHandler : MonoBehaviour
  4. {
  5.     void Start ()
  6.     {
  7.         UICamera.fallThrough = gameObject;
  8.     }
  9.  
  10.     void OnPress (bool isPressed)
  11.     {
  12.         Debug.Log(isPressed? "Pressed on nothing" : "Released nothing");
  13.         // if 'isPressed', move your rigidbody now
  14.     }
  15. }

xikky

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 46
    • View Profile
Re: How to check if touch/click is processed by NGUI or not?
« Reply #8 on: June 19, 2013, 12:45:01 AM »
Awesome! this worked :D Thank you.

For those working with javascript, place this in your input script:

  1. function Start () {
  2.  
  3.         UICamera.fallThrough = gameObject;
  4. }
  5.  
  6. function OnPress (isPressed)
  7. {
  8.        
  9.         if (isPressed)
  10.                 //do input commands here
  11. }
  12.