Skip to content

Instantly share code, notes, and snippets.

@codehoose
Created July 15, 2018 19:28
Show Gist options
  • Save codehoose/daa8ea19f142d4453e9a3bd6bd4b77b1 to your computer and use it in GitHub Desktop.
Save codehoose/daa8ea19f142d4453e9a3bd6bd4b77b1 to your computer and use it in GitHub Desktop.
Code from the video https://youtu.be/3NzNIsJO2Hs. Saves the dropdown's selected item to playerprefs & reloads the value when the game restarts.
[RequireComponent(typeof(Dropdown))]
public class SaveDropdownValue : MonoBehaviour
{
const string PrefName = "optionvalue";
private Dropdown _dropdown;
void Awake()
{
_dropdown = GetComponent<Dropdown>();
_dropdown.onValueChanged.AddListener(new UnityAction<int>(index =>
{
PlayerPrefs.SetInt(PrefName, _dropdown.value);
PlayerPrefs.Save();
}));
}
void Start ()
{
_dropdown.value = PlayerPrefs.GetInt(PrefName, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment