Unity3DTouch触摸基础手势

Unity3DTouch触摸基础手势

  1. 拖拽
    拖拽
  2. 缩放
    Unity3DTouch触摸基础手势_第1张图片
  3. 旋转
    Unity3DTouch触摸基础手势_第2张图片
    完整代码如下:
using UnityEngine;
using UnityEngine.EventSystems;

/// 
/// 基础Touch手势
/// 
public class BaseTouchGesture : MonoBehaviour,IPointerDownHandler,IPointerUpHandler{

    #region 字段

    /// 
    /// Touch触摸点1的位置
    /// 
    private Vector2 touch00Postion;

    /// 
    /// Touch触摸点2的位置
    /// 
    private Vector2 touch01Postion;

    /// 
    /// 是否选中
    /// 
    private bool isSelect;

    /// 
    /// 移动速度
    /// 
    private float moveSpeed;

    /// 
    /// 缩放速度
    /// 
    private float scaleSpeed;

    /// 
    /// 缩放阈值
    /// 
    private float scaleThreshold;

    /// 
    /// 最大缩放倍数
    /// 
    private float maxScaleSize;

    /// 
    /// 最小缩放倍数
    /// 
    private float minScaleSize;

    /// 
    /// 手势旋转速度
    /// 
    private float gestureRotateSpeed;

    /// 
    /// 手势旋转阈值
    /// 
    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
                }
            }
        }
	}

    /// 
    /// 计算两个向量之间的夹角(0-360)
    /// 
    /// 
    /// 
    /// 
    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:新手写博客,如果有什么说错的地方,欢迎大佬指正,不胜感激!

你可能感兴趣的:(Unity)