多点触控的方法,两边的触控互不干扰;

主要采用Input.touches的相关属性进行操作;

而采用IPointerDrag接口会造成两个drag的相互干扰;

代码如下:

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

public class TouchFunc : MonoBehaviour
{
    //左边的
    Vector3 beginDragPos_Left;
    Vector3 dragVector_Left;
    Vector3 dragVectorOne_Left;
    bool isDraging_Left;
    
    //右边的
    Vector3 beginDragPos_Right;
    Vector3 dragVector_Right;
    Vector3 dragVectorOne_Right;
    bool isDraging_Right;

    //控制的对象
    Vector3 mousePosition_Current;
    Vector3 mousePosition_Mobile;

    float mouseMoveSpeed;
    GameObject moveCenter_Mobile;
    GameObject aimCenter_Mobile;

    GameObject fireButton_Mobile;
    public static bool isDragFromFireButton;

    //某点触控
    void TouchPoint( int index)
    {
        //左边
        if (Input.GetTouch(index).position.x < Screen.width / 2)
        {
            if (Input.GetTouch(index).phase == TouchPhase.Began)
            {
                beginDragPos_Left = Input.GetTouch(index).position;
            }
            if (Input.GetTouch(index).phase == TouchPhase.Moved)
            {
                dragVector_Left += new Vector3(Input.GetTouch(index).deltaPosition.x, Input.GetTouch(index).deltaPosition.y,0);
                dragVectorOne_Left = dragVector_Left.normalized;

                isDraging_Left = true;
            }
            //右边
            if (Input.GetTouch(index).phase == TouchPhase.Ended)
            {
                dragVector_Left = Vector3.zero;
                dragVectorOne_Left = Vector3.zero;
                isDraging_Left = false;
            }
        }
        else
        {
            if (Input.GetTouch(index).phase == TouchPhase.Began)
            {
                beginDragPos_Right = Input.GetTouch(index).position;
            }
            if (Input.GetTouch(index).phase == TouchPhase.Moved)
            {
                dragVector_Right+= new Vector3(Input.GetTouch(index).deltaPosition.x, Input.GetTouch(index).deltaPosition.y, 0);
                dragVectorOne_Right = dragVector_Right.normalized;
                isDraging_Right = true;
            }
            if (Input.GetTouch(index).phase == TouchPhase.Ended)
            {
                dragVector_Right = Vector3.zero;
                dragVectorOne_Right = Vector3.zero;
                isDraging_Right = false;
            }
        }
    }

    void TouchMethod()
    {
        if (Input.touchCount == 1) TouchPoint(0);
        if (Input.touchCount == 2) { TouchPoint(0); TouchPoint(1); }
    }
 
    void DragFrom()
    {
        float width = fireButton_Mobile.GetComponent<Image>().rectTransform.rect.size.x;
        float height = fireButton_Mobile.GetComponent<Image>().rectTransform.rect.size.y;
        Vector3 pos = fireButton_Mobile.transform.position;
        if (beginDragPos_Right.x > pos.x - width / 2 && beginDragPos_Right.x < pos.x + width / 2 && beginDragPos_Right.y > pos.y - height / 2 && beginDragPos_Right.y < pos.y + height / 2)
        { isDragFromFireButton = true; }
        else { isDragFromFireButton = false; }

        if (isDraging_Right) PlayerBehavior.Instance.fireButtonDown_Mobile = true;
        else PlayerBehavior.Instance.fireButtonDown_Mobile = false;
    }
    //判断是否在拖动、修复判定bug
    void StateJudge()
    {
        if (Input.touchCount == 0)
        {
            isDraging_Left = false;
            isDraging_Right = false;
        }
    }
    void RightDrag()
    {
        //if (isDragFromFireButton) mouseMoveSpeed = 0.5f;
        //else mouseMoveSpeed = 1f;
 
        //瞄准按钮中心移动
        if (isDraging_Right)
            aimCenter_Mobile.transform.localPosition = Vector3.Lerp(aimCenter_Mobile.transform.localPosition, dragVectorOne_Right * Mathf.Clamp(dragVector_Right.magnitude, 0, 90), 0.2f);
        if (!isDraging_Right)
            aimCenter_Mobile.transform.localPosition = Vector3.Lerp(aimCenter_Mobile.transform.localPosition, Vector3.zero, 0.2f);

        //光标位置
        if (isDraging_Right)
        {
            Vector3 tempVec = new Vector3(Screen.width / 2, Screen.height / 2, 0) + dragVector_Right * mouseMoveSpeed;
            mousePosition_Mobile = new Vector3(Mathf.Clamp(tempVec.x, 0, Screen.width), Mathf.Clamp(tempVec.y, 0, Screen.height), 0);
        }
        if (!isDraging_Right)
        {
            mousePosition_Current = new Vector3(Screen.width / 2, Screen.height / 2, 0);
            mousePosition_Mobile = Vector3.Lerp(mousePosition_Mobile, mousePosition_Current,0.1f);
        }

        //把触控输入传给Player,以控制玩家转向和瞄准
        PlayerBehavior.Instance.mousePosition_Mobile = mousePosition_Mobile;
    }
    //控制运动、运动按钮的动作
    void LeftDrag()
    {
        //移动按钮中心移动
        if (isDraging_Left)
            moveCenter_Mobile.transform.localPosition = Vector3.Lerp(moveCenter_Mobile.transform.localPosition, dragVectorOne_Left * Mathf.Clamp(dragVector_Left.magnitude, 0, 90), 0.2f);
        if (!isDraging_Left)
            moveCenter_Mobile.transform.localPosition = Vector3.Lerp(moveCenter_Mobile.transform.localPosition, Vector3.zero, 0.2f);
        //数据传入,以控制玩家移动
        PlayerBehavior.Instance.moveVectorOne_Mobile = dragVectorOne_Left;
        //是否移动中的判定
        if (isDraging_Left && dragVector_Left.magnitude > 10) PlayerBehavior.Instance.isMoving_Mobile = true;
        else PlayerBehavior.Instance.isMoving_Mobile = false;

    }

    // Start is called before the first frame update
    void Start()
    {
        moveCenter_Mobile = GameObject.Find("MoveCenter_Mobile");
        aimCenter_Mobile = GameObject.Find("AimCenter_Mobile");
        mousePosition_Mobile = new Vector3(Screen.width / 2, Screen.height / 2, 0);
        fireButton_Mobile = GameObject.Find("FireButton_Mobile");
        //光标移动速度
        mouseMoveSpeed = 1f;

    }

    // Update is called once per frame
    void Update()
    {
        if (!GameManager.Instance.isInputMobileShow) return;
        if (PlayerBehavior.Instance.leveUp || (!PlayerBehavior.Instance.isAlive)) return;
        TouchMethod();

        StateJudge();
        RightDrag();
        LeftDrag();    }
    
}