Unity中实现触摸屏中的单点,手指滑动,多点触屏的功能

触屏操作

  当我们将项目移植到ios和android设备上,pc端的鼠标左键操作会自动适配为手机屏幕上的触屏(单点)操作,但也只是实现了鼠标的单点,比如手指滑动和多点触摸都无法检测。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchMgr : MonoBehaviour
{
    //手指第一次触摸点的位置
    Vector2 m_scenePos = new Vector2();
	//摄像机
    public Transform cameraTarget;
    void Start()
    {
        //允许多点触摸
        Input.multiTouchEnabled = true;
    }

   
    void Update()
    {
        //DesktopInput();
        //MobileInput();
    }

    //移动端控制摄像机旋转
    private void MobileInput()
    {
        if (Input.touchCount == 0)
            return;

        if (Input.touchCount==1)
        {
            if (Input.touches[0].phase== TouchPhase.Began)
            {
                m_scenePos = Input.touches[0].position;
            }
            else if (Input.touches[0].phase==TouchPhase.Moved)
            {
                //旋转摄像机
                cameraTarget.Rotate(new Vector3(-Input.touches[0].deltaPosition.y,Input.touches[0].deltaPosition.x,0), Space.Self);
            }

            if (Input.touches[0].phase==TouchPhase.Ended&&Input.touches[0].phase!= TouchPhase.Canceled)
            {
                Vector2 pos = Input.touches[0].position;

                //判断手指移动
                //水平移动
                if (Mathf.Abs(m_scenePos.x-pos.x)>Mathf.Abs(m_scenePos.y-pos.y))
                {
                    if (m_scenePos.x>pos.x)
                    {
                        Debug.Log("手指向左滑");
                        //TODO:...
                    }
                    else
                    {
                        Debug.Log("手指右滑");
                        //TODO:...
                    }
                }
                else
                {
                    if (m_scenePos.y>pos.y)
                    {
                        Debug.Log("手指下滑");
                        //TODO:...
                    }
                    else
                    {
                        Debug.Log("手指上滑");
                        //TODO:...
                    }
                }
            }
        }//多指逻辑
        else if (Input.touchCount>1)
        {
            //记录两个手指的位置
            Vector2 finger1 = new Vector2();
            Vector2 finger2 = new Vector2();

            //记录两个手指的移动
            Vector2 mov1 = new Vector2();
            Vector2 mov2 = 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;
                        mov2 = 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;
                        }
                        cameraTarget.transform.Translate(0, 0, mov * 0.1f);
                    }
                }
               
            }
        }
    }

    //Window端控制摄像机旋转
    void DesktopInput()
    {
        float mx = Input.GetAxis("Mouse X");
        float my = Input.GetAxis("Mouse Y");

        if (mx != 0 || my != 0)
        {
            if (Input.GetMouseButton(0))
            {
                cameraTarget.Rotate(new Vector3(-my*10,mx*10,0), Space.Self);
            }
        }
    }
}

  • 8
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值