unity 单指双指事件(单指点击移动,双指滑动拖放)

  1 using System.Collections;
  2 using System.Collections.Generic;
  3 using UnityEngine;
  4 
  5 public class TouchControl : MonoBehaviour {
  6 
  7     private Vector3 startFingerPos;
  8     private Vector3 endFingerPos;
  9     private float xMoveDistance;
 10     private float yMoveDistance;
 11     private int backValue = 0;
 12 
 13     private Vector3 target;
 14     private Vector3 offSet;
 15     private bool isOver = true;
 16 
 17     void FingerRotation()
 18     {
 19 
 20         xMoveDistance = Mathf.Abs(endFingerPos.x - startFingerPos.x);
 21         yMoveDistance = Mathf.Abs(endFingerPos.y - startFingerPos.y);
 22 
 23         if (xMoveDistance > yMoveDistance)
 24         {
 25 
 26             if (endFingerPos.x - startFingerPos.x > 0)
 27             {
 28                 backValue = -1; //沿着X轴负方向移动  
 29             }
 30             else
 31             {
 32                 backValue = 1; //沿着X轴正方向移动  
 33             }
 34 
 35         }
 36         if (backValue == -1)
 37         {
 38             transform.Rotate(Vector3.down * Time.deltaTime * 150, Space.Self);
 39         }
 40         else if (backValue == 1)
 41         {
 42             transform.Rotate(Vector3.up * Time.deltaTime * 150, Space.Self);
 43         }
 44 
 45 
 46     }
 47 
 48     void MoveTo(Vector3 tar)
 49     {
 50         if (!isOver)
 51         {
 52             Vector3 offSet = tar - transform.position;
 53             transform.position += offSet.normalized * 5 * Time.deltaTime;
 54             if (Vector3.Distance(tar, transform.position) < 0.5f)
 55             {
 56                 isOver = true;
 57                 transform.position = tar;
 58             }
 59         }
 60 
 61     }
 62 
 63 
 64     // Use this for initialization
 65     void Start()
 66     {
 67 
 68     }
 69 
 70     // Update is called once per frame
 71     void Update()
 72     {
 73 
 74         //没有触摸  
 75         if (Input.touchCount <= 0)
 76         {
 77             //MoveTo(target);
 78         }
 79 
 80         if(Input.touchCount==1)//单指操作
 81         {
 82             Touch t1 = Input.GetTouch(0);
 83             if ( t1.phase==TouchPhase.Began)
 84             {
 85                 //1. 获取鼠标点击位置
 86                 //创建射线;从摄像机发射一条经过鼠标当前位置的射线
 87                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//Input.GetTouch(0).position);
 88                                                                             //发射射线
 89                 RaycastHit hitInfo = new RaycastHit();
 90                 if (Physics.Raycast(ray, out hitInfo))
 91                 {
 92                     //获取碰撞点的位置
 93                     if (hitInfo.collider.name == "Plane")
 94                     {
 95                         target = hitInfo.point;
 96                         target.y = 0.5f;
 97                         isOver = false;
 98                     }
 99                 }
100                 transform.LookAt(target);
101             }
102             //2. 让角色移动到目标位置
103             MoveTo(target);
104         }
105         else if(Input.touchCount>1)//多指操作
106         {
107             Touch t1 = Input.GetTouch(0);
108             Touch t2 = Input.GetTouch(1);
109             //单点触控记录初始点
110             if (t1.phase == TouchPhase.Began)
111             {
112                 startFingerPos = t2.position;
113             }
114 
115             endFingerPos = t2.position;//实时手指位置 
116 
117             if ((t1.phase == TouchPhase.Moved)&& (t2.phase == TouchPhase.Moved)) //双指滑动进行物体旋转
118             {
119                 FingerRotation();
120                 return;
121             }//物体旋转
122 
123         }
124         MoveTo(target);
125     }
126 }

Touch.position 是 一个 像素坐标(手机左下角为(0,0))

Touch.phase {Began,Moved,Stationary,Canceled,Ended} 代表 手指点击的状态

每次手指点击会在input.touches[]增加一个touch实例,记录手指的状态信息,当有一个手指退出屏幕,那么数组中的位置产生一个空缺,

当有新手指加入时候,新手指将替代刚刚退出的手指在数组中的位置。记得在手指退出屏幕时候,将手指所绑定的跟踪信息重新初始化。

Input.touchCount 触摸随之增长,一秒50次增量。

Input.GetTouch(0).phase==TouchPhase.Moved 手指滑动中最后一帧滑动的状态是运动的。

TouchPhase  触摸的几个状态。

Touch.deltaPosition 增量位置(Input.GetTouch(0).deltaPosition)最后一帧滑动的值,只返回xy轴坐标,也可用vector3(z轴为0),所以一般用vector2接收。

原文地址:https://www.cnblogs.com/zyn95186/p/7341526.html