Toggle returns the current state of the button - either the same state passed in value or the new value as changed by the user. So a better pattern would be...
// TODO: Initialize these with GUIContent
private GUIContent _toggleButtonDepressedLabel;
private GUIContent _toggleButtonDefaultLabel;
// Current toggle state
bool _toggleButtonState;
void DisplayToggle()
{
var image = _toggleButtonValue
? _toggleButtonDepressedLabel
: _toggleButtonDefaultLabel;
var newState = EditorGUILayout.Toggle(image, _toggleButtonState);
if (newState != _toggleButtonState)
{
_toggleButtonState = newState;
OnToggleButtonChanged();
}
}
void OnGUI ()
{
DisplayToggle();
}
void OnToggleButtonChanged()
{
// Do stuff.
}
If you absolutely must have toggle buttons like that, youYou can use the same contentstate-dependent swapping pattern for GUIStyles.
private GUIStyle _toggleButtonDepressedStyle;
private GUIStyle _toggleButtonDefaultStyle;
// ...
var image = _toggleButtonValue
? _toggleButtonDepressedLabel
: _toggleButtonDefaultLabel;
var style = _toggleButtonValue
? _toggleButtonDepressedStyle
: _toggleButtonDefaultStyle;
var newState = EditorGUILayout.Toggle(image, _toggleButtonState, style);