如何使用Unity Toggle Check Function(选中和未选中)

Nur*_*lah 1 c# toggle oncheckedchanged unity-game-engine

我正在尝试切换。当我点击切换按钮(如果它处于打开状态,我是指选中的)时,它会关闭一些游戏对象;如果我点击切换按钮(如果它处于关闭状态,我是指未选中的),它会打开一些游戏对象,但是我不知道哪个功能是统一的 toggle.onselecttoggle.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);

    }
}
Run Code Online (Sandbox Code Playgroud)

Uri*_*pov 5

我会用这样的东西:

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
    }

}
Run Code Online (Sandbox Code Playgroud)