Unity3dPlayerPrefsメソッド拡張



Unity3d Playerprefs Method Extension

データベースを使用しない小規模なゲームに適しています。ローカライズされたストレージにUnity独自のAPIを使用します。拡張機能を追加して、随時更新することができます。

public static class PlayerPrefsExtention { #region Vector 2 /// /// Stores a Vector2 value into a Key /// public static bool SetVector2(string key, Vector2 vector) { return SetFloatArray(key, new float[2] { vector.x, vector.y}) } /// /// Finds a Vector2 value from a Key /// public static Vector2 GetVector2(string key) { float[] floatArray = GetFloatArray(key) if (floatArray.Length <2) return Vector2.zero return new Vector2(floatArray[0], floatArray[1]) } #endregion #region Vector 3 /// /// Stores a Vector3 value into a Key /// public static bool SetVector3(string key, Vector3 vector) { return SetFloatArray(key, new float[3] { vector.x, vector.y, vector.z }) } /// /// Finds a Vector3 value from a Key /// public static Vector3 GetVector3(string key) { float[] floatArray = GetFloatArray(key) if (floatArray.Length < 3) return Vector3.zero return new Vector3(floatArray[0], floatArray[1], floatArray[2]) } #endregion #region Quaternion /// /// Stores a Quaternion value into a Key /// public static bool SetQuaternion(string key, Quaternion value) { return SetFloatArray(key, new float[4] { value.x, value.y, value.z, value.w }) } /// /// Finds a Quaternion value from a Key /// public static Quaternion GetQuaternion(string key) { float[] floatArray = GetFloatArray(key) if (floatArray.Length < 4) return Quaternion.identity return new Quaternion(floatArray[0], floatArray[1], floatArray[2], floatArray[3]) } #endregion #region Bool Array /// /// Stores a Bool Array or Multiple Parameters into a Key /// public static bool SetBoolArray(string key, params bool[] boolArray) { if (boolArray.Length == 0) return false System.Text.StringBuilder sb = new System.Text.StringBuilder() for (int i = 0 i < boolArray.Length - 1 i++) sb.Append(boolArray[i]).Append('|') sb.Append(boolArray[boolArray.Length - 1]) try { PlayerPrefs.SetString(key, sb.ToString()) } catch (Exception e) { return false } return true } /// /// Returns a Bool Array from a Key /// public static bool[] GetBoolArray(string key) { if (PlayerPrefs.HasKey(key)) string[] stringArray = PlayerPrefs.GetString(key).Split(' return new bool[0] } /// /// Returns a Bool Array from a Key /// Note: Uses default values to initialize if no key was found /// public static bool[] GetBoolArray(string key, bool defaultValue, int defaultSize) { if (PlayerPrefs.HasKey(key)) return GetBoolArray(key) bool[] boolArray = new bool[defaultSize] for (int i = 0 i < defaultSize i++) boolArray[i] = defaultValue return boolArray } #endregion #region Int Array /// /// Stores a Int Array or Multiple Parameters into a Key /// public static bool SetIntArray(string key, params int[] intArray) { if (intArray.Length == 0) return false System.Text.StringBuilder sb = new System.Text.StringBuilder() for (int i = 0 i < intArray.Length - 1 i++) sb.Append(intArray[i]).Append('|') sb.Append(intArray[intArray.Length - 1]) try { PlayerPrefs.SetString(key, sb.ToString()) } catch (Exception e) { return false } return true } /// /// Returns a Int Array from a Key /// public static int[] GetIntArray(string key) { if (PlayerPrefs.HasKey(key)) '[0]) int[] intArray = new int[stringArray.Length] for (int i = 0 i < stringArray.Length i++) intArray[i] = Convert.ToInt32(stringArray[i]) return intArray return new int[0] } /// /// Returns a Int Array from a Key /// Note: Uses default values to initialize if no key was found /// public static int[] GetIntArray(string key, int defaultValue, int defaultSize) { if (PlayerPrefs.HasKey(key)) return GetIntArray(key) int[] intArray = new int[defaultSize] for (int i = 0 i < defaultSize i++) intArray[i] = defaultValue return intArray } #endregion #region Float Array /// /// Stores a Float Array or Multiple Parameters into a Key /// public static bool SetFloatArray(string key, params float[] floatArray) { if (floatArray.Length == 0) return false System.Text.StringBuilder sb = new System.Text.StringBuilder() for (int i = 0 i < floatArray.Length - 1 i++) sb.Append(floatArray[i]).Append('|') sb.Append(floatArray[floatArray.Length - 1]) try { PlayerPrefs.SetString(key, sb.ToString()) } catch (Exception e) { return false } return true } /// /// Returns a Float Array from a Key /// public static float[] GetFloatArray(string key) { if (PlayerPrefs.HasKey(key)) '[0]) float[] floatArray = new float[stringArray.Length] for (int i = 0 i < stringArray.Length i++) floatArray[i] = Convert.ToSingle(stringArray[i]) return floatArray return new float[0] } /// /// Returns a String Array from a Key /// Note: Uses default values to initialize if no key was found /// public static float[] GetFloatArray(string key, float defaultValue, int defaultSize) { if (PlayerPrefs.HasKey(key)) return GetFloatArray(key) float[] floatArray = new float[defaultSize] for (int i = 0 i < defaultSize i++) floatArray[i] = defaultValue return floatArray } #endregion #region String Array /// /// Stores a String Array or Multiple Parameters into a Key w/ specific char seperator /// public static bool SetStringArray(string key, char separator, params string[] stringArray) { if (stringArray.Length == 0) return false try { PlayerPrefs.SetString(key, String.Join(separator.ToString(), stringArray)) } catch (Exception e) { return false } return true } /// /// Stores a Bool Array or Multiple Parameters into a Key /// public static bool SetStringArray(string key, params string[] stringArray) { if (!SetStringArray(key, ' '[0], stringArray)) return false return true } /// /// Returns a String Array from a key & char seperator /// public static string[] GetStringArray(string key, char separator) { if (PlayerPrefs.HasKey(key)) return PlayerPrefs.GetString(key).Split(separator) return new string[0] } /// /// Returns a Bool Array from a key /// public static string[] GetStringArray(string key) { if (PlayerPrefs.HasKey(key)) return PlayerPrefs.GetString(key).Split(' '[0]) return new string[0] } /// /// Returns a String Array from a key & char seperator /// Note: Uses default values to initialize if no key was found /// public static string[] GetStringArray(string key, char separator, string defaultValue, int defaultSize) { if (PlayerPrefs.HasKey(key)) return PlayerPrefs.GetString(key).Split(separator) string[] stringArray = new string[defaultSize] for (int i = 0 i < defaultSize i++) stringArray[i] = defaultValue return stringArray } /// /// Returns a String Array from a key /// Note: Uses default values to initialize if no key was found /// public static String[] GetStringArray(string key, string defaultValue, int defaultSize) { return GetStringArray(key, ' '[0], defaultValue, defaultSize) } #endregion }