Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在压力机中多次检测到Unity输入_C#_Unity3d - Fatal编程技术网

C# 在压力机中多次检测到Unity输入

C# 在压力机中多次检测到Unity输入,c#,unity3d,C#,Unity3d,我有一个带有以下方法的源代码monobhavior脚本: private void Update () { if (Input.GetMouseButton(0)) { HandleInput(); } } private void HandleInput () { Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (

我有一个带有以下方法的源代码
monobhavior
脚本:

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);
    }
}
网格是透明的,我得到了预期的六边形(单元)。在编辑器中,单击单元格时会得到一条消息输出:

[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次以下内容:

[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

统一范式中有“最佳实践”吗?处理程序应该是异步的吗?处理程序是否应该忽略特定窗口内的后续调用


当我想在六边形平铺上使用一次单击作为切换时,这似乎会导致问题。

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

  • 当鼠标移动时,是否每帧都
    为真
    按下按钮直到松开。同样,它将是
    真的
    框架直到释放

  • 当 按住鼠标按钮。它将不再
    ,直到鼠标按下为止 释放并再次按下

在您的情况下,它看起来像是您需要的,它只会为true一次,并且只有在释放并再次按下时才能再次为true

同样的情况也适用于
GetKey
GetKeyDown
getkeydup

注意

如果您使用了,但仍被多次调用,请确保您的脚本未附加到其他或多个游戏对象


对于我来说,我已将Input.GetMouseButtonDown(0)放在FixedUpdate中。因此,Input.GetMouseButtonDown(0)在一次鼠标单击中多次返回true。将FixedUpdate替换为Update,它将起作用。

Lifesaver…我必须将这段旅程记录下来,这样我才能更正大约12个教程。我发现。。。尤其是关于平面,10:1的单位比和垂直于摄像机的角度。直到我弄明白这一点,我都快发疯了。等到你因为Unity中的脚本执行问题而遇到空异常错误时再这样做。这是一个罕见的问题,但我在这里看到了很多。除非你理解这个问题,否则很难修复。为什么Unity Answers没有得到Unity技术人员的任何关注?似乎是一个很好的地方,可以很快获得真正的知识。UA被Unity人员遗弃,因为它在没有他们的情况下工作。当用户免费做这项工作时,你为什么要花钱?因此,他们有几个人曾经偶尔出现在那里(他们有一颗钻石和他们的名字),还有一些CM来管理这个地方。事实上,Unity tech倾向于出现在Unity论坛上。