Author Topic: Prevent mouse panning over ngui (or check if mouse clicked ngui)  (Read 4361 times)

Lumind

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 14
    • View Profile
Hi, I got everything done from another my topic (and it works fine) http://www.tasharen.com/forum/index.php?topic=10012.msg47221#msg47221
I have script on my camera to pan with mouse clicked/finger tapped, by the way it's very buggy on android(clipping) :(
  1. private Vector3 startpoint; // startpoint
  2. private Vector3 endpoint; // endpoint
  3. private bool  panning=false;
  4. public float minPosX = 10;
  5. public float maxPosX =10;
  6.         //GameObject CO;
  7. public float minPosZ=-10;
  8. public float maxPosZ=10;
  9. float dist;
  10. bool MouseUse= true;
  11.  
  12. // mainloop
  13. void  Update (){
  14. Ray ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
  15. Ray ray2 = Camera.main.ScreenPointToRay (Input.mousePosition);
  16. if (Input.touchCount == 1) {
  17.  
  18.         // starts here, left mousebutton is pressed down
  19.         if(Input.GetMouseButtonDown(0))
  20.         {
  21.                 // make ray
  22.                 startpoint = ray1.GetPoint (10) ; // Returns a point at distance units along the ray
  23.                 startpoint.y = 0; // fix z to 0
  24.                 panning = true;
  25.         }
  26.  
  27.         // then left mousebutton is released
  28.         if(panning){
  29.         dist = Mathf.Clamp((Vector3.Distance(endpoint, startpoint)),0,1.0f);
  30.                 endpoint = ray2.GetPoint (10) ; // Returns a point at distance units along the ray
  31.                 endpoint.y = 0; // fix z, somehow its not always 0?
  32.                
  33.                 // Panning
  34.                 if(dist >= 0.1f){
  35.                 transform.position += startpoint-endpoint;
  36.                 Vector3 pos = transform.position;
  37.                 pos.x =Mathf.Clamp(pos.x, minPosX, maxPosX);
  38. pos.z =Mathf.Clamp(pos.z, minPosZ, maxPosZ);
  39.                                         transform.position = pos;
  40. }
  41. }
  42.  
  43.        
  44.         if(Input.GetMouseButtonUp(0)) // release button, stop pan
  45.         {
  46.                 panning = false;
  47.         }
  48.         }
  49. if (MouseUse == true)
  50. {
  51. if(Input.GetMouseButtonDown(0))
  52.         {
  53.                 // make ray
  54.                 startpoint = ray1.GetPoint (10) ; // Returns a point at distance units along the ray
  55.                 startpoint.y = 0; // fix z to 0
  56.                 panning = true;
  57.         }
  58.  
  59.         // then left mousebutton is released
  60.         if(panning){
  61.         dist = Mathf.Clamp((Vector3.Distance(endpoint, startpoint)),0,1.0f);
  62.                 endpoint = ray2.GetPoint (10) ; // Returns a point at distance units along the ray
  63.                 endpoint.y = 0; // fix z, somehow its not always 0?
  64.                
  65.                 // Panning
  66.                 if(dist >= 0.1f){
  67.                 transform.position+=startpoint-endpoint;
  68.                 Vector3 pos = transform.position;
  69.                 pos.x =Mathf.Clamp(pos.x, minPosX, maxPosX);
  70. pos.z =Mathf.Clamp(pos.z, minPosZ, maxPosZ);
  71.                                         transform.position = pos;
  72. }
  73. }
  74.  
  75.        
  76.         if(Input.GetMouseButtonUp(0)) // release button, stop pan
  77.         {
  78.                 panning = false;
  79.         }
  80. }
  81. }
I want that script somehow to know "mouse clicked (or at least is over) ngui, so don't pan"
Now it checks in Update function that mouse is clicked with these lines:
  1. if (Input.touchCount == 1) {
  2.  
  3.         // starts here, left mousebutton is pressed down
  4.         if(Input.GetMouseButtonDown(0))

Is there a way in ngui to check if mouse is clicked? Or detect that mouse is over/clicked ngui?
It's hard for me mostly because of that it must be in update (or there's a better way?)
« Last Edit: July 11, 2014, 03:12:48 PM by Lumind »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Prevent mouse panning over ngui (or check if mouse clicked ngui)
« Reply #1 on: July 11, 2014, 09:18:47 PM »
1. Don't use Input.Anything. Rely on NGUI events, and they will be handled automatically for you.
2. Set UICamera.fallThrough to an object you want handling events that don't affect the UI or any other collider. Set UICamera.genericEventHandler to an object you want to receive a copy of all the events.
3. For the sake of this example, I am going to assume that you set the genericEventHander and attached this script to it:
  1. using UnityEngine;
  2.  
  3. public class MyListener : MonoBehaviour
  4. {
  5.     void OnPress (bool isPressed)
  6.     {
  7.         if (isPressed) Debug.Log("Touch pressed on " + (UICamera.currentTouch.pressed != null ? UICamera.currentTouch.pressed.name : "<null>");
  8.         else Debug.Log("Touch released on " + (UICamera.currentTouch.pressed != null ? UICamera.currentTouch.pressed.name : "<null>");
  9.     }
  10. }
There you go, you now have a script that tells you when you pressed and released on something using purely NGUI's events. You can now easily add your existing code here, excluding Input.Get and Input.mousePosition functionality completely. UICamera.hoveredObject will be 'null' when you are not hovering over an NGUI event handling collider, and UICamera.lastTouchPosition will tell you what the position of the last touch was on the screen. Likewise, UICamera.lastWorldPosition will tell you the same thing, but in your 3D world (assuming this event was handled by the UICamera attached to your main camera).

For more info, check UICamera.currentTouch. It has all kinds of useful data in there. Check UICamera itself while you're at it. There are quite a few parameters. Look for UICamera.current#### and UICamera.last####.

Lumind

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Prevent mouse panning over ngui (or check if mouse clicked ngui)
« Reply #2 on: July 12, 2014, 08:27:06 AM »
thanks, I'm trying this now. But UICamera.hoveredObject is not "null" even when I hover over Unity game object that have colliders.
And it sets the same as genericEventHandler (Main Camera in this case)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Prevent mouse panning over ngui (or check if mouse clicked ngui)
« Reply #3 on: July 12, 2014, 04:32:47 PM »
Ah yes, UICamera will set it to 'genericEventHandler' if nothing else. Line 1270 of UICamera.cs. Well, check for that instead of a null then.