Unity3DTouch触摸基础手势
- 拖拽
- 缩放
- 旋转
完整代码如下:
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// 基础Touch手势
/// </summary>
public class BaseTouchGesture : MonoBehaviour,IPointerDownHandler,IPointerUpHandler{
#region 字段
/// <summary>
/// Touch触摸点1的位置
/// </summary>
private Vector2 touch00Postion;
/// <summary>
/// Touch触摸点2的位置
/// </summary>
private Vector2 touch01Postion;
/// <summary>
/// 是否选中
/// </summary>
private bool isSelect;
/// <summary>
/// 移动速度
/// </summary>
private float moveSpeed;
/// <summary>
/// 缩放速度
/// </summary>
private float scaleSpeed;
/// <summary>
/// 缩放阈值
/// </summary>
private float scaleThreshold;
/// <summary>
/// 最大缩放倍数
/// </summary>
private float maxScaleSize;
/// <summary>
/// 最小缩放倍数
/// </summary>
private float minScaleSize;
/// <summary>
/// 手势旋转速度
/// </summary>
private float gestureRotateSpeed;
/// <summary>
/// 手势旋转阈值
/// </summary>
private float gestureRotateThreshold;
#endregion
#region 接口实现函数
void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
{
this.isSelect = true;
}
void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
{
this.isSelect = false;
}
#endregion
void Update () {
if (Input.touchCount > 0)
{
if (this.isSelect)
{
if (Input.touchCount < 2)
{
this.transform.position = Vector3.Lerp(this.transform.position,Input.touches[0].position,Time.deltaTime * this.moveSpeed);
Debug.Log("移动");
}
else if (Input.touchCount < 3)
{
#region 初始化位置
if (this.touch00Postion == Vector2.zero && this.touch01Postion == Vector2.zero)
{
this.touch00Postion = Input.touches[0].position;
this.touch01Postion = Input.touches[1].position;
return;
}
#endregion
#region 缩放
//上一帧距离
float lastDistance = Vector2.Distance(this.touch00Postion, this.touch01Postion);
//当前帧距离
float currentDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
//差值
float difference = lastDistance - currentDistance;
if (difference < -this.scaleThreshold)
{
this.transform.localScale = Vector3.Lerp(this.transform.localScale, Vector3.one * this.maxScaleSize, Time.deltaTime * this.scaleSpeed);
if (Vector3.Distance(this.transform.localScale, Vector3.one * this.maxScaleSize) <= 0.01f)
this.transform.localScale = Vector3.one * this.maxScaleSize;
Debug.Log("放大");
}
else if (difference > this.scaleThreshold)
{
this.transform.localScale = Vector3.Lerp(this.transform.localScale, Vector3.one * this.minScaleSize, Time.deltaTime * this.scaleSpeed);
if (Vector3.Distance(this.transform.localScale, Vector3.one * this.minScaleSize) <= 0.01f)
this.transform.localScale = Vector3.one * this.minScaleSize;
Debug.Log("缩小");
}
#endregion
#region 旋转
//上一帧方向
Vector2 lastDir = (this.touch00Postion - this.touch01Postion).normalized;
//当前帧方向
Vector2 currentDir = (Input.touches[0].position - Input.touches[1].position).normalized;
//角度差
float angle = this.VectorAngle(lastDir,currentDir);
if (angle < -this.gestureRotateThreshold)
this.transform.Rotate(Vector3.forward, Time.deltaTime * this.gestureRotateSpeed);
else if (angle > this.gestureRotateThreshold)
this.transform.Rotate(Vector3.forward, -Time.deltaTime * this.gestureRotateSpeed);
#endregion
#region 更新位置
this.touch00Postion = Input.touches[0].position;
this.touch01Postion = Input.touches[1].position;
#endregion
}
}
}
}
/// <summary>
/// 计算两个向量之间的夹角(0-360)
/// </summary>
/// <param name="from"></param>
/// <param name="to"></param>
/// <returns></returns>
public float VectorAngle(Vector2 from, Vector2 to)
{
float angle;
Vector3 cross = Vector3.Cross(from, to);
angle = Vector2.Angle(from, to);
return cross.z > 0 ? -angle : angle;
}
}
ps:新手写博客,如果有什么说错的地方,欢迎大佬指正,不胜感激!