<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns="http://www.w3.org/1999/xhtml">
Unity判断手势触摸的类型 ,判断手势的滑动方向,并获取刚触摸以及触摸结束事的坐标
本章咱们一起来看下unity对有触摸手势做出的响应
单点触摸
``` Input.touchCount==1 ```移动触摸
``` Input.GetTouch(0).phase==TouchPhase.Moved ```多点触摸
``` Input.touchCount>1 ```判断两只手指至少有一只为移动触摸
``` Input.GetTouch(0).phase==TouchPhase.Moved||Input.GetTouch(1).phase==TouchPhase.Moved ```
/**
* 判断是否为单点触摸
``` public static bool singleTouch() { if(Input.touchCount==1) return true; return false; } ```/**
* 判断单点触摸条件下 是否为移动触摸
``` public static bool moveSingleTouch() { if (Input.GetTouch(0).phase==TouchPhase.Moved) return true; return false; } ```
/**
*判断是否为多点触摸
``` public static bool multipointTouch() { if (Input.touchCount > 1) return true; return false; } ```
/**
*判断两只手指至少有一只为移动触摸
``` public static bool moveMultiTouch() { if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved) return true; return false; } ```
/**
*
* 新建一个公共方法用于判断手指的移动方向
* 假如是往左或者往上 则模型往各个轴的正方向位置移动 函数返回1
* 加入是往右或者往下 则模型往各个轴的负方向位置移动 函数返回-1
*
* **/
``` int judueFinger(){ if (Input.GetTouch(0).phase == TouchPhase.Began && startPosFlag == true) { //Debug.Log("======开始触摸====="); startFingerPos = Input.GetTouch(0).position; startPosFlag = false; } if (Input.GetTouch(0).phase == TouchPhase.Ended) { //Debug.Log("======释放触摸====="); startPosFlag = true; } nowFingerPos = Input.GetTouch(0).position; xMoveDistance = Mathf.Abs(nowFingerPos.x - startFingerPos.x); yMoveDistance = Mathf.Abs(nowFingerPos.y - startFingerPos.y); if (xMoveDistance>yMoveDistance) { if(nowFingerPos.x-startFingerPos.x>0){ //Debug.Log("=======沿着X轴负方向移动====="); backValue = -1; //沿着X轴负方向移动 } else { //Debug.Log("=======沿着X轴正方向移动====="); backValue = 1; //沿着X轴正方向移动 } } else { if (nowFingerPos.y - startFingerPos.y>0) { //Debug.Log("=======沿着Y轴正方向移动====="); backValue = 1; //沿着Y轴正方向移动 }else{ //Debug.Log("=======沿着Y轴负方向移动====="); backValue = -1; //沿着Y轴负方向移动 } } return backValue; } ```- 顶
- 0
- 踩
- 0
<div class="similar_c"style="margin:20px 0px 0px 0px">
相关文章推荐
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
</html>