Unity3DTouch触摸基础手势

Unity3DTouch触摸基础手势

  1. 拖拽
    拖拽
  2. 缩放
    缩放
  3. 旋转
    旋转
    完整代码如下:
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:新手写博客,如果有什么说错的地方,欢迎大佬指正,不胜感激!

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值