unity3d - 统一处理鼠标点击的正确方法是什么?
问题描述
我正在检查 Input.GetMouseButtonDown(0) 是否然后打印到 Debug.log 但因为 update() 函数每帧运行一次,所以输出发生了很多次。处理此问题的最佳方法是什么,使其不会立即运行多次?
解决方案
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.
推荐阅读
- javascript - react js中onClick音频的循环进度条
- sql - ORA-00904:: 尝试创建表时标识符无效,尽管未使用保留关键字
- go - Go get all dependencies 返回 Terratest 无法识别的导入路径
- node.js - 如何使用node.js根据mongodb中的条件更新数组内的对象
- c# - 如何更改命名空间的前缀?
- python - discord.ext.commands.errors.CommandInvokeError:命令引发异常:AttributeError:'NoneType'对象没有属性'id'
- python - 为什么这个构造函数不允许这个函数打印 hello world?
- clickhouse - Clickhouse uniqExact 聚合函数无法正常工作
- mongodb - 如何解决mongo收集1000,000条记录加入elasticsearch索引200,000,000条记录?
- c# - 如何使用字符串的值作为对象的名称?