Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: ranilian on April 20, 2012, 01:28:00 PM

Title: How to pinch
Post by: ranilian on April 20, 2012, 01:28:00 PM
Hey Aren,

Is there a way to pinch to zoom in NGUI? Any built-in support for that or should we handle it on our own?

Thanks,
Rani
Title: Re: How to pinch
Post by: ArenMook on April 20, 2012, 01:49:39 PM
If you're talking about gesture recognition, FingerGestures plugin is what you're looking for. I didn't even try to re-invent that wheel. :)
Title: Re: How to pinch
Post by: cyrus104 on June 29, 2012, 05:53:27 PM
ArenMook, thanks and great product and support. We wanted to see how you would recommending integrating finger gestures with ngui. We would like to put swipe and some of the other gestures along with pinch on NGUI objects.

Thanks
Title: Re: How to pinch
Post by: ArenMook on June 29, 2012, 10:15:08 PM
I've never done it myself, but others have, so I'll wait for them to pitch in.
Title: Re: How to pinch
Post by: zazery on July 02, 2012, 06:43:19 AM
I've been using FingerGestures for non-NGUI objects but it should be pretty straightforward. They don't interfere with each other. You can use the toolbox scripts it comes with (uses SendMessage) and direct your gestures to a script that has access to the NGUI components to modify.

If you want FingerGestures to ignore touches that began by touching an NGUI object's collider just setup the global filter delegate. Here's a quick snippet to illustrate how to do this. My UI is on a layer called NGUI, which is layer 8.

  1. Camera NGUICamera;
  2.  
  3. bool FingerGesturesGlobalFilter( int fingerIndex, Vector2 position )
  4. {      
  5.         Ray ray = NGUICamera.ScreenPointToRay(new Vector3(position.x , position.y, 0));
  6.         return !Physics.Raycast(ray, 200, LayerMask.NameToLayer("NGUI"));
  7. }
  8.  
  9. void Start()
  10. {
  11.         NGUICamera = UICamera.FindCameraForLayer(8).camera; // function argument expect layer number; not mask
  12.         FingerGestures.GlobalTouchFilter = FingerGesturesGlobalFilter; // setup the delegate
  13. }
  14.  
Title: Re: How to pinch
Post by: joreldraw on July 03, 2012, 05:06:15 AM
  1. if (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
  2.        {
  3.  
  4.          curDist = Input.GetTouch(0).position - Input.GetTouch(1).position; //current distance between finger touches
  5.          prevDist = ((Input.GetTouch(0).position - Input.GetTouch(0).deltaPosition) - (Input.GetTouch(1).position - Input.GetTouch(1).deltaPosition)); //difference in previous locations using delta positions
  6.          touchDelta = curDist.magnitude - prevDist.magnitude;
  7.                
  8.                        
  9.  
  10.                  if ((touchDelta > 0)) // Out {}
  11.          
  12.                  if ((touchDelta < 0)) //In {}
  13.                
  14.                        
  15. }
Title: Re: How to pinch
Post by: kolchaud on September 17, 2012, 01:47:28 PM
Hi everyone,

I use this code in my project :

Quote
Camera NGUICamera;

bool FingerGesturesGlobalFilter( int fingerIndex, Vector2 position )
{   
    Ray ray = NGUICamera.ScreenPointToRay(new Vector3(position.x , position.y, 0));
    return !Physics.Raycast(ray, 200, LayerMask.NameToLayer("NGUI"));
}

void Start()
{
    NGUICamera = UICamera.FindCameraForLayer(8).camera; // function argument expect layer number; not mask
    FingerGestures.GlobalTouchFilter = FingerGesturesGlobalFilter; // setup the delegate
}


And FingerGesturesGlobalFilter still return true, even if I visually touch NGUI widget (on NGUI layer). Did you set something on NGUI stuff ?

Thanks by advance for the reply
Title: Re: How to pinch
Post by: Dakcenturi on December 14, 2012, 08:36:10 PM
I have a separate but related question.

If your whole screen is actually a GUI element (IE there isn't anywhere that is not a GUI element from NGUI) then this method won't work correct?

How would you get the pinch to zoom functionality from NGUI to work in such a circumstance?
Title: Re: How to pinch
Post by: jeldrez on March 26, 2013, 08:03:18 AM
Sorry for reviving this old post, but I have a doubt related to FingerGestures and in their own forum the support isn't so good as here.
Did anyone knows how to resize the input to a specific size, because now it works but in the entire screen. I want an specific gesture in a specific place and size. How I do that?
Title: Re: How to pinch
Post by: SketchWork on April 18, 2013, 04:22:48 PM
If you make sure you update to the latest version 3.0 you can use the onPinch event and see what the user is pinching on and either carry on to return without doing anything.  This way you can restrict what the user can pinch on.
Title: Re: How to pinch
Post by: lai3d on May 24, 2013, 02:49:11 AM
I've been using FingerGestures for non-NGUI objects but it should be pretty straightforward. They don't interfere with each other. You can use the toolbox scripts it comes with (uses SendMessage) and direct your gestures to a script that has access to the NGUI components to modify.

If you want FingerGestures to ignore touches that began by touching an NGUI object's collider just setup the global filter delegate. Here's a quick snippet to illustrate how to do this. My UI is on a layer called NGUI, which is layer 8.

  1. Camera NGUICamera;
  2.  
  3. bool FingerGesturesGlobalFilter( int fingerIndex, Vector2 position )
  4. {      
  5.         Ray ray = NGUICamera.ScreenPointToRay(new Vector3(position.x , position.y, 0));
  6.         return !Physics.Raycast(ray, 200, LayerMask.NameToLayer("NGUI"));
  7. }
  8.  
  9. void Start()
  10. {
  11.         NGUICamera = UICamera.FindCameraForLayer(8).camera; // function argument expect layer number; not mask
  12.         FingerGestures.GlobalTouchFilter = FingerGesturesGlobalFilter; // setup the delegate
  13. }
  14.  

it doesn't work either.
Title: Re: How to pinch
Post by: lai3d on May 24, 2013, 02:56:43 AM
   bool FingerGesturesGlobalFilter( int fingerIndex, Vector2 position )
   {   
      RaycastHit hit = new RaycastHit();
      return !UICamera.Raycast(new Vector3(position.x , position.y, 0), ref hit);
   }

Use above code. It totally works.
Title: Re: How to pinch
Post by: zeus_82 on September 26, 2016, 07:50:47 AM
TouchKit library is quite good and thorough for gesture work.
and it is free.

Can be found here: https://github.com/prime31/TouchKit