Skip to content

Instantly share code, notes, and snippets.

@masayuki5160
Last active August 29, 2015 14:11
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 masayuki5160/265f766fd21725e298fd to your computer and use it in GitHub Desktop.
Save masayuki5160/265f766fd21725e298fd to your computer and use it in GitHub Desktop.
PlayerPrefsをためしにラップしてみる
using UnityEngine;
using System.Collections;
public class main : MonoBehaviour {
// Use this for initialization
void Start () {
// stringのテスト
MyPlayerPrefs.setVal(MyPlayerPrefs.KEY.TMP_KEY, "1");
// integerのテスト
MyPlayerPrefs.setVal(MyPlayerPrefs.KEY.SORT, 100);
// データ取得例
Debug.Log("test2::str->" + MyPlayerPrefs.getVal(MyPlayerPrefs.KEY.TMP_KEY));
Debug.Log("test2::int->" + MyPlayerPrefs.getVal(MyPlayerPrefs.KEY.SORT).ToString());
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//============================================================================================//
// PlayerPrefsのラッパー
//============================================================================================//
public static class MyPlayerPrefs {
/*
* PlayerPrefsで使用できる型リスト
*/
private enum TYPE {
INT,
STRING,
FLOAT
}
/*
* PlayerPrefsで使用するキーリスト
* 追加する場合はここに追加してからね
*/
public enum KEY {
SORT, // ex. 編成画面でのソート順
TMP_KEY // ex. なんらかのキー
}
/*
* PlayerPrefsで管理しているデータリスト
*/
private static Dictionary<KEY, TYPE> myPrefsList = new Dictionary<KEY, TYPE>(){
{KEY.SORT, TYPE.INT},
{KEY.TMP_KEY, TYPE.STRING}
};
//============================================================================================//
/*
* データ取得
*/
public static object getVal(KEY key) {
switch(myPrefsList[key]){
case TYPE.INT:
return PlayerPrefs.GetInt(key.ToString());
case TYPE.STRING:
return PlayerPrefs.GetString(key.ToString());
case TYPE.FLOAT:
return PlayerPrefs.GetFloat(key.ToString());
default:
return null;
}
}
/*
* データセット
*/
public static void setVal(KEY key, object val) {
if(val is string){
PlayerPrefs.SetString(key.ToString(), (string)val);
}else if(val is int){
PlayerPrefs.SetInt(key.ToString(), (int)val);
}else if(val is float){
PlayerPrefs.SetFloat(key.ToString(), (float)val);
}
}
}
@masayuki5160
Copy link
Author

objectをつかうように修正

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