【问题标题】:Unity3d Touch input doesn't work in webglUnity3d Touch 输入在 webgl 中不起作用
【发布时间】:2022-07-18 17:29:27
【问题描述】:

我正在构建一个 webgl 构建并尝试放大/缩小,但在测试时它在手机上的 webgl 构建中不起作用。

 private void OnEnable()
    {
        controls.Enable();
    }

    private void OnDisable()
    {
        controls.Disable();
    }
    private void Start()
    {
        controls.Touch.SecondaryTouchContact.started += _ => ZoomStart();
        controls.Touch.SecondaryTouchContact.canceled += _ => ZoomEnd();
    }
    private void ZoomStart()
    {
        ZoomCoroutine = StartCoroutine(ZoomDetection());
        isTouch = true;
    }
    private void ZoomEnd()
    {
        StopCoroutine(ZoomCoroutine);
        Debug.Log("zoom end");
        isTouch = false;
    }

    IEnumerator ZoomDetection()
    {
        float previousDistance = 0f, distance = 0f;
        while (true)
        {
            isTouch = true;
            Debug.Log("zoom");

            distance = Vector2.Distance(controls.Touch.PrimaryFingerPosition.ReadValue<Vector2>(),
            controls.Touch.SecondaryFingerPostion.ReadValue<Vector2>());
            //detection
            //Zoom out

            if (distance > previousDistance)
            {
                cameraTransform.position = Vector3.MoveTowards(cameraTransform.position, targetGameObject.transform.position, -1.0f * 0.5f);


            }
            // zoom in 
            else if (distance < previousDistance)
            {
                cameraTransform.position = Vector3.MoveTowards(cameraTransform.position, targetGameObject.transform.position, 0.5f);

            }


            // keep track of previous distance for next loop
            previousDistance = distance;
            yield return null;
        }

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您必须为 Webgl 添加平台脚本符号。这是怎么做的

    #if UNITY_WEBGL
    
                "Your zoom code goes here"
    
    #endif
    

    More on platform scripting symbols.

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 2020-07-29
      相关资源
      最近更新 更多