【问题标题】:How to use Unity Toggle Check Function (checked and unchecked)Unity Toggle Check 功能使用方法(勾选和不勾选)
【发布时间】:2015-07-26 10:05:38
【问题描述】:

我正在尝试切换。当我点击切换开关(如果它打开我的意思是选中),它会关闭一些游戏对象,如果我点击切换开关(如果它关闭我的意思是未选中)它会打开一些游戏对象但我不知道哪个功能统一 toggle.onselect? toggle.onValuesChanged?或其他人

public GameObject controlledObject;
public NamesGet nameController;
public Text text;
public Button button;
public Toggle toggle;
private bool deneme;
public void FixedUpdate()
{
    text.text = controlledObject.name;

    if(?????????)
    {
        controlledObject.SetActive(!controlledObject.activeSelf);

    }
}

【问题讨论】:

    标签: c# unity3d toggle oncheckedchanged


    【解决方案1】:

    我会使用这样的东西:

    toggle.onValueChanged.AddListener((value) =>
        {
            MyListener(value);
       });//Do this in Start() for example
    
    public void MyListener(bool value)
    {
     if(value)
        {
        //do the stuff when the toggle is on
        }else {
        //do the stuff when the toggle is off
        }
    
    }
    

    【讨论】:

    • 如果我使用您的代码,统一将我发送到此错误:'UnityEngine.Events.UnityEvent.AddListener(UnityEngine.Events.UnityAction)' 的最佳重载方法匹配有一些无效的参数我写了这段代码:toggle.onValueChanged.AddListener(MyListener);
    • @Nurullah 我改变了我的答案,让它工作。我最终测试了它。
    【解决方案2】:

    新的 而是使用 OnValueChanged 为什么不使用 EventTrigger - Pointer Click 所以它只会被调用一次。 与通过脚本添加监听器相同,但更快捷方便。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多