slider.onchanged in unity 1

slider.onchanged in unity

//All in one
slider.onValueChanged.addListener(delegate{myFunction(slider.value);});

//Easier to read
ListenSlider(slider, (value) => { myFunction(value); });

public void ListenSlider(Slider slider, Action  onchange )
	{
		slider.onValueChanged.AddListener(delegate{onchange(slider.value);});
	}

Here is what the above code is Doing:
1. We create a function that takes a slider and an action as parameters.
2. We add a listener to the slider’s onValueChanged event.
3. The listener calls the action, passing in the slider’s value.

Now we can use the ListenSlider function to add a listener to any slider.

Similar Posts