unity c#脚本_爆发! 如何在Unity C#脚本中停止无限循环

unity c#脚本

(or what I did at my first Hack Week with Unity)

(或者我在Unity的第一个Hack Week所做的事情)

这篇文章是关于打破Unity中脚本无限循环的小技巧。 它可以在Windows / 64位编辑器中使用,也可以在64位版本中启用脚本调试。 稍加努力,就可以在32位上运行,甚至可以在禁用脚本调试的情况下进行构建。 (This post is about a little trick for breaking infinite loops in scripts in Unity. It works with Windows / 64 bit in the Editor and in 64 bit builds with script debugging enabled. With a little more effort it can be made to work on 32 bit and even builds with script debugging disabled.)

Infinite loops seems to be something that should be easily avoidable. But from time to time, I’ve encountered them in sneaky variants. Once it was the broken random function returning 1.000001 on impossibly rare cases. Another time a degenerate mesh sent a NaN right into an unsuspecting while(1) { d += 1.0; if(d>10.0) break; /* .. */ } loop. And then there was the broken data structure traversed by an algorithm that assumed current = current.next; would surely lead to an end eventually.

无限循环似乎应该很容易避免。 但是我时不时地在各种变体中遇到它们。 一旦发生故障,随机函数将在极少数情况下返回1.000001。 又一次,退化的网格将 NaN 正确 发送给了 一个不知不觉的while(1) { d += 1.0; if(d>10.0) break; /* .. */ } while(1) { d += 1.0; if(d>10.0) break; /* .. */ } while(1) { d += 1.0; if(d>10.0) break; /* .. */ }循环。 然后有一个假定的算法遍历了破碎的数据结构 current = current.next; 肯定会最终结束。

If you have experienced an infinite loop in your script code in Unity, you know it is quite unpleasant. Unity becomes unresponsive and you may have to kill the Editor entirely to get out of the mess. If you were lucky enough to have the debugger attached before running your game, you may be able to break it. But usually only if you can guess the right place in the code to set a breakpoint.

如果您在Unity中的脚本代码中遇到了无限循环,那么您就会知道这很不愉快。 Unity变得React迟钝,您可能必须完全杀死编辑器才能摆脱混乱。 如果您有幸在运行游戏之前连接了调试器,则可以将其破坏。 但是通常只有在您可以猜出代码中正确的位置设置断点的情况下,才可以。

Some years back, before joining Unity, I found a way to break a script that is stuck like this. But it was not until my first Hack Week here, that I got to talk to the right people and realized why the trick works and how it may be used to create a proper way to break scripts in Unity (sneak peek of my Hack Week project). Until we get that feature properly done and release it, you can use the trick below. Or just hang on for the fun of a little tour into disassembled, jit’ed code. What could possibly go wrong?

几年前,在加入Unity之前,我找到了一种方法来破坏像这样卡住的脚本。 但是直到我第一次在这里参加“黑客周”活动时,我才开始与合适的人进行交谈,并意识到 该技巧 为何 起作用以及如何使用该技巧来在Unity中创建破坏脚本的正确方法( 偷看我的“黑客周”项目) )。 在我们正确完成该功能并将其发布之前,您可以使用以下技巧。 或者只是继续浏览一下反汇编的jit'ed代码的乐趣。 可能出什么问题了?

不要在工作中尝试这个! (Do not try this at work!)

As a trained professional you know the value of practice, so try this out on a toy project before you attempt a rescue operation at work. Fire up Unity and create an empty project, add a box to an empty scene and create a new C# script “Quicksand” attached to the box. The script should contain this code:

作为训练有素的专业人员,您知道练习的价值,因此,在尝试进行救援操作之前,请在玩具项目中进行尝试。 启动Unity并创建一个空项目,在一个空场景中添加一个框,并在框上创建一个新的C#脚本“ Quicksand”。 该脚本应包含以下代码:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值