Undesirable call of Toggle's "On Value Changed" when "isOn" is changed through script
I'm saving the user's value of "isOn" for various toggle states, and then setting those toggle states during start.
Each toggle has a method I want called when the user changes the value of the toggle, but not when the value is changed via script. However, it seems as though the "On Value Changed" event gets called when I change the value through script.
Here is a sample of the code that gets called in start to set the toggle
private void Start()
{
if (PlayerPrefs.HasKey("rememberAuth"))
{
rememberAuth = PlayerPrefsX.GetBool("rememberAuth");
rememberToggle.isOn = rememberAuth;
}
else
{
PlayerPrefsX.SetBool("rememberAuth", rememberAuth);
}
}
And here is a sample of the method that gets called by the "on value changed" event.
public void RememberMeToggle()
{
if(rememberToggle.isOn)
{
SaveAuth();
}
else if (!rememberToggle.isOn)
{
DeleteAuth();
}
PlayerPrefsX.SetBool("rememberAuth", rememberAuth);
PlayerPrefs.Save();
}
Now, if the bool value of the toggle is different from the default value that was set in the editor, the RememberMeToggle() method gets called even though I don't want it to. This can cause some issues.
I've found a solution for this, and that is to remove the method call from the "On Value Changed" Event, in the inspector and then add an "Event Trigger" Component with a "Pointer Click" event and use that instead.
Is this what I'm supposed to do? Is there a better option? Is the function of "On Value Changed" being trigger from the script desirable and intended?
I had a lot of trouble finding a solution through searches, so if I am doing this right, even though I answered my own question, it might help someone else who's searching.
Thanks for posting this question - you were right, someone else was indeed searching!
Answer by Hellium · Jun 19, 2018 at 05:05 PM
Here are two useful functions you can call to "turn off" and "turn on" the persistent listeners of a UnityEvent (such as toggle.onValueChanged
)
public void Mute( UnityEngine.Events.UnityEventBase ev )
{
int count = ev.GetPersistentEventCount();
for ( int i = 0 ; i < count ; i++ )
{
ev.SetPersistentListenerState( i, UnityEngine.Events.UnityEventCallState.Off );
}
}
public void Unmute( UnityEngine.Events.UnityEventBase ev )
{
int count = ev.GetPersistentEventCount();
for ( int i = 0 ; i < count ; i++ )
{
ev.SetPersistentListenerState( i, UnityEngine.Events.UnityEventCallState.RuntimeOnly );
}
}
In your code:
private void Start()
{
if (PlayerPrefs.HasKey("rememberAuth"))
{
rememberAuth = PlayerPrefsX.GetBool("rememberAuth");
Mute( rememberToggle.onValueChanged ) ;
rememberToggle.isOn = rememberAuth;
Unmute( rememberToggle.onValueChanged ) ;
}
else
{
PlayerPrefsX.SetBool("rememberAuth", rememberAuth);
}
}
What are the for loops for in the mute/unmute methods?
The for
loop retrieve each callback specified in the event, in the inspector (the persistent listener)
If the callbacks are attached using code, you will have to call toggle.onValueChanged.RemoveListener( function )
ins$$anonymous$$d
Oh right. Of course. I forgot about adding multiple callbacks on the event. Thank you.
Thanks for the answer! Any other solutions since June 2018?
Doesn't the code works? I think the code is still valid today.
You can use Toggle.SetIsOnWithoutNotify if you want set isOn without invoking onValueChanged callback.
Answer by Speedomon · Apr 11 at 05:03 PM
Fortunately there is an easier way now: SetIsOnWithoutNotify
Your answer
Follow this Question
Related Questions
How do I save the state of a toggle button? 1 Answer
Canvas Renderer event 0 Answers
Why can't spam click on UI Image ? 0 Answers
Disabling the AudioListener works in Editor but not in Build. 2 Answers
Issues with fullscreen toggle 0 Answers