Skip to content

Instantly share code, notes, and snippets.

@ialex32x
Last active December 16, 2015 18:18
Show Gist options
  • Save ialex32x/5476216 to your computer and use it in GitHub Desktop.
Save ialex32x/5476216 to your computer and use it in GitHub Desktop.
detect zoom in/out by 2 touches.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
public int lastTouchCount;
public float startTouchSpace;
public float thisTouchSpace;
public bool isControl;
public bool isZoomIn;
public float zoomValue;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.touchCount == 2)
{
isControl = true;
var t1 = Input.GetTouch(0);
var t2 = Input.GetTouch(1);
thisTouchSpace = (t1.position - t2.position).magnitude;
if (Input.touchCount != lastTouchCount)
startTouchSpace = thisTouchSpace;
var zoomValue_t = thisTouchSpace - startTouchSpace;
if (Mathf.Abs(zoomValue_t - zoomValue) > 3f)
{
zoomValue = zoomValue_t;
}
isZoomIn = zoomValue < 0f;
}
else
{
isControl = false;
}
lastTouchCount = Input.touchCount;
}
void OnGUI()
{
if (isControl)
{
GUILayout.Label(isZoomIn ? "zoom in" : "zoom out");
GUILayout.Label("value:" + zoomValue);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment