Skip to content

Instantly share code, notes, and snippets.

@nkjzm
Last active September 25, 2019 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkjzm/156499eee0717a2728f958fd6224ceb4 to your computer and use it in GitHub Desktop.
Save nkjzm/156499eee0717a2728f958fd6224ceb4 to your computer and use it in GitHub Desktop.
SliderやToggleの状態を簡易的に保存する拡張メソッド
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));
}
}
@nkjzm
Copy link
Author

nkjzm commented Sep 25, 2019

【Unity】既存のSliderやToggleの状態を簡易的に保存する拡張メソッド【SaveStateExtensions】 - Qiita
https://qiita.com/nkjzm/items/62f3bce82ed2d75833c0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment