按下一次时多次检测到Unity输入

编程入门 行业动态 更新时间:2024-10-27 22:28:23
本文介绍了按下一次时多次检测到Unity输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有使用以下方法的源MonoBehavior脚本:

I have a source MonoBehavior script with these methods:

private void Update () { if (Input.GetMouseButton(0)) { HandleInput(); } } private void HandleInput () { Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(inputRay, out hit)) { var position = transform.InverseTransformPoint(hit.point); Coordinates coords = Coordinates.FromPosition(position); HexCell cell = null; if (!cells.TryGetValue(coords, out cell)) { var err = string.Format("{0} is an invalid position and coordinates are improperly calculated: {1}", position, coords); print(err); throw new System.ArgumentException(err); } print(string.Format("[{0}] cell: [{1}] {2}", DateTime.Now, cell.id, cell.coordinates)); OnCellTouched(cell); } }

网格是透明的,我得到了预期的六边形(单元格).在编辑器中,单击单元格时,我得到一条消息输出:

The mesh is transparent and I get the hexagon (cell) as expected. In the editor, I get a single message output when I click on a cell:

[05.31.2017.14.15.14.2836] cell: [0] [x=0;y=0;z=0] [05.31.2017.14.15.15.7359] cell: [197] [x=12;y=-22;z=10]

但是,当我构建并运行该应用程序时:单击任何单元格,每次单击将记录以下4-6次:

However, when I build and run the application: single-click on any cell, the following is logged 4 - 6 times per click:

[05.31.2017.14.57.50.1588] cell: [0] [x=0;y=0;z=0] [05.31.2017.14.57.50.1695] cell: [0] [x=0;y=0;z=0] [05.31.2017.14.57.50.1861] cell: [0] [x=0;y=0;z=0] [05.31.2017.14.57.50.2028] cell: [0] [x=0;y=0;z=0] [05.31.2017.14.57.50.2195] cell: [0] [x=0;y=0;z=0] [05.31.2017.14.57.52.6195] cell: [197] [x=12;y=-22;z=10] [05.31.2017.14.57.52.6360] cell: [197] [x=12;y=-22;z=10] [05.31.2017.14.57.52.6530] cell: [197] [x=12;y=-22;z=10] [05.31.2017.14.57.52.6691] cell: [197] [x=12;y=-22;z=10] [05.31.2017.14.57.52.6857] cell: [197] [x=12;y=-22;z=10]

是否有可能由编辑器忽略的Unity引擎多次传播一次单击?由于每个日志条目都有不同的时间戳(毫秒),因此范围差异为157-170ms.

Is it possible that a single click is propagated multiple times by the Unity engine that the editor is ignoring? Since each log entry has a different time stamp (milliseconds), the range difference being 157-170ms.

Unity范例中是否有最佳实践"?处理程序应该异步吗?处理程序是否应该忽略某个窗口内的后续调用?

Is there a "best practice" in the Unity paradigm? Should the handler be async? Should the handler ignore subsequent calls within a certain window?

当我想使用单击作为六边形图块上的切换时,这似乎会引起问题.

It seems like this would cause problems when I want to use a single click as a toggle on a hexagon tile.

推荐答案

这是几乎所有新的Unity程序员都会遇到的问题.

This is a problem almost all new Unity programmers go through.

  • Input.GetMouseButton 在每帧时为true鼠标 按住按钮直至释放.同样,它将是每个true 框架,直到发布.

  • Input.GetMouseButton is true every frame when the mouse Button is held down until released. Again, it will be true every frame until released.

Input.GetMouseButtonDown 是true只是一旦 按住鼠标按钮.直到鼠标被不再次为true. 释放并再次按下.

Input.GetMouseButtonDown is true just once when the mouse button held down. It will not be true again until mouse is released and pressed again.

在您的情况下,您似乎需要 Input.GetMouseButtonDown 只会一次为真,并且只有在释放并再次按下时才能再次为真.

In your case, it looks like you need Input.GetMouseButtonDown which will only be true once and can only be true again when released and pressed again.

GetKey,GetKeyDown和GetKeyUp相同.

注意:

如果您使用 Input.GetMouseButtonDown ,但仍被称为多个然后确保您的脚本未附加到另一个或多个GameObject.

If you use Input.GetMouseButtonDown but it is still being called multiple times then make sure that your script is not attached to another or multiple GameObjects.

更多推荐

按下一次时多次检测到Unity输入

本文发布于:2023-10-12 21:18:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485924.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按下   检测到   Unity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!