Last active
September 25, 2019 09:15
-
-
Save nkjzm/156499eee0717a2728f958fd6224ceb4 to your computer and use it in GitHub Desktop.
SliderやToggleの状態を簡易的に保存する拡張メソッド
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
public static class SaveStateExtensions | |
{ | |
public static void SaveState(this Toggle toggle, string key) | |
{ | |
key = $"{new System.Diagnostics.StackFrame(1).GetMethod().ReflectedType}+{key}"; | |
Debug.Log(key); | |
toggle.isOn = PlayerPrefs.GetInt(key, 0) == 1; | |
toggle.onValueChanged.AddListener(isOn => PlayerPrefs.SetInt(key, isOn ? 1 : 0)); | |
} | |
public static void SaveState(this Slider slider, string key) | |
{ | |
key = $"{new System.Diagnostics.StackFrame(1).GetMethod().Name}+{key}"; | |
slider.value = PlayerPrefs.GetFloat(key, 0f); | |
slider.onValueChanged.AddListener(value => PlayerPrefs.SetFloat(key, value)); | |
} | |
public static void SaveState(this InputField inputField, string key) | |
{ | |
key = $"{new System.Diagnostics.StackFrame(1).GetMethod().Name}+{key}"; | |
inputField.text = PlayerPrefs.GetString(key, string.Empty); | |
inputField.onValueChanged.AddListener(value => PlayerPrefs.SetString(key, value)); | |
} | |
public static void SaveState(this Dropdown dropdown, string key) | |
{ | |
key = $"{new System.Diagnostics.StackFrame(1).GetMethod().Name}+{key}"; | |
dropdown.value = PlayerPrefs.GetInt(key, 0); | |
dropdown.onValueChanged.AddListener(value => PlayerPrefs.SetInt(key, value)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
【Unity】既存のSliderやToggleの状態を簡易的に保存する拡張メソッド【SaveStateExtensions】 - Qiita
https://qiita.com/nkjzm/items/62f3bce82ed2d75833c0