I'm developing games for almost 1 year but the code for UI elements is the most painful part of my game dev. Making UI is working is pretty easy although it's a lot of work to do it seems like I'm writing the same code again and again. Here is some code example that'll help you understand what I'm trying to convey to you
public Slider volume; //let suppose i have too many sliders
public float volume_val;
private Toggle soundToggler;// let suppose i have too many Toggle
public bool isMuted;
private Text coinText; // let suppose i have too many Text
public float coins;
// let suppose i have too many XYZ fields
private void Awake ()
{
volume_val = PlayerPrefs.GetFloat ("volume_val");
volume.value = volume_val;
volume.onValueChanged.AddListener ( (newVolume) => {
volume_val = newVolume;
PlayerPrefs.SetFloat ("volume_val", volume_val);
});
isMuted = PlayerPrefs.GetInt ("isMuted") == 1 ? true : false;
soundToggler.isOn = isMuted;
soundToggler.onValueChanged.AddListener ((newVal)=> {
isMuted = newVal;
PlayerPrefs.SetInt ("isMuted", isMuted ? 1 : 0);
});
coins = PlayerPrefs.GetFloat ("coins");
coinText.text = coins;
// update text on value update
}
As you can see by the code I'm repeating the same code, again and again, is there any generic way to do this?