大家好,我是阿赵。
Unity自带的PlayerPrefs功能,相信用过Unity的朋友都知道。这个功能可以在本地存储一些数据。在游戏里面做一些简单的本地记录功能还是比较的方便。
不过有时候PlayerPrefs的值存多了之后,会容易乱,哪些key已经存过,哪些key的值是多少,在测试功能的时候要手动清除某些key之类,会比较麻烦。
这里我简单写了一个管理PlayerPrefs的Unity工具。
在设置key的时候,可以选择3种数据类型
如果需要保存的值和选择的类型不对应,会有错误提示
如果类型正确就会设置成功
在检查key是否存在的时候,由于Unity的API并没有判断保存的key是什么类型的值,所以我是把3种值都列出来了。
可以直接删除某个key保存的值
可以删除所有key,不过由于这个操作会比较危险,所以有二次确认
下面是工具的源码:
using UnityEditor;
using UnityEngine;
public class PlayerPrefsToolWin : EditorWindow
{
static private PlayerPrefsToolWin _instance;
public static PlayerPrefsToolWin Instance {
get
{
if(_instance == null)
{
_instance = (PlayerPrefsToolWin)EditorWindow.GetWindow(typeof(PlayerPrefsToolWin));
_instance.maxSize = _instance.minSize = new Vector2(600, 200);
_instance.titleContent = new GUIContent("PlayerPrefs管理工具");
}
return _instance;
}
}
private string[] typeStrs = new string[] {"int","float","string" };
private int typeAddIndex = 0;
private string keyCheck = "";
private string keySet = "";
private string keyDel = "";
private string addValueStr = "";
[MenuItem("Tools/小工具/PlayerPrefs管理工具")]
static void ShowWin()
{
PlayerPrefsToolWin.Instance.Show();
}
void OnGUI()
{
ShowLine("查询Key是否存在");
ShowCheckKey();
ShowLine();
ShowLine("设置Key对应的值");
ShowSetKey();
ShowLine();
ShowLine("删除Key对应的值");
ShowDelKey();
ShowLine();
}
#region 检查key是否存在
private void ShowCheckKey()
{
GUILayout.BeginHorizontal();
GUILayout.Label("key:", GUILayout.Width(60));
keyCheck = EditorGUILayout.TextField(keyCheck, GUILayout.Width(140));
if (GUILayout.Button("检查",GUILayout.Width(100)))
{
CheckKeyExistFun();
}
GUILayout.EndHorizontal();
}
private void CheckKeyExistFun()
{
if(string.IsNullOrEmpty(keyCheck))
{
ShowTips("key为空,请输入正确的key");
return;
}
bool isExist = PlayerPrefs.HasKey(keyCheck);
if(isExist == true)
{
string str = PlayerPrefs.GetString(keyCheck);
float floatVal = PlayerPrefs.GetFloat(keyCheck);
float intVal = PlayerPrefs.GetInt(keyCheck);
string tipsStr = "key:" + keyCheck + "已经存在\n";
tipsStr += "string的值为:" + str + "\n";
tipsStr += "float的值为:" + floatVal + "\n";
tipsStr += "int的值为:" + intVal;
ShowTips(tipsStr);
}
else
{
ShowTips("key:" + keyCheck + "不存在");
}
}
#endregion
#region 设置Key对应的值
private void ShowSetKey()
{
GUILayout.BeginHorizontal();
GUILayout.Label("key:", GUILayout.Width(60));
keySet = EditorGUILayout.TextField(keySet, GUILayout.Width(140));
typeAddIndex = EditorGUILayout.Popup(typeAddIndex, typeStrs, GUILayout.Width(80));
addValueStr = EditorGUILayout.TextField(addValueStr, GUILayout.Width(200));
if (GUILayout.Button("设置", GUILayout.Width(100)))
{
SetKeyFun();
}
GUILayout.EndHorizontal();
}
private void SetKeyFun()
{
if(string.IsNullOrEmpty(keySet))
{
ShowTips("key为空,请输入正确的key");
return;
}
if(string.IsNullOrEmpty(addValueStr))
{
ShowTips("设置的值为空,请输入正确的值");
return;
}
bool isSuccess = false;
if(typeAddIndex == 0)
{
int intVal = 0;
try
{
intVal = int.Parse(addValueStr.Trim());
}
catch(System.Exception ex)
{
ShowTips("设置的值不是整型,请输入正确的值:" + addValueStr);
return;
}
PlayerPrefs.SetInt(keySet, intVal);
isSuccess = true;
}
else if(typeAddIndex == 1)
{
float floatVal = 0;
try
{
floatVal = float.Parse(addValueStr.Trim());
}
catch(System.Exception ex)
{
ShowTips("设置的值不是浮点类型,请输入正确的值:"+ addValueStr);
return;
}
PlayerPrefs.SetFloat(keyCheck, floatVal);
isSuccess = true;
}
else if(typeAddIndex == 2)
{
PlayerPrefs.SetString(keyCheck, addValueStr);
isSuccess = true;
}
else
{
ShowTips("设置失败,位置的数据类型");
return;
}
if(isSuccess == true)
{
ShowTips("设置成功");
}
else
{
ShowTips("设置失败");
}
}
#endregion
#region 删除key对应的值
private void ShowDelKey()
{
GUILayout.BeginHorizontal();
GUILayout.Label("key:", GUILayout.Width(60));
keyDel = EditorGUILayout.TextField(keyDel, GUILayout.Width(140));
if (GUILayout.Button("删除", GUILayout.Width(100)))
{
DelKeyFun();
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("", GUILayout.Width(60));
if (GUILayout.Button("删除所有key", GUILayout.Width(240),GUILayout.Height(30)))
{
DelAllKeyFun();
}
GUILayout.EndHorizontal();
}
private void DelKeyFun()
{
if(string.IsNullOrEmpty(keyDel))
{
ShowTips("key为空,请输入正确的key");
return;
}
bool isExist = PlayerPrefs.HasKey(keyDel);
if(isExist == false)
{
ShowTips("key:" + keyDel + "不存在");
}
else
{
PlayerPrefs.DeleteKey(keyDel);
ShowTips("key:" + keyDel + "删除成功");
}
}
private void DelAllKeyFun()
{
if(ShowTipsSelect("确定需要删除所有的key?"))
{
PlayerPrefs.DeleteAll();
ShowTips("删除成功");
}
}
#endregion
#region 其他选项
private void ShowTips(string str)
{
Debug.Log(str);
EditorUtility.DisplayDialog("提示", str, "确定");
}
private bool ShowTipsSelect(string str)
{
return EditorUtility.DisplayDialog("提示", str, "确定","取消");
}
private void ShowLine(string str = "", int w = 600, int h = 20)
{
if (string.IsNullOrEmpty(str))
{
h = 5;
}
GUILayout.Box(str, GUILayout.Width(w), GUILayout.Height(h));
}
#endregion
}