【unity】屏幕事件

发布时间:2024年01月05日

SetInputSystem屏幕上点击的点位置为(x,y)

事件
public delegate void touchevent(Vector2[] touchesPos);

public static event touchevent touchMove;移动
public static event touchevent touchDown;按下
public static event touchevent touchUp;抬起

private static Camera targetCamera;

    public static Camera TargetCamera
    {
        get
        {
            if (targetCamera== null && GameStart.Instance.Eyes.Length != 0)
            {
                targetCamera= GameStart.Instance.Eyes[0].GetComponent<Camera>();
            }

            return targetCamera;
        }
    }
public class Point
{
    public float x;
    public float y;
}

public class GestureBean
{
    public int status = -1;
    public List<Point> pointList;
}

void SetInputSystem()
{
    var phase = TouchPhase.None;
        if (GetFirstPoint(gestureBean.pointList, out Vector2 touchPos))
        {
            Vector2 vector2 = GetScreenPos(TargetCamera, touchPos);
            switch (@gestureBean.status)
            {
                case 0: //单指DOWN
                    Debug.Log("单指DOWN");
                    phase = TouchPhase.Began;
                    break;
                case 1: //1:手指UP
                    Debug.Log("手指UP");
                    phase = TouchPhase.Canceled;
                    break;
                case 2: //2:手指MOVE
                    Debug.Log("手指MOVE");
                    phase = TouchPhase.Moved;
                    break;
                case 5: //5:多指DOWN
                    Debug.Log("多指DOWN");
                    phase = TouchPhase.Began;
                    break;
            }
            if (phase == default)
                return;
            InputSystem.QueueStateEvent(currentTouch, new TouchState
            {
                phase = phase,
                position = vector2   //GetScreenPos(LeftCamera, touchPos),
            });
        }
}
   static bool GetFirstPoint(List<Point> pointList, out Vector2 pos)
    {
        if (pointList != null && pointList.Count > 0)
        {
            pos = new Vector2(pointList[0].x, pointList[0].y);
            return true;
        }

        pos = Vector2.zero;
        return false;
    }

 public static Vector2 GetScreenPos(Camera targetCamera, Vector2 mousePercent)
    {
        Vector2 temp = new Vector2(mousePercent.x, 1 - mousePercent.y);
        return targetCamera.ViewportToScreenPoint(temp);
    }

invokeTouchEvent

void InvokeTouchEvent(GestureBean gestureBean)
{
    if (gestureBean.pointList.Count == 0)
        {
            return;
        }

        Vector2[] temp = new Vector2[gestureBean.pointList.Count];
        for (int i = 0; i < temp.Length; i++)
        {
            temp[i] = GetScreenPos(TargetCamera, new Vector2(gestureBean.pointList[i].x, gestureBean.pointList[i].y));

        }

        switch (@gestureBean.status)
        {
            case 0: //单指DOWN
                touchDown?.Invoke(temp);
                break;
            case 1: //1:手指UP
                touchUp?.Invoke(temp);
                break;
            case 2: //2:手指MOVE
                touchMove?.Invoke(temp);
                break;
            case 5: //5:多指DOWN
                touchDown?.Invoke(temp);
                break;
        }
}

OnEnable&&OnDisable

private void OnEnable()
    {
        TouchEventMsg.touchDown += TouchDown;
        TouchEventMsg.touchMove += TouchMove;
        TouchEventMsg.touchUp += TouchUp;
}

 private void OnDisable()
    {
        TouchEventMsg.touchDown -= TouchDown;
        TouchEventMsg.touchMove -= TouchMove;
        TouchEventMsg.touchUp -= TouchUp;
}

TouchDown

void TouchDown(Vector2[] touchesPos)
    {
        if (touchesPos.Length > 0)
        {
            DragDown(touchesPos[0]);
        }
        
    }

 void DragDown(Vector2 pos)
    {
        Vector2 viewPort = LeftCamera.ScreenToViewportPoint(pos);
        //整体初始位置 Camera.main.
        Ray ray = LeftCamera.ViewportPointToRay(viewPort);
        //从摄像机发出到点击坐标的射线
        RaycastHit hitInfo;

        if (Physics.Raycast(ray, out hitInfo, 100,~ (1<< LayerMask.NameToLayer("ModelChild"))))
        {
            GameObject go = hitInfo.collider.gameObject;
            print("点击到模型:" + go.name);
            ...
        }
    }

TouchMove

 void TouchMove(Vector2[] touchesPos)
    {
        if (touchesPos.Length == 1)
        {
            Drags(touchesPos);
        }
        else if (touchesPos.Length == 2)
        {
            TouchCheck(touchesPos);
        }
    }
void Drags(Vector2[] touchesPos)
    {

        if (lastTouch_Single == Vector2.zero || lastTouch_Single == null)
            lastTouch_Single = touchesPos[0];
        
        Vector2 deltaPos = touchesPos[0]-lastTouch_Single;//触摸点位置,在两帧时间内的改变(即这一帧触摸点的位置,减去前一帧触摸点的位置)
        float angle = Mathf.Atan2(deltaPos.x, deltaPos.y) * 180.0f / Mathf.PI;
        Vector3 newVec = Quaternion.AngleAxis(-angle, LeftCamera.transform.forward) * LeftCamera.transform.right;
        float delta = 0.5f * Mathf.Sqrt(deltaPos.x * deltaPos.x + deltaPos.y * deltaPos.y);

        ...
        
        lastTouch_Single = touchesPos[0];
    }
void TouchCheck(Vector2[] touches)
    {
        if (lastTouch == null || lastTouch.Length!= 2)
            lastTouch = touches;

        //计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型    
        float oldDistance = Vector2.Distance(lastTouch[0], lastTouch[1]);
        float newDistance = Vector2.Distance(touches[0], touches[1]);
        //两个距离之差,为正表示放大手势, 为负表示缩小手势    
        float offset = newDistance - oldDistance;
        Debug.Log("offset:" + offset);

        MoveWithTwoFingures(touches);
        
        //LogText.text = offset.ToString();
        //放大系数  
        float scaleFactor = offset / 500f;

        Vector3 localScale = GameManager.Instance.list_cureentSelectGo[0].transform.localScale;
        Debug.Log("scaleFactor:" + scaleFactor + ";localScale" + localScale);
        Vector3 scale = new Vector3(localScale.x + scaleFactor,
            localScale.y + scaleFactor,
            localScale.z + scaleFactor);
        ScaleWithTwoFingures(scale, 0.3f);
        //记住最新的触摸点,下次使用    
        lastTouch = touches;
    }
public void MoveWithTwoFingures(Vector2[] touches)
    {
        if (touches[0].x > lastTouch[0].x && touches[1].x > lastTouch[1].x)
        {
            Vector2 deltaPos = touches[0]-lastTouch[0];//触摸点位置,在两帧时间内的改变(即这一帧触摸点的位置,减去前一帧触摸点的位置)
           ...
        }
        if (touches[0].x < lastTouch[0].x && touches[1].x < lastTouch[1].x)
        {
            Vector2 deltaPos = touches[0]-lastTouch[0];//触摸点位置,在两帧时间内的改变(即这一帧触摸点的位置,减去前一帧触摸点的位置)
            ...
        }
        if (touches[0].y > lastTouch[0].y && touches[1].y > lastTouch[1].y)
        {
            Vector2 deltaPos = touches[0]-lastTouch[0];//触摸点位置,在两帧时间内的改变(即这一帧触摸点的位置,减去前一帧触摸点的位置)
           ...
        }
        if (touches[0].y < lastTouch[0].y && touches[1].y <lastTouch[1].y)
        {
            Vector2 deltaPos = touches[0]-lastTouch[0];//触摸点位置,在两帧时间内的改变(即这一帧触摸点的位置,减去前一帧触摸点的位置)
            ...
        }
    }
    //最小缩放到 a 倍   
    public void ScaleWithTwoFingures(Vector3 scale,float a)
    {
        if (scale.x > a && scale.y > a && scale.z > a)
        {
            ...
        }
    }

TouchUp

void TouchUp(Vector2[] touchesPos)
    {
        lastTouch = null;
        lastTouch_Single = Vector2.zero;
    }

文章来源:https://blog.csdn.net/qq_35064654/article/details/135397419
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。