按下一次时多次检测到Unity输入 [英] Unity Input detected multiple times in a press

查看:771
本文介绍了按下一次时多次检测到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.

GetKeyGetKeyDownGetKeyUp相同.

注意:

如果您使用 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输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆