Unity PlayerPrefs数据持久化 拓展
【摘要】
PlayerPrefs 是用于本地持久化保存与读取的类,以键值对的形式将数据进行保存,它支持三种数据类型的保存和读取: int、float、string,以下为对PlayerPrefs的进一步封装,使其支持更多的数据类型。
using System;using UnityEngine; namespace SK.Framework{ ...
PlayerPrefs 是用于本地持久化保存与读取的类,以键值对的形式将数据进行保存,它支持三种数据类型的保存和读取: int、float、string,以下为对PlayerPrefs的进一步封装,使其支持更多的数据类型。
-
using System;
-
using UnityEngine;
-
-
namespace SK.Framework
-
{
-
/// <summary>
-
/// 本地数据持久化
-
/// </summary>
-
public static class PersistentData
-
{
-
/// <summary>
-
/// 保存Int类型数据
-
/// </summary>
-
/// <param name="key">Key值</param>
-
/// <param name="value">Int数据</param>
-
public static void SetInt(string key, int value)
-
{
-
PlayerPrefs.SetInt(key, value);
-
}
-
/// <summary>
-
/// 保存Float类型数据
-
/// </summary>
-
/// <param name="key">Key值</param>
-
/// <param name="value">Float数据</param>
-
public static void SetFloat(string key, float value)
-
{
-
PlayerPrefs.SetFloat(key, value);
-
}
-
/// <summary>
-
/// 保存String类型数据
-
/// </summary>
-
/// <param name="key">Key值</param>
-
/// <param name="value">String数据</param>
-
public static void SetString(string key, string value)
-
{
-
PlayerPrefs.SetString(key, value);
-
}
-
/// <summary>
-
/// 保存Bool类型数据
-
/// </summary>
-
/// <param name="key">Key值</param>
-
/// <param name="value">Bool数据</param>
-
public static void SetBool(string key, bool value)
-
{
-
PlayerPrefs.SetInt(key, value ? 0 : 1);
-
}
-
/// <summary>
-
/// 保存Enum类型数据
-
/// </summary>
-
/// <typeparam name="T">Enum类型</typeparam>
-
/// <param name="key">Key值</param>
-
/// <param name="value">Enum数据</param>
-
public static void SetEnum<T>(string key, T value) where T : Enum
-
{
-
PlayerPrefs.SetString(key, value.ToString());
-
}
-
/// <summary>
-
/// 保存Object数据 (Object可序列化)
-
/// </summary>
-
/// <typeparam name="T">Object类型</typeparam>
-
/// <param name="key">Key值</param>
-
/// <param name="value">Value值</param>
-
public static void SetObject<T>(string key, T value)
-
{
-
PlayerPrefs.SetString(key, value.ToJson());
-
}
-
-
/// <summary>
-
/// 获取Int类型数据
-
/// </summary>
-
/// <param name="key">Key值</param>
-
/// <returns>Value值</returns>
-
public static int GetInt(string key)
-
{
-
return PlayerPrefs.GetInt(key);
-
}
-
/// <summary>
-
/// 获取Float类型数据
-
/// </summary>
-
/// <param name="key">Key值</param>
-
/// <returns>Value值</returns>
-
public static float GetFloat(string key)
-
{
-
return PlayerPrefs.GetFloat(key);
-
}
-
/// <summary>
-
/// 获取String类型数据
-
/// </summary>
-
/// <param name="key">Key值</param>
-
/// <returns>Value值</returns>
-
public static string GetString(string key)
-
{
-
return PlayerPrefs.GetString(key);
-
}
-
/// <summary>
-
/// 获取Bool类型数据
-
/// </summary>
-
/// <param name="key">Key值</param>
-
/// <returns>Value值</returns>
-
public static bool GetBool(string key)
-
{
-
return PlayerPrefs.GetInt(key) == 0;
-
}
-
/// <summary>
-
/// 获取Enum类型数据
-
/// </summary>
-
/// <typeparam name="T">Enum类型</typeparam>
-
/// <param name="key">Key值</param>
-
/// <returns>Value值</returns>
-
public static T GetEnum<T>(string key) where T : Enum
-
{
-
return (T)Enum.Parse(typeof(T), PlayerPrefs.GetString(key));
-
}
-
/// <summary>
-
/// 获取Object数据
-
/// </summary>
-
/// <typeparam name="T">Object类型</typeparam>
-
/// <param name="key">Key值</param>
-
/// <returns>Value值</returns>
-
public static T GetObject<T>(string key)
-
{
-
return PlayerPrefs.GetString(key).ToObject<T>();
-
}
-
-
/// <summary>
-
/// 保存
-
/// </summary>
-
public static void Save()
-
{
-
PlayerPrefs.Save();
-
}
-
/// <summary>
-
/// 删除指定数据
-
/// </summary>
-
/// <param name="key">Key值</param>
-
public static void Delete(string key)
-
{
-
PlayerPrefs.DeleteKey(key);
-
}
-
/// <summary>
-
/// 删除全部数据
-
/// </summary>
-
public static void DeleteAll()
-
{
-
PlayerPrefs.DeleteAll();
-
}
-
/// <summary>
-
/// 是否存在Key值
-
/// </summary>
-
/// <param name="key">Key值</param>
-
/// <returns>是否存在</returns>
-
public static bool HasKey(string key)
-
{
-
return PlayerPrefs.HasKey(key);
-
}
-
}
-
}
文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。
原文链接:coderz.blog.csdn.net/article/details/114027572
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)