Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: jjobby on April 22, 2012, 04:42:50 AM

Title: How to detect if mouse is clicking on NGUI area?
Post by: jjobby on April 22, 2012, 04:42:50 AM
Hi, I have a scene that player can rotate camera by holding left click and moving the mouse. The script which is used to rotate camera has its own input detection. But I don't want camera to rotate if the mouse is on NGUI area. How can I check if the mouse is clicking on NGUI area?
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: loopyllama on April 22, 2012, 02:02:16 PM
You are processing your mouse movements in a script. NGUI processes mouse movements too. You want to know in your mouse movement code if NGUI has processed the mouse movement. Use Unity script evaluation order to ensure your code gets processed later than Update meaning your code will be evaluated after NGUI. Now you only need to attach a script to your NGUI widget, object, background, whatever, that sets a public static bool to true in an NGUI Event like OnPress. So your attached script would have an event listener for OnPress in Awake, and a method that sets a public static bool to true if the bool passed to you from OnPress is true, or you could use OnHover or OnDrag or whatever best suits your needs. Then in your mouse movement code you can check the value of the public static bool and respond accordingly. Don't forget to reset the bool back to false!
The NGUI code is adequately documented. Check out UICamera for events you can listen for.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: ArenMook on April 22, 2012, 03:18:20 PM
I'd just like to add that changing your own input detection to use the NGUI's event system will be far, far easier. :)
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: ArenMook on April 22, 2012, 03:20:28 PM
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.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: milali on April 23, 2012, 06:27:12 AM
I'd just like to add that changing your own input detection to use the NGUI's event system will be far, far easier. :)

so your saying make a panel over the main screen play and when you touch it. move the camera as you want to, rather than handle the on touch events manually?
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: loopyllama on April 23, 2012, 07:29:49 AM
I think he is saying do what he did in windward. otherwise you'd have to go the route I mentioned and make your touches and NGUIs touches talk to each other, which would make for code that is not pretty.  ;)
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: ArenMook on April 23, 2012, 01:24:27 PM
What I did in Windward is the ideal approach for handling events that fall through the UI onto your game.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: milali on April 24, 2012, 04:59:25 AM
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.

sorry for the numpty question, what do you mean by game event listener script?
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: ArenMook on April 24, 2012, 02:07:27 PM
Just a script you write attached to a game object you designate as UICamera.fallThrough. Implement functions inside: OnClick, OnPress, etc -- and these will only be called if the UI doesn't handle them.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: milali on April 25, 2012, 04:37:03 AM
nice! will do!
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: milali on April 26, 2012, 04:15:02 AM
one question:

how do you handle multiple touches (for zoom and rotation) at the same time with ngui interface for on mousedown etc
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: TericDragon on April 26, 2012, 11:32:43 AM
We ran into this problem ourselves with our game project.  Since the input controller for our game was already well established by the time we decided to investigate NGUI, it was not practical to replace our current working input system with NGUI input handling.

In order to solve the problem of input fall-through, I made sure that all NGUI elements are on the UI layer, and I implemented the following in our game's main input controller:

  1. // This grabs the camera attached to the NGUI UI_Root object.
  2. Camera nguiCam = GameController.Instance.GameUI.NGUI_Manager.GetComponentInChildren<Camera>();
  3.  
  4. if( nguiCam != null )
  5. {
  6.         // pos is the Vector3 representing the screen position of the input
  7.         Ray inputRay = nguiCam.ScreenPointToRay( pos );    
  8.         RaycastHit hit;
  9.  
  10.         if( Physics.Raycast( inputRay.origin, inputRay.direction, out hit, Mathf.Infinity, LayerMask.NameToLayer( "UI" ) ) )
  11.         {
  12.                 // UI was hit, so don't allow this input to fall through to the gameplay input handler
  13.         }
  14. }
  15.  
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: ArenMook on April 26, 2012, 03:18:59 PM
one question:

how do you handle multiple touches (for zoom and rotation) at the same time with ngui interface for on mousedown etc
UICamera.lastTouchID tells you the ID of the touch / mouse / controller event that triggered OnClick, OnPress, etc function that you're in.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: milali on April 28, 2012, 10:14:15 PM
Thanks for the replies, I will put more time into this
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: theHost on May 12, 2012, 06:52:41 PM
Sorry to resurrect this old thread, but I thought it be best to post a followup question here than a new thread.

I am trying out Aren's suggestion of attaching my input handler object by setting UICamera.fallThrough to it. There are at least 2 issues I am coming across.
The OnKey only starts firing once I press the mouse on to the screen after hitting play (in the editor). It's almost like it's not active until the mouse activates the game screen. Normally Unity will take key input as soon as you hit play as long as the editor window is active.
I only seem to get OnPress (and other events) once when I initially do the press down. Is there a way to get continuos callbacks when the touch is down? Maybe I am missing something here?
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: ArenMook on May 12, 2012, 08:17:53 PM
Continuous callbacks? NGUI doesn't work like that. If you want it to be continuous, use Input. Just check to see which object has focus to see if you should be processing the events or not.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: NewwaveNick on June 17, 2012, 02:27:15 AM
I don't mean to necro-post too badly, but there's something worth contributing to this thread for anyone that comes across it looking for a solution to this problem (like I did recently.)

We ran into this problem ourselves with our game project.  Since the input controller for our game was already well established by the time we decided to investigate NGUI, it was not practical to replace our current working input system with NGUI input handling.

In order to solve the problem of input fall-through, I made sure that all NGUI elements are on the UI layer, and I implemented the following in our game's main input controller:

  1. // This grabs the camera attached to the NGUI UI_Root object.
  2. Camera nguiCam = GameController.Instance.GameUI.NGUI_Manager.GetComponentInChildren<Camera>();
  3.  
  4. if( nguiCam != null )
  5. {
  6.         // pos is the Vector3 representing the screen position of the input
  7.         Ray inputRay = nguiCam.ScreenPointToRay( pos );    
  8.         RaycastHit hit;
  9.  
  10.         if( Physics.Raycast( inputRay.origin, inputRay.direction, out hit, Mathf.Infinity, LayerMask.NameToLayer( "UI" ) ) )
  11.         {
  12.                 // UI was hit, so don't allow this input to fall through to the gameplay input handler
  13.         }
  14. }
  15.  
This solution is really good! I was in the exact same situation where I needed to make minimal impact on an existing project, and this dropped in cleanly. So thank you for contributing this!

However, there's a small bug in the script. Instead of:
LayerMask.NameToLayer( "UI" )

It should be:
1 << LayerMask.NameToLayer( "UI" )

This caused me a bit of headache since it worked at first, but once I added a layer below UI it suddenly broke. Just wanted to note it for anyone reading because it's a good snippet otherwise.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: ArenMook on June 17, 2012, 06:41:43 PM
GetComponentInChildren is a slow operation, so using it every frame is a bad idea. UICamera.mainCamera gives you the camera instead.

Casting a ray into the screen is also unnecessary, assuming "pos" is mouse position. UICamera.hoveredObject already tells you what's under the mouse.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: NewwaveNick on June 20, 2012, 02:40:51 AM
GetComponentInChildren is a slow operation, so using it every frame is a bad idea. UICamera.mainCamera gives you the camera instead.

Casting a ray into the screen is also unnecessary, assuming "pos" is mouse position. UICamera.hoveredObject already tells you what's under the mouse.
Ah yes, that should have been cached. Now I don't have to though, since you already have it available.

And I definitely did not know about the hover object functionality. Nice! Same basic principal though as the original script. They both will drop into my project rather cleanly.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: Rafe on July 21, 2012, 02:06:53 AM
I also have a question as I attempt this. I have at least three scripts on different objects that need to handle events. Are there any delegates I can use? Otherwise it seems like I need a separate gameobject to use as the fall through receiver with a script that exposes static event delegates for my other scripts. WDYT?
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: ArenMook on July 21, 2012, 04:25:52 PM
The latter. Set up a single object as UICamera.genericEventHandler, and let it be your central point for event distribution. Generic event handler will receive all events, regardless of whether they were handled by the UI or not.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: Rafe on July 21, 2012, 06:14:18 PM
I have been mocking up some ideas but I still feel like I am missing some information. I think I have boiled it down to a specific need...

* My GUI camera (2D) will see things that need to be drag-and-drop. It also needs to block all events to the game, so if I click or drag on a button, the game doesn't receive any fall-through input.

* My game camera (3D) is controlled by dragging anywhere in the game. There are a lot of 3D GUI elements that receive clicks, but all drags in-game are for the camera (Our camera script is a special behaviour, so I need my current script.)

I tried to set this up by setting the 3D camera mask to nothing, or deleting it, but in both cases the fall-through hover object is "FallThrough EventHandler". Here is my debug log for the click from the code below:
  1. Clicked FallThrough EventHandler (UnityEngine.GameObject)
  2. UnityEngine.Debug:Log(Object)
  3. UIGameCamera:OnClick() (at Assets/Scripts/UI/UIGameCamera.cs:17)
  4. UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
  5. UICamera:Notify(GameObject, String, Object) (at Assets/NGUI/Scripts/UI/UICamera.cs:517)
  6. UICamera:ProcessTouch(Boolean, Boolean) (at Assets/NGUI/Scripts/UI/UICamera.cs:957)
  7. UICamera:ProcessMouse() (at Assets/NGUI/Scripts/UI/UICamera.cs:761)
  8. UICamera:Update() (at Assets/NGUI/Scripts/UI/UICamera.cs:623)
Here is the simple test class I am using to try this out:
  1. public class UIGameCamera : MonoBehaviour
  2. {
  3.     private void Start()
  4.     {
  5.         UICamera.fallThrough = this.gameObject;
  6.     }
  7.  
  8.     private void OnClick()
  9.     {
  10.         Debug.Log("Clicked " + UICamera.hoveredObject);
  11.     }
  12.  
  13.     private void OnDrag(Vector2 delta)
  14.     {
  15.         Debug.Log("Dragging Camera");
  16.     }
  17. }

If I could get the object that is clicked, this would be done. If I am not doing anything wrong, and I need to do a quick raycast, that is fine. I just want to know I am following nGUI methodology and not trying to fight it.

Thanks a lot!


P.S. I bought through the Unity store because I didn't have anything in our PayPal account and the current free version isn't the latest version. I would have liked to buy direct.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: ArenMook on July 21, 2012, 08:27:08 PM
Assuming your event mask on the UICamera is set up correctly, all the objects seen by that camera will be receiving events (and will intercept them so they don't end up on the fallThrough object).
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: Rafe on July 22, 2012, 02:33:50 AM
The issue is that the hover object is always the fall-through game object, and not the gameObject that is visible under the mouse. It seems like I have a couple of choices:

1) Use UICamera on all cameras. In this case there is no fall-through, but the hover-object is always good, so I would have to check the hover object's layer against the 2D camera layer to see if it is a 2D GUI hit or a 3D game camera hit

2) I can use a UICamera on just the 2D camera. Fall-through now works, but the hover object is always this hidden fall-through gameObject, not any of my objects, so I would have to use the nGUI pos to do a ray cast and see what gets hit.

I'm going with #2 since I already have the code to do this from the old system. I am interested if I am missing something here though.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: Rafe on July 22, 2012, 03:10:04 AM
...continuing from my last post, I have it nearly working. The issue I am having is that OnClick is being called 'on mouse up' no matter what. I can click anywhere, drag around and OnClick is called when I let go.

  1. public class UIGameCamera : MonoBehaviour
  2. {
  3.     public LayerMask layers;
  4.     public bool debug = false;
  5.  
  6.     private Camera gameCam;
  7.  
  8.     private void Start()
  9.     {
  10.         this.gameCam = this.GetComponent<Camera>();
  11.         UICamera.fallThrough = this.gameObject;
  12.     }
  13.  
  14.     private void OnClick()
  15.     {        
  16.         // Can't select anything when the game is paused
  17.         if (Time.timeScale == 0) return;
  18.  
  19.         Ray ray = this.gameCam.ScreenPointToRay(UICamera.currentTouch.pos);
  20.         RaycastHit hit;
  21.         if (!Physics.Raycast(ray, out hit, Mathf.Infinity, this.layers))
  22.         {
  23.             Debug.Log("Nothing Clicked");
  24.             return;
  25.         }
  26.  
  27.         // Cache the transform of the object hit
  28.         Transform hitXform = hit.collider.transform;
  29.  
  30.         Debug.Log("Clicked " + hitXform.name);
  31.     }
  32.  
  33.     private void OnDrag(Vector2 delta)
  34.     {
  35.         Debug.Log("Dragging Camera");
  36.     }
  37. }
  38.  
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: ArenMook on July 22, 2012, 01:54:48 PM
"hover object is always the fall-through game object" that makes no sense to me. Did you mean that in reverse?

Generally when dragging you should disable the collider of whatever you started dragging, or it will be intercepting events. Also when you can always modify UICamera.currentTouch.pressed game object to be something else, and that something else will receive your OnClick instead.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: Rafe on July 22, 2012, 02:05:33 PM
"hover object is always the fall-through game object" that makes no sense to me. Did you mean that in reverse?

I mean that whatever GameObject is set to UICamera.fallThrough is always returned by UICamera.hoveredObject when an event "falls through". I was hoping that UICamera.hoveredObject would always be the collider (GameObject) under the mouse. UICamera.fallThrough is already recieving the events. Having UICamera.hoveredObject returning the same thing isn't helpful (maybe it is helpful to non-fall-through recievers, but I thought I read earlier in this thread that you recommended UICamera.hoveredObject for the situation I am working with?)

Generally when dragging you should disable the collider of whatever you started dragging, or it will be intercepting events.

I am not dragging an object, I am dragging the mouse. The me explain better.... The OnClick() event is getting sent On-mouse-up no matter what. If I click and let go, I get the object that was clicked as expected. if I click, then drag the mouse over another collider, then let go, the new collider is returned. A click event should be interrupted, and not sent, when a drag event occurs after mouse-down, right?


Also when you can always modify UICamera.currentTouch.pressed game object to be something else, and that something else will receive your OnClick instead.

Cool trick, thanks!
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: Rafe on July 24, 2012, 02:54:36 AM
I figured out the problem I was having with OnClick always being fired even after a drag. The real object being hit for fall-through is  UICamera.hoveredObject. So clicking, then dragging anywhere in the fall-through area, and then letting go, is technically releasing on the same UICamera.hoveredObject. I added my own bool to figure out if there was a drag.

While I'm happy this is taking less code than my old version, it had a steep learning curve that could be removed by giving me a hook in to what is a GUI vs non-GUI hit. Everything has been so easy except for sorting out these events...just my opinion of course.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: ArenMook on July 24, 2012, 03:05:59 AM
No need for a boolean value. You can simply set:
  1. UICamera.currentTouch.clickNotification = UICamera.ClickNotification.None;
...when you start your dragging process.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: pretender on September 27, 2012, 09:40:40 AM
i did this and it works, is it that easy?

  1. void OnTapHandler(Vector2 pos)
  2. {
  3.    if(UICamera.hoveredObject == null)
  4.    {
  5.         //...tap handler code
  6.    }
  7.  
  8.  

i have few places where i can check this, mostly in events, so no every update loop, is there any drawbacks in doing it this way?
thanks!
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: pretender on September 27, 2012, 04:40:23 PM
ok i see, this does not work for touch devices, and it is logical ;D since it is about hover...
anything similiar i could do to make it work on touch devices?
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: pretender on September 27, 2012, 05:07:34 PM
ok this seems to work for android

void OnTapHandler(Vector2 pos)
{
 
 if(UICamera.lastHit.collider == null)
   {
        //...tap handler code
   }

}

i hope i do not do something wrong...there are no significant slowdowns
is this a proper way of doing this?
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: ArenMook on September 27, 2012, 06:29:33 PM
It's fine.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: twDuke on March 10, 2013, 09:11:30 AM
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.

Hi, i would be really gratefull if you could setup some example scene or scripts for how to do this.

BR
TWD
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: toniepeli on March 25, 2013, 09:47:25 AM
Hey,

I tried many different ways also but the last that I got working properly was to merge the Tags of the NGUI elements for example panels to something like GUIElement and then raycast

var ray:Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (RayCastX(ray, "GUIElement"))
   mouseOnGUI = true;

That raycasting should be made to go through all until the proper tag is found or null :)
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: fwalker on May 23, 2013, 08:14:10 PM
There seems to be a few solutions to this in this thread. Is there a final official way to do this?
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: ArenMook on May 23, 2013, 08:55:09 PM
What I suggested is what I used, so you can consider it "official" if you like.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: titin_lc on October 15, 2013, 04:48:13 AM
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.

Your method sounds great if you have a centralized game event listener. But in my case, the scene has a lot of event listeners because almost every object do their job inside OnMouseDown() methods.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: rebelcoder on December 07, 2013, 10:16:34 PM
Just came across this issue on my current project. I would like to contribute by posting my solution. NGUI is AWESOME :)


In UICamera.cs I made the following changes


   public static bool ObjectWasHit;

   void FixedUpdate ()
   {
      if (useMouse && Application.isPlaying && handlesEvents)
      {
         ObjectWasHit = Raycast(Input.mousePosition, out lastHit);

         if (!ObjectWasHit)
            hoveredObject = fallThrough;

         if (hoveredObject == null)
            hoveredObject = genericEventHandler;

         for (int i = 0; i < 3; ++i)
            mMouse.current = hoveredObject;
      }
   }

Then elsewhere I do

void SomeInputHandler()
{
    if(UICamera.ObjectWasHit)
     return;
}
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: McSwan on January 15, 2014, 08:07:12 AM
Ok, this was confusing for me but I got it now. Follow aren's initial advice.

In short, events not captured by the UI, can get captured by your GameEventHandler .

Like this Demo version I have whipped up-

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class GameEventHandler : MonoBehaviour {
  5.  
  6.         private bool leftMouseDown = false;
  7.         private bool rightMouseDown = false;
  8.         private bool middleMouseDown = false;
  9.  
  10.  
  11.         // Use this for initialization
  12.         void Start ()
  13.         {
  14.                 UICamera.fallThrough = this.gameObject;
  15.         }
  16.        
  17.         void OnClick()
  18.         {
  19.                 Debug.Log("Mouse Click captured in GameEventHandler");
  20.         }
  21.  
  22.         void OnPress()
  23.         {
  24.  
  25.                 Debug.Log("Pressing mouse");
  26.                 if(Input.GetMouseButtonDown(0))
  27.                 {
  28.                         leftMouseDown = true;
  29.                         Debug.Log("Left mouse Down");
  30.                 }
  31.                 else
  32.                 if (Input.GetMouseButtonUp(0) )
  33.                 {
  34.                         leftMouseDown = false;
  35.                         Debug.Log("Left mouse Up");
  36.                 }
  37.  
  38.                 if(Input.GetMouseButtonDown(1))
  39.                 {
  40.                         rightMouseDown = true;
  41.                         Debug.Log("rightMouseDown");
  42.                 }
  43.                 else
  44.                         if (Input.GetMouseButtonUp(1) )
  45.                 {
  46.                         rightMouseDown = false;
  47.                         Debug.Log("rightMouse Up");
  48.                 }
  49.  
  50.                 if(Input.GetMouseButtonDown(2))
  51.                 {
  52.                         middleMouseDown = true;
  53.                         Debug.Log("middleMouseDown");
  54.                 }
  55.                 else
  56.                         if (Input.GetMouseButtonUp(2) )
  57.                 {
  58.                         middleMouseDown = false;
  59.                         Debug.Log("middleMouse Up");
  60.                 }
  61.         }
  62.  
  63.         void OnHover()
  64.         {
  65.                 Debug.Log("Your Hovering over a button, which doesn't have a onHover over-ride on the button itself");
  66.         }
  67. }
  68.  
  69.  
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: SamK on November 12, 2014, 12:42:27 AM
Im facing the same problem. Im making an isometric game. When i press a button on HUD and drag or any BG/button on a UI and drag the ray goes through and ISO functionalites are performed like drag or click.

Isnt there any Trigger/listener/dataMember liken when i press a button and drag the mouse/finger out the state remains pressed.
and Dragging is another case but simple click also goes through.

here is a code snippet of what im doing. If goes into this first code block but it also goes forward meaning skips this.

  1. if(iPhoneInput.touchCount > 0)
  2.         {
  3.             iPhoneTouch touch = iPhoneInput.touches[0];
  4.             ray = gState.world.nGUICamera.ScreenPointToRay(touch.position);                
  5.             if (Physics.Raycast(ray, out hit, Mathf.Infinity, LayerMask.NameToLayer("UI")) && (
  6.                 touch.phase == iPhoneTouchPhase.Ended || touch.phase == iPhoneTouchPhase.Canceled
  7.                 || touch.phase == iPhoneTouchPhase.Began))
  8.             {
  9.                 Debug.Log("IN CANCLE MODE");
  10.                 CancelTouchMode(); // MyFUcntion
  11.                 return;
  12.             }
  13.         }
  14.  
  15.  
  16. and down some where in update i  do
  17. if(iPhoneInput.touchCount==1 )
  18.                 {        
  19.                         SingleTouchAction();
  20.                 }


HOw to block this touch.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: SamK on November 12, 2014, 02:09:29 AM
I achieved what i wanted to do .. no need of anything. Didnt acutally replace the Input with NGUI touch.. I added Ngui touch.
like i was doing before .
  1. if(Input.touchCount==1)
  2.         {
  3.             Debug.Log("SingleTouchAction");
  4.             SingleTouchAction();
  5.         }
  6. Now i did
  7. if(UICamera.touchCount==0 &&  Input.touchCount==1)
  8.         {
  9.             Debug.Log("SingleTouchAction");
  10.             SingleTouchAction();
  11.         }
  12.  

I tried the fall through method .. I tells other objets when UI objects are hit or some event is triggered.
My problem was. When i touched any button the touch went through the button and other ISO world functionalities started performing which it shouldnt in the fisrt place. So  I added this ..
Is this the corrent way ??
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: ArenMook on November 12, 2014, 07:47:05 AM
UICamera.isOverUI -- duplicate posts will receive duplicate replies. :P
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: SamK on December 05, 2014, 05:55:56 AM
Hey aren,
Problem Click/Touch going through.  My game is a touch device game. But obviously i want to make it work on editor too.
WHat i want is when i click any UI the other code should run.
PS. all UI is in UI layer.

UICamera.touch count isnt working.  Well partially
I have the same code.
  1. if(Input.touchCount==1)
  2.         {
  3.             Debug.Log("SingleTouchAction");
  4.             SingleTouchAction();
  5.         }
UICamera.touchCount gives 1 when a ui is pressed but the above code runs (OnClick fucntionality) means when we relese the mouse button or lift the finger.
HOw can i detect any UI is clicked. so I would be able to stop the above code from executing.
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: ArenMook on December 05, 2014, 03:12:36 PM
Ok, for something like this, I'd do it differently. I would register UICamera.onPress to some static delegate, and inside I would keep track of the number of touches over the UI.
  1. using UnityEngine;
  2.  
  3. public class MyTouchCounter : MonoBehaviour
  4. {
  5.     void Start () { UICamera.onPress += MyListener; }
  6.  
  7.     static public int touchCountOverUI = 0;
  8.  
  9.     static void MyListener (bool isPressed)
  10.     {
  11.         if (UICamera.isOverUI)
  12.         {
  13.             if (isPressed) ++touchCountOverUI;
  14.             else --touchCountOverUI;
  15.         }
  16.     }
  17. }
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: SamK on December 09, 2014, 12:58:18 AM
OK will try this .. Is this isOverUI property in the latest version im on 3.68 but  cant find it. I can update to the newer version though.
Hope this works. and if isOverUI is triggered when the the cursor is overUI it think it can work for the editor but how would it work on touch devices ? 
Title: Re: How to detect if mouse is clicking on NGUI area?
Post by: SamK on December 09, 2014, 05:41:27 AM
Ok i did this. moved the UI towrds the screen -z
  1. if (Physics.Raycast(ray, out hit, Mathf.Infinity,1<<LayerMask.NameToLayer( "UI" )))
  2. MyFunction();
  3.  

I already tried this but The UI was behind other objects thats why it didnt worked earlier.
Too damn basic  :P