如何修复Unity中的游戏内双击问题?

问题描述 投票:0回答:1

我在游戏中双击这个问题。我编写了这段代码来检查输入并返回它收到的输入类型。 在编辑器中它工作得很好,但是当我导出游戏时它总是返回双击类型。即使我只点击一次。不确定是什么导致了这个问题.. 下面是鼠标输入脚本,另一个是我在其他脚本中使用它的方法。 鼠标输入脚本:

using System.Collections;
using UnityEngine.EventSystems;

public class MouseInput : MonoBehaviour
{
    #region Variables

    private enum ClickType      { None, Single, Double }

    private static ClickType    currentClick    = ClickType.None;
    readonly float              clickdelay      = 0.25f;

    #endregion

    void OnEnable()
    {
        StartCoroutine(InputListener());
    }
    void OnDisable()
    {
        StopAllCoroutines();
    }

    public static bool SingleMouseClick()
    {
        if (currentClick == ClickType.Single)
        {
            currentClick = ClickType.None;
            return true;
        }

        return false;
    }
    public static bool DoubleMouseClick()
    {
        if (currentClick == ClickType.Double)
        {
            currentClick = ClickType.None;
            return true;
        }

        return false;
    }

    private IEnumerator InputListener()
    {
        while (enabled)
        {
            if (Input.GetMouseButtonDown(0))
            { yield return ClickEvent(); }

            yield return null;
        }
    }
    private IEnumerator ClickEvent()
    {
        if (EventSystem.current.IsPointerOverGameObject()) yield break;

        yield return new WaitForEndOfFrame();

        currentClick = ClickType.Single;
        float count = 0f;
        while (count < clickdelay)
        {
            if (Input.GetMouseButtonDown(0))
            {
                currentClick = ClickType.Double;
                yield break;
            }
            count += Time.deltaTime;
            yield return null;
        }
    }
}

用法:

if (MouseInput.SingleMouseClick())
{
    Debug.Log("Single click");
    Select(true);
}
else if (MouseInput.DoubleMouseClick())
{
    Debug.Log("Double click");
    Select(false);
}
c# unity3d input mouse double-click
1个回答
1
投票

所以在框架上Input.GetMouseButtonDown(0)评估为真,你调用ClickEvent()然后在WaitForEndOfFrame();上产生然后最终回到另一个Input.GetMouseButtonDown(0))

你的问题是WaitForEndOfFrame()等到当前帧结束:

在所有相机和GUI渲染之后,等待直到帧结束,就在屏幕上显示帧之前。

它不会等到下一帧。因此,Input API返回的所有值仍然是相同的。你想要一个yield return null而不是WaitForEndOfFrame()

© www.soinside.com 2019 - 2024. All rights reserved.