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

时间:2018-08-16 16:45:48

标签: javascript c# unity3d unity-webgl

我有一个嵌入在React应用程序中的Unity WebGL播放器。 React应用程序具有可拖放的拼贴块,可以拖放到WebGL播放器上。当瓷砖开始成为药物统一体时,开始进行光线投射,以便您可以知道屏幕上将要放下的对象。所有这些在使用鼠标时都可以正常工作,但是我注意到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发送消息以开始光线投射;我得到的atouch.position恰好在我触摸的位置,并且不会用手指移动...该死吗?

2 个答案:

答案 0 :(得分:0)

如果您具有Unity 5+版本,则可以对所有内容使用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;

答案 1 :(得分:0)

我在Unity forums上发布了一个解决方案,以防将来对该线程进行任何更新。

在WebGL中,Unity Input类不会注册始于WebGL播放器之外的触摸事件。为了解决这个问题,我使用了两个布尔值,这些值由我的React组件通过GameInstance.SendMessage方法进行切换;以及用于存储从React发送的值(也通过SendMessage的Vector3)。这是重要的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;
}