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

10 篇文章 0 订阅
  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 frame
	void 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);//主要通过控制主摄像的远近来放大缩小

                }
            }


        }
    }

}



  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值