首页 > 解决方案 > 统一处理鼠标点击的正确方法是什么?

问题描述

我正在检查 Input.GetMouseButtonDown(0) 是否然后打印到 Debug.log 但因为 update() 函数每帧运行一次,所以输出发生了很多次。处理此问题的最佳方法是什么,使其不会立即运行多次?

标签: unity3d

解决方案


Input.GetMouseButtonDown(0)仅在用户实际单击鼠标(或手指)向下的帧上返回 true;如果您看到日志多次运行,那是因为您的脚本同时运行在多个对象上。

来自 Unity 的文档:

Returns true during the frame the user pressed the given mouse button.

You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the mouse button and pressed it again. button values are 0 for the primary button (often the left button), 1 for secondary button, and 2 for the middle button.

推荐阅读