In the basic description for UIToggle, it would be great to share the best/easiest/latest way to use checkboxes to execute different scripts.
Let's take the basic options menu example. If the user checks the box, execute the TurnOn script. If the user, at some later point, unchecks the box, execute the TurnOff script. And remember from game session to game session what the user selected with the UISaved Option component.
Here's my current guess as to the best answer.
Create a method and attach it to the OnValueChange field of the UIToggle component. That method should look something like this…
public void ChangeWithTheCheckBox () {
//when the toggle is checked
if (MyToggle.current.value) {
//do this
TurnOn();
}
//when the toggle is unchecked
else {
//do this other thing
TurnOff();
}
}
I've noticed that UIToggle.value is better to use than the PlayerPref Key Name for that toggle since there's a race condition between the toggle notifying the script and the playerpref being updated.
The trouble with this approach is that the toggle value changes (sometimes) at scene startup, which would execute either TurnOn or TurnOff without user intent, which may not be desired. For example, if the checkbox was for Facebook, the first time it was checked you would want to register and login, but you would not want to re login every time you loaded the options menu.
So I'm hoping there's a best practice for this common scenario.