Unity触摸屏

来源:互联网 发布:德国工业发展数据分析 编辑:程序博客网 时间:2024/04/28 21:16
Unity 触屏操作

当将Unity游戏运行到iOS或Android设备上时,桌面系统的鼠标左键可以自动变为手机屏幕上的触屏操作,但如多点触屏等操作却是无法利用鼠标操作进行的。Unity的Input类中不仅包含桌面系统的各种输入功能,也包含了针对移动设备触屏操作的各种功能,下面介绍一下Input类在触碰操作上的使用。

首先介绍一下Input.touches结构,这是一个触摸数组,每个记录代表着手指在屏幕上的触碰状态。每个手指触控都是通过Input.touches来描述的:

fingerId

触摸的唯一索引

position

触摸屏幕的位置

deltatime

从最后状态到目前状态所经过的时间

tapCount

点击数。Andorid设备不对点击计数,这个方法总是返回1

deltaPosition

自最后一帧所改变的屏幕位置

phase

相位,也即屏幕操作状态

其中phase(状态)有以下这几种:

Began

手指刚刚触摸屏幕

Moved

手指在屏幕上移动

Stationary

手指触摸屏幕,但自最后一阵没有移动

Ended

手指离开屏幕

Canceled

系统取消触控跟踪,原因如把设备放在脸上或同时超过5个触摸点

 

下面通过一段代码来进行移动设备触摸操作的实现:

using UnityEngine;using System.Collections;public class AndroidTouch : MonoBehaviour {    private int isforward;//标记摄像机的移动方向    //记录两个手指的旧位置    private Vector2 oposition1=new Vector2();    private Vector2 oposition2=new Vector2();    Vector2 m_screenPos = new Vector2(); //记录手指触碰的位置    //用于判断是否放大    bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)    {        //函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势        float leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));        float leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));        if (leng1 < leng2)    //放大手势         {             return true;        }        else           //缩小手势        {           return  false;        }    }    void Start()    {        Input.multiTouchEnabled = true; //开启多点触碰    }    void Update()    {        if (Input.touchCount <= 0)              return;        if (Input.touchCount == 1)    //单点触碰移动摄像机        {            if (Input.touches[0].phase == TouchPhase.Began)                m_screenPos = Input.touches[0].position;   //记录手指刚触碰的位置            if (Input.touches[0].phase == TouchPhase.Moved) //手指在屏幕上移动,移动摄像机            {            transform.Translate(new Vector3( Input.touches[0].deltaPosition.x * Time.deltaTime,    
                                             Input.touches[0].deltaPosition.y * Time.deltaTime, 0));            }        }        else if (Input.touchCount > 1)//多点触碰        {            //记录两个手指的位置            Vector2 nposition1 = new Vector2();            Vector2 nposition2 = new Vector2();            //记录手指的每帧移动距离            Vector2 deltaDis1 = new Vector2();            Vector2 deltaDis2 = new Vector2();            for (int i = 0; i < 2; i++)            {                Touch touch = Input.touches[i];                if (touch.phase == TouchPhase.Ended)                    break;                if (touch.phase == TouchPhase.Moved) //手指在移动                {                     if (i == 0)                    {                        nposition1 = touch.position;                        deltaDis1 = touch.deltaPosition;                    }                    else                    {                        nposition2 = touch.position;                        deltaDis2 = touch.deltaPosition;       if (isEnlarge(oposition1, oposition2, nposition1, nposition2)) //判断手势伸缩从而进行摄像机前后移动参数缩放效果                            isforward = 1;                   else                            isforward = -1;                    }                    //记录旧的触摸位置                    oposition1 = nposition1;                    oposition2 = nposition2;                }                //移动摄像机                Camera.main.transform.Translate(isforward*Vector3.forward * Time.deltaTime*(Mathf.Abs(deltaDis2.x+deltaDis1.x)+Mathf.Abs(deltaDis1.y+deltaDis2.y)));            }          }    }}

将这个脚本绑定在主摄像机上,发现单触摸操作可上下左右移动摄像机,双触摸操作可以缩放。

导出Android 在手机上运行,可以发现触摸起了效果。




====================================================

一: 下面先说经常用的三个事件  手指按下、手指移动、手指松开1.  手指按下if(input.touchCount==1){   if(input.touches[0].phase==TouchPhase.Beagn)   {          // 手指按下时,要触发的代码   }​​​​​​​2.  手指在屏幕上滑动if(input.touchCount==1){       if(input.touches[0].phase==TouchPhase.Move)       {                    // 手指滑动时,要触发的代码           float s01=Input.getAxis("Mouse X");    //手指水平移动的距离          float s02=Input.getAxis("Mouse Y");    //手指垂直移动的距离     }​​​​​​​3.  手指在屏幕上松开时​   if(input.touches[0].phase==TouchPhase.Ended)&&                        Input.touches[0].phase!=TouchPhase.Canceled  ​​二: 上面介绍的是单手指触发事件,下面介绍的是多手指触发事件if(touchCount==2)   //代表有两个手指{   if(Input.getTouch(0).phase==TouchPhase.Moved&&    //第一个手指                  Input.getTouch(1).phase==TouchPhase.Moved)            //第二个手指    {          vecter3 s1=input.getTouch(0).position;         //第一个手指屏幕坐标          vecter3 s2=input.getTouch(1).position;         //第二个手指屏幕坐标          newdis=Vecter2.distance(s1,s2);          if(newdis>olddis)             //手势外拉          {                 distance+=Time.deltaTime*50f;          }          if(newdis          {                distance-=Time.deltaTime*50f;          }         olddis=newdis;    }}​​​​​​​​​​​​​​总结:1.  不管是触屏事件还是PC端的事件,世界转屏幕还是屏幕转世界以及射线检测都是管用的2.  安卓端的手指坐标(Input.touches[0].position)等同于PC端的鼠标屏幕坐标(Input.mousePosition)​FR:徐海涛(Hunk Xu)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

另附一些简单的鼠标与触摸屏通用代码:


[csharp] view plain copy
 print?
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class JFTouch  {  
  5.       
  6.     public static bool TouchBegin()  
  7.     {  
  8.         if(Input.GetMouseButtonDown(0))  
  9.         {  
  10.             return true;  
  11.         }  
  12.         else if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)  
  13.         {  
  14.             return true;  
  15.         }  
  16.         return false;  
  17.     }  
  18.       
  19.     public static bool TouchEnd()  
  20.     {  
  21.         if(Input.GetMouseButtonUp(0))  
  22.         {  
  23.             return true;  
  24.         }  
  25.         else if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)  
  26.         {  
  27.             return true;  
  28.         }  
  29.         return false;  
  30.           
  31.     }  
  32.       
  33.     public static bool TouchIng()  
  34.     {  
  35.         if(Input.GetMouseButton(0))  
  36.         {  
  37.             return true;  
  38.         }  
  39.         else if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)  
  40.         {  
  41.             return true;  
  42.         }  
  43.         return false;  
  44.     }  
  45.       
  46. }  


0 0
原创粉丝点击