Unity触摸屏幕 拖拽物体 旋转物体 放大缩小物体 单击 双击 长按

Unity触摸屏幕 拖拽物体 旋转物体 放大缩小物体 单击 双击 长按

拖拽物体

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

public class PlayerMove : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }
    public float speed = 0.1f;
    void Update()
    {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            //触摸位置与上次触摸位置之间的差异
            Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
            transform.Translate(touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0);
        }
    }
}

旋转物体

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

public class PlayerRotate : MonoBehaviour
{
    private float xSpeed=150f;
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (Input.touchCount == 1)
            {
                if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                  transform.Rotate(Vector3.up*Input.GetAxis("Mouse X")*-xSpeed*Time.deltaTime,Space.World);
                }
            }

        }
    }
}

放大缩小物体

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

public class PlayerEnlarge : MonoBehaviour
{
    private Vector2 oldPos01;
    private Vector2 oldPos02;
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount == 2)
        {
            //第一个手指 或者第二个手指
            if (Input.GetTouch(0).phase == TouchPhase.Moved|| Input.GetTouch(1).phase == TouchPhase.Moved)
            {
                Vector2 temPos1 = Input.GetTouch(0).position;
                Vector2 temPos2 = Input.GetTouch(1).position;
                if (IsEnlarge(oldPos01,oldPos02,temPos1,temPos2))
                {
                    //放大
                    float oldScale = transform.localScale.x;
                    float newScale = oldScale * 1.025f;
                    transform.localScale=new Vector3(newScale,newScale,newScale);
                }
                else
                {
                    //缩小
                    float oldScale = transform.localScale.x;
                    float newScale = oldScale / 1.025f;
                    transform.localScale = new Vector3(newScale, newScale, newScale);
                }

                oldPos01 = temPos1;
                oldPos02 = temPos2;
            }
        }
    }
    //判断手势
    bool IsEnlarge(Vector2 op1, Vector2 op2, Vector2 np1, Vector2 np2)
    {
        //Mathf.Sqrt返回的平方根
        float lenth01 = Mathf.Sqrt((op1.x - op2.x) * (op1.x - op2.x) + (op1.y - op2.y) * (op1.y - op2.y));
        float lenth02 = Mathf.Sqrt((np2.x - np2.x) * (np2.x - np2.x) + (np2.y - np2.y) * (np2.y - np2.y));
        if (lenth01 < lenth02)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

单击 双击 长按

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.UIElements;

public class PlayerButton : MonoBehaviour
{
    private bool newTouch;
    private float touchTime;
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                //Input.touchCount 几个手指
                //Input.GetTouch(0).tapCount 点击次数
                //TouchPhase.Began 手指触摸了屏幕。
                //TouchPhase.Moved 手指在屏幕上
                //TouchPhase.Stationary 手指触摸屏幕但未移动
                //TouchPhase.Ended 手指从屏幕上抬起
                //TouchPhase.Canceled 系统取消了触摸的跟踪
                //单击
                if (Input.touchCount==1 && Input.GetTouch(0).phase == TouchPhase.Began)
                {
                    if (Input.GetTouch(0).tapCount == 1)
                    {
                        Debug.Log("单击");
                    }
                }
                //双击
                if (Input.GetTouch(0).tapCount == 2 && Input.GetTouch(0).phase == TouchPhase.Began)
                {
                    Debug.Log("双击");
                }
                //长按
                if (Input.GetTouch(0).tapCount == 1)
                {
                    Touch touch = Input.GetTouch(0);
                    if (touch.phase == TouchPhase.Began)
                    {
                        newTouch = true;
                        touchTime = Time.time;
                    }
                    else if (touch.phase == TouchPhase.Stationary)
                    {
                        if (newTouch && Time.time - touchTime > 1f)
                        {
                            newTouch = false;
                            Debug.Log("长按");
                        }
                    }
                    else
                    {
                        newTouch = false;
                    }
                }
            }
        }

    }
}

 

你可能感兴趣的:(C#,unity)