保存一个向量
static function SetVector3 (key : string, value : Vector3) : boolean
[code=js]//尝试保存一个物体位置
var player : GameObject;
if (!PlayerPrefsX.SetVector3("layerPosition", player.transform.position))
print("不能保存物体位置!");[/code]
成功返回真,否则假(例如用Webplayer保存超过1M数据的时候).
获得一个向量
[code=js]var player : GameObject;
player.transform.position = PlayerPrefsX.GetVector3("layerPosition");[/code]
如果读取的向量存在的话将会返回这个向量值.
保存一组整型数据
[code=js]//当保存Scores命名的分数时候创建一个10成员数组
var myScores = new int[10];
for (i = 0; i < myScores.Length; i++)
myScores = i+1;
if (!PlayerPrefsX.SetIntArray("Scores", myScores))
print("不能保存分数!");[/code]
static function GetIntArray (key : String) : int[] {
if (PlayerPrefs.HasKey(key)) {
var stringArray = PlayerPrefs.GetString(key).Split("|"[0]);
var intArray = new int[stringArray.Length];
for (i = 0; i < stringArray.Length; i++) {
intArray = parseInt(stringArray);
}
return intArray;
}
return new int[0];
}
static function GetIntArray (key : String, defaultValue : int, defaultSize : int) : int[] {
if (PlayerPrefs.HasKey(key)) {
return GetIntArray(key);
}
var intArray = new int[defaultSize];
for (i = 0; i < defaultSize; i++) {
intArray = defaultValue;
}
return intArray;
}
static function SetFloatArray (key : String, floatArray : float[]) : boolean {
if (floatArray.Length == 0) return false;
var sb = new System.Text.StringBuilder();
for (i = 0; i < floatArray.Length-1; i++) {
sb.Append(floatArray).Append("|");
}
sb.Append(floatArray);
static function GetFloatArray (key : String) : float[] {
if (PlayerPrefs.HasKey(key)) {
var stringArray = PlayerPrefs.GetString(key).Split("|"[0]);
var floatArray = new float[stringArray.Length];
for (i = 0; i < stringArray.Length; i++) {
floatArray = parseFloat(stringArray);
}
return floatArray;
}
return new float[0];
}
static function GetFloatArray (key : String, defaultValue : float, defaultSize : int) : float[] {
if (PlayerPrefs.HasKey(key)) {
return GetFloatArray(key);
}
var floatArray = new float[defaultSize];
for (i = 0; i < defaultSize; i++) {
floatArray = defaultValue;
}
return floatArray;
}
脚本的C#版
[code=cs]// Contribution (Created CSharp Version) 10/2010: Daniel P. Rossi (DR9885)
// Contribution (Created Bool Array) 10/2010: Daniel P. Rossi (DR9885)
// Contribution (Made functions public) 01/2011: Bren
using UnityEngine;
using System;
public static class PlayerPrefsX
{
#region Vector 3
/// <summary>
/// Stores a Vector3 value into a Key
/// </summary>
public static bool SetVector3(string key, Vector3 vector)
{
return SetFloatArray(key, new float[3] { vector.x, vector.y, vector.z });
}
/// <summary>
/// Finds a Vector3 value from a Key
/// </summary>
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 Bool Array
/// <summary>
/// Stores a Bool Array or Multiple Parameters into a Key
/// </summary>
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).Append("|");
sb.Append(boolArray[boolArray.Length - 1]);
/// <summary>
/// Returns a Bool Array from a Key
/// </summary>
public static bool[] GetBoolArray(string key)
{
if (PlayerPrefs.HasKey(key))
{
string[] stringArray = PlayerPrefs.GetString(key).Split("|"[0]);
bool[] boolArray = new bool[stringArray.Length];
for (int i = 0; i < stringArray.Length; i++)
boolArray = Convert.ToBoolean(stringArray);
return boolArray;
}
return new bool[0];
}
/// <summary>
/// Returns a Bool Array from a Key
/// Note: Uses default values to initialize if no key was found
/// </summary>
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 = defaultValue;
return boolArray;
}
#endregion
#region Int Array
/// <summary>
/// Stores a Int Array or Multiple Parameters into a Key
/// </summary>
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).Append("|");
sb.Append(intArray[intArray.Length - 1]);
/// <summary>
/// Returns a Int Array from a Key
/// </summary>
public static int[] GetIntArray(string key)
{
if (PlayerPrefs.HasKey(key))
{
string[] stringArray = PlayerPrefs.GetString(key).Split("|"[0]);
int[] intArray = new int[stringArray.Length];
for (int i = 0; i < stringArray.Length; i++)
intArray = Convert.ToInt32(stringArray);
return intArray;
}
return new int[0];
}
/// <summary>
/// Returns a Int Array from a Key
/// Note: Uses default values to initialize if no key was found
/// </summary>
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 = defaultValue;
return intArray;
}
#endregion
#region Float Array
/// <summary>
/// Stores a Float Array or Multiple Parameters into a Key
/// </summary>
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).Append("|");
sb.Append(floatArray[floatArray.Length - 1]);
/// <summary>
/// Returns a Float Array from a Key
/// </summary>
public static float[] GetFloatArray(string key)
{
if (PlayerPrefs.HasKey(key))
{
string[] stringArray = PlayerPrefs.GetString(key).Split("|"[0]);
float[] floatArray = new float[stringArray.Length];
for (int i = 0; i < stringArray.Length; i++)
floatArray = Convert.ToSingle(stringArray);
return floatArray;
}
return new float[0];
}
/// <summary>
/// Returns a String Array from a Key
/// Note: Uses default values to initialize if no key was found
/// </summary>
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 = defaultValue;
return floatArray;
}
#endregion
#region String Array
/// <summary>
/// Stores a String Array or Multiple Parameters into a Key w/ specific char seperator
/// </summary>
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 ***e;
}
/// <summary>
/// Stores a Bool Array or Multiple Parameters into a Key
/// </summary>
public static bool SetStringArray(string key, params string[] stringArray)
{
if (!SetStringArray(key, "
"[0], stringArray))
return false;
return ***e;
}
/// <summary>
/// Returns a String Array from a key & char seperator
/// </summary>
public static string[] GetStringArray(string key, char separator)
{
if (PlayerPrefs.HasKey(key))
return PlayerPrefs.GetString(key).Split(separator);
return new string[0];
}
/// <summary>
/// Returns a Bool Array from a key
/// </summary>
public static string[] GetStringArray(string key)
{
if (PlayerPrefs.HasKey(key))
return PlayerPrefs.GetString(key).Split("
"[0]);
return new string[0];
}
/// <summary>
/// Returns a String Array from a key & char seperator
/// Note: Uses default values to initialize if no key was found
/// </summary>
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 = defaultValue;
return stringArray;
}
/// <summary>
/// Returns a String Array from a key
/// Note: Uses default values to initialize if no key was found
/// </summary>
public static String[] GetStringArray(string key, string defaultValue, int defaultSize)
{
return GetStringArray(key, "
"[0], defaultValue, defaultSize);
}