- Home /
Get source of onValueChanged Event
Hi,
I have several Toggles.
I have a Controller class that has one OnValueChanged(bool) method. I want that all toggles call that one method and let the controller decide what to do.
Problem is that there seems to be no way to know where the OnValueChanged event is coming from.
Other UI implementations always have a OnEvent(source, value) implementation.
Do I have to create one OnValueChanged method per Toggle, or are there other, better solutions?
You can do that if you assign the function to call on a OnValueChanged event from the inspector referencing the function from you Controller class.
$$anonymous$$aybe you can use a ToggleGroup to handler your toggles? Or tell us more about what behaviour you expect or what you are trying to achieve.
(Recently I had to work with several toggle groups and change the color of the parent objects if some of the children toggles where selected)
It's a pretty easy setup. I have 4 Toggles that toggle the visibility of objects. It's supposed to be extensible with more toggles and objects added over time.
So I wanted to have 1 OnValueChanged(Toggle source, bool value) method that does:
public void OnValueChanged(Toggle source, bool value)
{
if(source == toggle1)
{
gameobject1.render.enabled = value;
} else if (source == toggle 2)
{
gameobject2.render.enabled = value;
}
....
}
Or, a bit cleaner, access a Dictionary < Toggle, GameObject >:
public void OnValueChanged(Toggle source, bool value)
{
dictionary[source].renderer.enabled = value;
}
Well you can do :
public void OnValueChanged(Toggle source)
{
dictionary[source].renderer.enabled = source.isOn;
}
then you can assign from the inspector the toggle reference for you callback.
edit: should look like this I don't know if there is another way...
Your answer
Follow this Question
Related Questions
Sound getting deeper 2 Answers
client/server in the same project? 0 Answers
InvalidCastException: Cannot cast from source type to destination type. 3 Answers
Toggling through music 1 Answer
Is there a toggle for snap to grid? 10 Answers