unity android本地存储,【Unity项目实战】本地存储工具类LocalSave分享

如果想要查找存储值的位置的话:

Mac OS

在Mac OS X上PlayerPrefs是存储在~/Library/Preferences文件夹,名为unity.[company name].[product name].plist,其中company name和product name名是在Project Setting中设置,.plist文件可用于编辑器和桌面平台运行。 (打开Find,按住Option键,点击“前往 →“资源库”,就可以找到Preferences文件夹。)

Windows

在Windows平台下,PlayerPrefs被存储在注册表的 HKEY_CURRENT_USER\Software[company name][product name]键下(打开“运行”输入regedit打开注册表),其中company name和product name名是在Project Setting中设置。

例如

[HKEY_CURRENT_USER\SOFTWARE*1*2]

*1对应PlayerSettings的Company Name

*2对应Product Name

...

using UnityEngine;

using System.Collections;

using System;

public class LocalSave

{

public static string GetString(string key, string defaultvalue = "")

{

return PlayerPrefs.GetString(key, defaultvalue);

}

public static void SetString(string key, string value)

{

PlayerPrefs.SetString(key, value);

}

public static int GetInt(string key, int defaultvalue = 0)

{

return PlayerPrefs.GetInt(key, defaultvalue);

}

public static void SetInt(string key, int value)

{

PlayerPrefs.SetInt(key, value);

}

public static float GetFloat(string key, float defaultvalue = 0)

{

return PlayerPrefs.GetFloat(key, defaultvalue);

}

public static void SetFloat(string key, float value)

{

PlayerPrefs.SetFloat(key, value);

}

///

/// 储存Bool

///

public static void SetBool(string key, bool value)

{

PlayerPrefs.SetString(key + "Bool", value.ToString());

}

///

/// 取Bool

///

public static bool GetBool(string key)

{

try

{

return bool.Parse(PlayerPrefs.GetString(key + "Bool"));

}

catch (Exception e)

{

return false;

}

}

///

/// 储存IntArray

///

public static void SetIntArray(string key, int[] value)

{

for (int i = 0; i < value.Length; i++)

{

PlayerPrefs.SetInt(key + "IntArray" + i, value[i]);

}

PlayerPrefs.SetInt(key + "IntArray", value.Length);

}

///

/// 取IntArray

///

public static int[] GetIntArray(string key)

{

int[] intArr = new int[1];

if (PlayerPrefs.GetInt(key + "IntArray") != 0)

{

intArr = new int[PlayerPrefs.GetInt(key + "IntArray")];

for (int i = 0; i < intArr.Length; i++)

{

intArr[i] = PlayerPrefs.GetInt(key + "IntArray" + i);

}

}

return intArr;

}

///

/// 储存FloatArray

///

public static void SetFloatArray(string key, float[] value)

{

for (int i = 0; i < value.Length; i++)

{

PlayerPrefs.SetFloat(key + "FloatArray" + i, value[i]);

}

PlayerPrefs.SetInt(key + "FloatArray", value.Length);

}

///

/// 取FloatArray

///

public static float[] GetFloatArray(string key)

{

float[] floatArr = new float[1];

if (PlayerPrefs.GetInt(key + "FloatArray") != 0)

{

floatArr = new float[PlayerPrefs.GetInt(key + "FloatArray")];

for (int i = 0; i < floatArr.Length; i++)

{

floatArr[i] = PlayerPrefs.GetFloat(key + "FloatArray" + i);

}

}

return floatArr;

}

///

/// 储存StringArray

///

public static void SetStringArray(string key, string[] value)

{

for (int i = 0; i < value.Length; i++)

{

PlayerPrefs.SetString(key + "StringArray" + i, value[i]);

}

PlayerPrefs.SetInt(key + "StringArray", value.Length);

}

///

/// 取StringArray

///

public static string[] GetStringArray(string key)

{

string[] stringArr = new string[1];

if (PlayerPrefs.GetInt(key + "StringArray") != 0)

{

stringArr = new string[PlayerPrefs.GetInt(key + "StringArray")];

for (int i = 0; i < stringArr.Length; i++)

{

stringArr[i] = PlayerPrefs.GetString(key + "StringArray" + i);

}

}

return stringArr;

}

///

/// 储存Vector2

///

public static void SetVector2(string key, Vector2 value)

{

PlayerPrefs.SetFloat(key + "Vector2X", value.x);

PlayerPrefs.SetFloat(key + "Vector2Y", value.y);

}

///

/// 取Vector2

///

/// 键

///

public static Vector2 GetVector2(string key)

{

Vector2 Vector2;

Vector2.x = PlayerPrefs.GetFloat(key + "Vector2X");

Vector2.y = PlayerPrefs.GetFloat(key + "Vector2Y");

return Vector2;

}

///

/// 储存Vector3

///

public static void SetVector3(string key, Vector3 value)

{

PlayerPrefs.SetFloat(key + "Vector3X", value.x);

PlayerPrefs.SetFloat(key + "Vector3Y", value.y);

PlayerPrefs.SetFloat(key + "Vector3Z", value.z);

}

///

/// 取Vector3

///

public static Vector3 GetVector3(string key)

{

Vector3 vector3;

vector3.x = PlayerPrefs.GetFloat(key + "Vector3X");

vector3.y = PlayerPrefs.GetFloat(key + "Vector3Y");

vector3.z = PlayerPrefs.GetFloat(key + "Vector3Z");

return vector3;

}

///

/// 储存Quaternion

///

public static void SetQuaternion(string key, Quaternion value)

{

PlayerPrefs.SetFloat(key + "QuaternionX", value.x);

PlayerPrefs.SetFloat(key + "QuaternionY", value.y);

PlayerPrefs.SetFloat(key + "QuaternionZ", value.z);

PlayerPrefs.SetFloat(key + "QuaternionW", value.w);

}

///

/// 取Quaternion

///

public static Quaternion GetQuaternion(string key)

{

Quaternion quaternion;

quaternion.x = PlayerPrefs.GetFloat(key + "QuaternionX");

quaternion.y = PlayerPrefs.GetFloat(key + "QuaternionY");

quaternion.z = PlayerPrefs.GetFloat(key + "QuaternionZ");

quaternion.w = PlayerPrefs.GetFloat(key + "QuaternionW");

return quaternion;

}

}

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值