Last active
August 29, 2015 14:11
-
-
Save masayuki5160/265f766fd21725e298fd to your computer and use it in GitHub Desktop.
PlayerPrefsをためしにラップしてみる
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 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()); | |
} | |
} |
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 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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
objectをつかうように修正