Author Topic: How to detect if mouse is clicking on NGUI area?  (Read 75234 times)

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #30 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?

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #31 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?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #32 on: September 27, 2012, 06:29:33 PM »
It's fine.

twDuke

  • Guest
Re: How to detect if mouse is clicking on NGUI area?
« Reply #33 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

toniepeli

  • Guest
Re: How to detect if mouse is clicking on NGUI area?
« Reply #34 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 :)

fwalker

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #35 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?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #36 on: May 23, 2013, 08:55:09 PM »
What I suggested is what I used, so you can consider it "official" if you like.

titin_lc

  • Guest
Re: How to detect if mouse is clicking on NGUI area?
« Reply #37 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.

rebelcoder

  • Guest
Re: How to detect if mouse is clicking on NGUI area?
« Reply #38 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;
}

McSwan

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 3
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #39 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.  

SamK

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #40 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.
« Last Edit: November 12, 2014, 12:57:40 AM by SamK »

SamK

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #41 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 ??

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #42 on: November 12, 2014, 07:47:05 AM »
UICamera.isOverUI -- duplicate posts will receive duplicate replies. :P

SamK

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #43 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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to detect if mouse is clicking on NGUI area?
« Reply #44 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. }