执行一段代码后,Unity 5无响应 [英] Unity 5 unresponsive after a piece of code is executed

查看:451
本文介绍了执行一段代码后,Unity 5无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是团结的绝对初学者.我一直在研究UI,这是一种简单的登录表单.其中有两个Toggle用于选择性别,即男性或女性.我一直在做的是调用一种检查是否已选择雄性的方法,当按下另一个切换开关时,它将从雄性中删除该检查.

I am a absolute beginner in unity. I have been working on an UI which is a simple login form. In which I have two Toggle for selecting gender i.e male or female. What I have been doing is that calling a method which checks if a male is already selected it will remove the check from male when the other toggle is pressed.

单击雌性Toogle时,我的Unity冻结.这是我的代码.

My Unity freezes when I click the female Toogle. here is my code.

Toggle female;
Toggle male;
void Start(){
    female = GameObject.Find ("toggle_female").GetComponent<Toggle> ();
     male = GameObject.Find ("toggle_male").GetComponent<Toggle> ();
}

 public void isMale(){
    if (female.isOn)
        female.isOn = false;

    male.isOn = true;

}

public void isFemale(){
    if (male.isOn)
        male.isOn = false;

    female.isOn = true;
}

推荐答案

您正在使用 Toggle .

You are running into infinite loop with your Toggle.

当女性拨动开关更改时, onValueChanged 事件为触发.我假设您的isFemale()函数与Toggle onValueChanged 事件,因此将调用isFemale().

When the female toggle changes, the onValueChanged event is triggered. I assume that your isFemale() function is linked with the Toggle onValueChanged event, so isFemale() will be called.

当调用isFemale()时,它将执行male.isOn = false;导致 调用isMale()函数,然后将运行female.isOn = false;,导致再次调用isFemale().这是永恒的.

When isFemale() is called, it will do male.isOn = false; causing isMale() function to be called which will then run female.isOn = false; causing isFemale() to be called again. This is everlasting.

onValueChanged 事件>是通过脚本设置的.这是问题所在.

The onValueChanged event should never be fired when isOn is set from script. This is the problem.

解决方案:

您正在寻找 ToggleGroup .

1 .创建男性切换键 GameObject -> UI -> 切换,然后将其命名为男性切换.

1.Create male toggle GameObject -> UI -> Toggle then name it Male toggle.

2 .创建女性切换器 GameObject -> UI -> 切换,然后将其命名为女性切换.您可以稍后更改标签文本.

2.Create female toggle GameObject -> UI -> Toggle then name it Female toggle. You can change the label text later on.

3 .创建一个空的GameObject. GameObject -> 创建空白. 将其重命名为 ToggleGroup .

3.Create an empty GameObject. GameObject -> Create Empty. Rename it to ToggleGroup.

选择新创建的GameObject,转到 Component -> ToggleGroup .现在,您有了名为 ToggleGroup 的GameObject,并附加了ToggleGroup组件.

Select the newly created GameObject, go to Component -> ToggleGroup. Now, you have GameObject called ToggleGroup with ToggleGroup Component attached to it.

4 .选择您的男性切换,然后将 ToggleGroup 游戏对象拖到男性切换的 Group插槽中.选择您的女性切换器,然后将 ToggleGroup 游戏对象拖到女性切换器的 Group插槽

4.Select your Male toggle, then drag the ToggleGroup GameObject into Male toggle's Group slot. Select your Female toggle, then drag the ToggleGroup GameObject into Female toggle's Group slot

选择您的女性拨动,然后选择 unckeck 打开" 复选框,以确保只有一个(男性拨动 )默认为选中状态.点击播放,现在一次只能选择一个切换开关.

Select your Female toggle, then unckeck Is On checkbox to make sure that only one(Male toggle) is selected by default. Click play and now, only one toggle can be selected at a time.

这篇关于执行一段代码后,Unity 5无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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