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;

}

时间: 2024-08-25 11:16:19

unity判断触摸的相关文章

Unity 手指触摸的方向(单手)

最近写了一个跑酷游戏,总结下里面的知识点:O(∩_∩)O~ using UnityEngine; using System.Collections; public class Demo : MonoBehaviour { public Vector3 lastMonseDown; /// <summary> /// 判断手指触摸的方向 /// </summary> /// <returns></returns> TouchDir GetTouchDir()

unity, 判断可见性

如果一个物体被unity判定为”退一万步讲也一定不可见“,则unity不会去渲染它,但是如果我们给此物体添加了逻辑,这个逻辑仍然会被执行. 因此,假如这个逻辑是巨耗性能的逻辑,比如说”mesh的每个顶点都按sin(t)波动“之类,那么我们需要手动根据可见性去优化这个逻辑,比如不可见就停了它,或者不可见时运行一个此逻辑的极简版等. 所以需要能获得物体的可见性. 方法1: MeshRender.isVisible; 如果MeshRender.isVisible==true,说明unity认为这个me

Unity判断鼠标是否在UI(UGUI)上

"EventSystem.current.IsPointerOverGameObject()" UI和3D场景同时都需要响应触摸事件,如果同时响应可能就会出现触摸UI的时候影响到了3D部分.为了解决这个问题在判断3D响应之前要先判断手指是否点击在UI上. 1 #if UNITY_ANDROID && !UNITY_EDITOR 2 #define ANDROID 3 #endif 4 5 6 #if UNITY_IPHONE && !UNITY_EDIT

unity 判断平台(安卓,iOS还是编辑器)

两种方式 --------------- C预处理器编译判断 --------------- #if UNITY_IOS // ... iOS项目才会编译 #elif UNITY_ANDROID // ... apk 或 iOS项目才会编译 #elif UNITY_EDITOR // ... UNITY调试时候才编译 #endif --------------- 代码运行时判断 --------------- if(Application.platform == RuntimePlatform.

移动端判断触摸的方向

最近做微信端页面,于是趁周末梳理了下移动端的事件这一块. 通过判断手指在屏幕上移动的位置减去手指放在屏幕上时的位置,就可以得出是往什么方向滑动: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css">

unity判断是否点击了UI界面

1.判断是否点击 UGUI界面: using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems; public class UiClickTest : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Up

Unity判断网络连接类型

使用NetworkReachability判断手机游戏当前的网络连接类型,是wifi还是234G 1 using UnityEngine; 2 using System.Collections; 3 4 public class Test : MonoBehaviour { 5 6 string str; 7 // Use this for initialization 8 void Start () { 9 if (Application.internetReachability == Net

Unity3D判断触摸方向

据说 Temple Run(神庙逃亡) 就是用这种方式操作的 废话不多说 直接上代码 using UnityEngine; using System.Collections; public class Touch : MonoBehaviour { private Vector2 beginPos; // Use this for initialization void Start() { } // Update is called once per frame void Update() {

Unity判断平台

方式一:Application.platform 不管当前在BuildSetting上选择的是什么平台,Windows平台下,一定会返回RuntimePlatfrom.WindowEditor,即返回的是当前真实的运行环境 方式二:#if UNITY_X 如果是Eidtor状态下,BuildSetting上选择的是Android平台,那么UNITY_EDITOR和UNITY_ANDROID都会被编译