代码之家  ›  专栏  ›  技术社区  ›  Kyle Richardson

从Unity播放器外部开始的触摸获取触摸输入

  •  0
  • Kyle Richardson  · 技术社区  · 7 年前

    我有一个Unity WebGL播放器,它嵌入在React应用程序中。React应用程序具有拖放平铺,这些平铺可以是药物,也可以放在WebGL播放器上。当瓷砖开始变成毒品时,unity开始进行光线投射,这样你就可以知道屏幕上要放置的是什么物体。所有这些在使用鼠标时都能很好地工作,但我注意到 Input.touchCount 始终返回0,除非触摸源于WebGL播放器。有人知道怎么解决这个问题吗?有一段时间我一直在打这个。。。

    这是光线投射代码。就像我说的,它对老鼠来说是完美的。。。但我找不到 touch.position 返回。

    public void LateUpdate()
    {
        if (SHOULD_CAST_RAY)
        {
            // always returning 0
            Debug.Log(Input.touchCount);
    
            RaycastHit hit;
            Vector3 position = Input.touchSupported
                && Input.touchCount == 1
                    ? new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0)
                    : Input.mousePosition;
    
            if (Physics.Raycast(RigsCamera.ScreenPointToRay(position), out hit, CameraRigControllerScript.CameraDistanceMax * 1.5f, 1 << 10))
            {
                if (CURRENT_SELECTION == null)
                {
                    CURRENT_SELECTION = UnsafeGetModelInstantiationFromRaycast(hit);
                    ApplySelectionIndication();
                }
                else if (!IsAlreadySelected(hit))
                {
                    RemoveSelectionIndication();
                    CURRENT_SELECTION = UnsafeGetModelInstantiationFromRaycast(hit);
                    ApplySelectionIndication();
                }
                return;
            }
    
            if (CURRENT_SELECTION != null)
            {
                RemoveSelectionIndication();
                CURRENT_SELECTION = null;
            }
        }
    }
    

    另外,如果我触摸Unity WebGL播放器上的屏幕,然后开始拖动我的一个React组件,该组件向Unity发送一条消息以开始光线投射;我得到的a touch.position是在我触摸的点上,并且不会用手指移动。。。该死的?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Horothenic    7 年前

    如果你有Unity5+版本,你可以对所有东西使用mousePosition,因为Input类处理Touch(0)和Mouse(0)完全相同,你试过了吗?

    Vector3 position = Input.touchSupported && Input.touchCount == 1
    ? new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0)
    : Input.mousePosition;
    

    尝试将此更改为仅

    Vector3 position = Input.mousePosition;
    
        2
  •  0
  •   Kyle Richardson    7 年前

    我在 Unity forums

    在WebGL中,统一 Input GameInstance.SendMessage 方法;以及用于存储从React发送的值的Vector3,也可以通过 SendMessage . 这是重要的c位。如果有人有任何问题,请问,我会带你走完剩下的!

    bool SHOULD_CAST_RAY;
    bool USE_EXTERNAL_ORIGINATING_TOUCH_POS;
    Vector3 EXTERNAL_ORIGINATING_TOUCH_POS;
    
    public void LateUpdate()
    {
        if (SHOULD_CAST_RAY)
        {
            if (USE_EXTERNAL_ORIGINATING_TOUCH_POS && EXTERNAL_ORIGINATING_TOUCH_POS.z < 0) { return; }
    
            RaycastHit hit;
            Vector3 screenPoint = Input.mousePresent ? Input.mousePosition : Vector3.zero;
            Vector3 viewportPoint = USE_EXTERNAL_ORIGINATING_TOUCH_POS ? RigsCamera.ScreenToViewportPoint(EXTERNAL_ORIGINATING_TOUCH_POS) : Vector3.zero;
    
            if (Physics.Raycast(
                USE_EXTERNAL_ORIGINATING_TOUCH_POS
                    ? RigsCamera.ViewportPointToRay(new Vector3(viewportPoint.x, 1 - viewportPoint.y, 0))
                    : RigsCamera.ScreenPointToRay(screenPoint),
                out hit,
                CameraRigControllerScript.CameraDistanceMax * 1.5f,
                1 << 10
            )) {
                if (CURRENT_SELECTION == null)
                {
                    CURRENT_SELECTION = UnsafeGetModelInstantiationFromRaycast(hit);
                    ApplySelectionIndication();
                }
                else if (!IsAlreadySelected(hit))
                {
                    RemoveSelectionIndication();
                    CURRENT_SELECTION = UnsafeGetModelInstantiationFromRaycast(hit);
                    ApplySelectionIndication();
                }
                return;
            }
    
            if (CURRENT_SELECTION != null)
            {
                RemoveSelectionIndication();
                CURRENT_SELECTION = null;
            }
        }
    }
    
    // The below methods are used to control the raycasting from React through sendMessage
    public void ClearExternalOriginatingTouchPosition()
    {
        EXTERNAL_ORIGINATING_TOUCH_POS = new Vector3(0, 0, -1f);
        USE_EXTERNAL_ORIGINATING_TOUCH_POS = false;
    }
    
    public void DisableRaycasting()
    {
        SHOULD_CAST_RAY = false;
        RemoveSelectionIndication();
        CURRENT_SELECTION = null;
    }
    
    public void EnableRaycasting()
    {
        SHOULD_CAST_RAY = true;
    }
    
    public void SetExternalOriginatingTouchPosition(string csv)
    {
        string[] pos = csv.Split(',');
        EXTERNAL_ORIGINATING_TOUCH_POS = new Vector3(float.Parse(pos[0]), float.Parse(pos[1]), 0);
        USE_EXTERNAL_ORIGINATING_TOUCH_POS = true;
    }