我可以“反转”吗?一个布尔?
我进行了一些检查来查看屏幕是否处于活动状态。代码如下所示:
if (GUI.Button(new Rect(Screen.width / 2 - 10, 50, 50, 30), "Rules")) //Creates a button
{
if (ruleScreenActive == true) //check if the screen is already active
ruleScreenActive = false; //handle according to that
else
ruleScreenActive = true;
}
有没有办法 - 每当我单击按钮时 - 反转 ruleScreenActive
的值?
(这是Unity3D中的C#)
I have some checks to see if a screen is active. The code looks like this:
if (GUI.Button(new Rect(Screen.width / 2 - 10, 50, 50, 30), "Rules")) //Creates a button
{
if (ruleScreenActive == true) //check if the screen is already active
ruleScreenActive = false; //handle according to that
else
ruleScreenActive = true;
}
Is there any way to - whenever I click the button - invert the value of ruleScreenActive
?
(This is C# in Unity3D)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过否定 bool 的值来摆脱 if/else 语句:
You can get rid of your if/else statements by negating the bool's value:
我认为最好这样写:
这样就可以避免将变量名写两次......这可能会导致错误
I think it is better to write:
that way you avoid writing the variable name twice ... which can lead to errors
这将被内联,因此可读性增加,运行时成本保持不变:
给出:
This would be inlined, so readability increases, runtime costs stays the same:
To give:
这对于长变量名很有用。您不必将变量名称写两次。
例子:
This can be useful for long variable names. You don't have to write the variable name twice.
Example: