一:滑动屏幕在固定轴上移动物体
using UnityEngine;
public class Test : MonoBehaviour
{
public float speed;
private void JudgeGesture()
{
if (Input.touchCount == 1)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
{
Vector2 touchDeltaPosition = touch.deltaPosition;
transform.Translate(touchDeltaPosition.x * speed, 0, 0);
}
}
}
}
二:点击屏幕
using UnityEngine;
public class Test : MonoBehaviour
{
private void JudgeGesture()
{
if (Input.touchCount == 1)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
}
}
if (Input.touchCount == 1)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began && touch.tapCount == 2)
{
}
}
}
}
三:长按屏幕
using UnityEngine;
public class Test : MonoBehaviour
{
private bool newTouch;
private float touchTime;
public float timeval;
private void JudgeGesture()
{
if (Input.touchCount == 1)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
newTouch = true;
touchTime = Time.time;
}
else if (touch.phase == TouchPhase.Stationary)
{
if (newTouch && Time.time - touchTime > timeval)
{
newTouch = false;
}
}
else
{
newTouch = false;
}
}
}
}
四:旋转物体
using UnityEngine;
public class Test : MonoBehaviour
{
public float speed;
private void Update()
{
JudgeGesture();
}
private void JudgeGesture()
{
if (Input.touchCount == 1)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
{
transform.Rotate(Vector3.up * Time.deltaTime * speed * -Input.GetAxis("Mouse X"));
}
}
}
}
五:缩放物体
using UnityEngine;
public class Test : MonoBehaviour
{
private Vector3 oldPos1;
private Vector3 oldPos2;
public float deltaValue;
public float maxScale;
public float minScale;
private void JudgeGesture()
{
if (Input.touchCount == 2)
{
Touch touch0 = Input.GetTouch(0);
Touch touch1 = Input.GetTouch(1);
if (touch0.phase == TouchPhase.Moved || touch1.phase == TouchPhase.Moved)
{
Vector2 newPos1 = touch0.position;
Vector2 newPos2 = touch1.position;
if (Mathf.Abs(Vector3.Distance(newPos1, newPos2)) > Mathf.Abs(Vector3.Distance(oldPos1, oldPos2)))
{
if (transform.localScale.x > maxScale)
{
return;
}
float oldScale = transform.localScale.x;
float newScale = oldScale * deltaValue;
transform.localScale = new Vector3(newScale, newScale, newScale);
}
else if (Mathf.Abs(Vector3.Distance(newPos1, newPos2)) <
Mathf.Abs(Vector3.Distance(oldPos1, oldPos2)))
{
if (transform.localScale.x < minScale)
{
return;
}
float oldScale = transform.localScale.x;
float newScale = oldScale / deltaValue;
transform.localScale = new Vector3(newScale, newScale, newScale);
}
oldPos1 = newPos1;
oldPos2 = newPos2;
}
}
}
}