Unity触屏操作 (主要是解决多点触 屏问题)

来源:互联网 发布:插件式软件开发 编辑:程序博客网 时间:2024/05/21 17:09
  1)、声明允许多点触屏
  2)判断手指触摸到屏幕的位置
  3)判断手指触摸到屏幕的数目
    input.touchCount =0
      return
    input.touchCount =1
      我们让其来做点事: (移动摄像机左右移动)
        1)用phase来判断触碰的状态
          Began:表示手指已触摸屏幕
          Move:手指在屏幕上移动
          End:手指从屏幕上移开。这是一个触摸的最后状态
          Canceled:系统取消跟踪触摸,如用户把屏幕放到 他脸上或超过五个接触同时发生。这是一个触摸 的最后状态。
          Stationary:手指触摸屏幕,但并没有移动。
        2)当判断Input.touches[0].phase ==TouchesPhase.Began
          用一个Verctor2记录下Input.touches[0].position
        3)当判断Input.touches[0].phase == TouchesPhase.Move
          此时就可用来移动主摄像机了 Camin.main.tranform.translate(new Verctor3(Input.touches[0].position.x*Time.deltaTime, Input.touches[0].position.y*Time.deltaTime,0))

    input.touchCount>1


      我们让其来做点事 移动摄像机的Z轴控制 远近,而达到物体放大 缩小的目的
        1、定义变量来储存两个touchCount的开始位置 与所移动的距离
          Vector2 finger1 = new Vector2()
      Vector2 finger2 = new Vector2()
          Vector2 mov1 = new Vector2()
          Vector2 mov2 = new Vector2()
        2、想像一个我们划动屏幕的动作 我们的两个手指放下后一般会一个 手指定住,一个手指移动来放大, 当然也会有两个手指都移动,但我 们只取这种状态来作判断即可
          在一个for循环里定义一个touch类型变量来 接收Input.touches[i]
            1、对touch.phase作判断 如果touch.phase ==touches.Ended 则break
            2对touch.phase作判断 如果touch.phase ==touches.Move
              0、定议一个floa mov 来接收最终经过判断所移动的值
                mov = move.x+move.y
              1、for循环作一个判断i==1时对finger1、mov1、赋值
              2、else里对finger2、mov2赋值,并对比较finger1与finger2的X、Y

            其实无论touchCount的数目是多少 我们都只取两个点来做判断即可


以下插入总体源码(这个与上面所讲有点不同,这个直接挂在与要旋转放大缩小的物体上)

using UnityEngine;using System.Collections;public class mobileChane : MonoBehaviour {// Use this for initialization    private float mx;    private float my;    private float xSpeed =3;    private float ySpeed =3;    private Vector2 start;    private Quaternion mRoation;void Start () {        Input.multiTouchEnabled = true;}// Update is called once per framevoid Update () {        MoblieInput();}    void MoblieInput()    {        if(Input.touchCount ==0 )        {            return;        }        if(Input.touchCount ==1)        {            if(Input.touches[0].phase ==TouchPhase.Began)            {                start = Input.touches[0].position;            }            if(Input.touches[0].phase ==TouchPhase.Moved)            {                mx += Input.touches[0].deltaPosition.x * xSpeed;                my += Input.touches[0].deltaPosition.y * ySpeed; ;            }            mRoation = Quaternion.Euler(mx, my, 0);            transform.rotation = mRoation;                    }        else if(Input.touchCount>1)        {            Vector2 finger1 = new Vector2();            Vector2 finger2 = new Vector2();            Vector2 mov1 = new Vector2();            Vector2 mov2 = new Vector2();          //  Vector2 mov = 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)                {                    float mov = 0;                    if(i==0)                    {                        finger1 = touch.position;                        mov1 = touch.deltaPosition;                    }                    else                    {                        finger2 = touch.position;                        finger2 = touch.deltaPosition;                        //开始做移动判断                        if(finger1.x>finger2.x)                        {                            mov = mov1.x;                        }                        else                        {                            mov = mov2.x;                        }                        if(finger1.y >finger2.y)                        {                            mov += mov1.y;                         }                        else                        {                            mov += mov2.y;                        }                    }                    Camera.main.transform.Translate(0, 0, mov * Time.deltaTime);//主要通过控制主摄像的远近来放大缩小                }            }        }    }}



0 0
原创粉丝点击