【问题标题】:Unity OnMouseDown too slowunity OnMouseDown 太慢
【发布时间】:2021-04-07 09:06:34
【问题描述】:

我正在编写一个答题器游戏,我不希望我的程序在我按下 UI 时进行计算。我尝试过使用 OnMouseDown、OnPointerDown 和 !EventSystem.current.IsPointerOverGameObject(),但它们都破坏了点击器的感觉,因为你不能点击垃圾邮件。有没有更短的替代品?提前谢谢!

编辑:我已经完成了您评论的所有事情,并且发现了问题。每当我单击鼠标按钮时,文本都会显示我每次点击获得的硬币数量。因此,它被视为一个 UI 元素,因此无法进行垃圾点击。我现在已经关闭了文本的“Raycast Target”,现在它可以正常工作了。感谢您的帮助!

【问题讨论】:

  • Input.GetButtonDown() in fixedupdate
  • @John 的答案可能是最好的。其他想法:也许你应该跟踪每个对象的“最后点击时间”,并为这种情况定义一个替代的、更轻的行为?还要考虑在每次点击时打开协程。

标签: c# unity3d user-interface


【解决方案1】:

您可以将 Input 类与检查一起使用:

void Update()
    {
        // Check if the left mouse button was clicked
        if (Input.GetMouseButtonDown(0))
        {
            // Check if the mouse was clicked over a UI element
            if (EventSystem.current.IsPointerOverGameObject())
            {
                Debug.Log("Clicked on the UI");
            }
        }
    }

或者您可以在要检测点击但通过 UI 检查的特定对象上使用 OnMouseDown: 无效 OnMouseDown() {

        // Check if the mouse was clicked over a UI element
        if (EventSystem.current.IsPointerOverGameObject())
        {
            Debug.Log("Clicked on the UI");
        }
   
}

【讨论】:

    猜你喜欢
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    相关资源
    最近更新 更多