您当前的位置: 首页 >  unity

CoderZ1010

暂无认证

  • 3浏览

    0关注

    168博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity PlayerPrefs数据持久化 拓展

CoderZ1010 发布时间:2021-02-24 16:47:45 ,浏览量:3

PlayerPrefs 是用于本地持久化保存与读取的类,以键值对的形式将数据进行保存,它支持三种数据类型的保存和读取: int、float、string,以下为对PlayerPrefs的进一步封装,使其支持更多的数据类型。

using System;
using UnityEngine;

namespace SK.Framework
{
    /// 
    /// 本地数据持久化
    /// 
    public static class PersistentData
    {
        /// 
        /// 保存Int类型数据
        /// 
        /// Key值
        /// Int数据
        public static void SetInt(string key, int value)
        {
            PlayerPrefs.SetInt(key, value);
        }
        /// 
        /// 保存Float类型数据
        /// 
        /// Key值
        /// Float数据
        public static void SetFloat(string key, float value)
        {
            PlayerPrefs.SetFloat(key, value);
        }
        /// 
        /// 保存String类型数据
        /// 
        /// Key值
        /// String数据
        public static void SetString(string key, string value)
        {
            PlayerPrefs.SetString(key, value);
        }
        /// 
        /// 保存Bool类型数据
        /// 
        /// Key值
        /// Bool数据
        public static void SetBool(string key, bool value)
        {
            PlayerPrefs.SetInt(key, value ? 0 : 1);
        }
        /// 
        /// 保存Enum类型数据
        /// 
        /// Enum类型
        /// Key值
        /// Enum数据
        public static void SetEnum(string key, T value) where T : Enum
        {
            PlayerPrefs.SetString(key, value.ToString());
        }
        /// 
        /// 保存Object数据 (Object可序列化)
        /// 
        /// Object类型
        /// Key值
        /// Value值
        public static void SetObject(string key, T value)
        {
            PlayerPrefs.SetString(key, value.ToJson());
        }

        /// 
        /// 获取Int类型数据
        /// 
        /// Key值
        /// Value值
        public static int GetInt(string key)
        {
            return PlayerPrefs.GetInt(key);
        }
        /// 
        /// 获取Float类型数据
        /// 
        /// Key值
        /// Value值
        public static float GetFloat(string key)
        {
            return PlayerPrefs.GetFloat(key);
        }
        /// 
        /// 获取String类型数据
        /// 
        /// Key值
        /// Value值
        public static string GetString(string key)
        {
            return PlayerPrefs.GetString(key);
        }
        /// 
        /// 获取Bool类型数据
        /// 
        /// Key值
        /// Value值
        public static bool GetBool(string key)
        {
            return PlayerPrefs.GetInt(key) == 0;
        }
        /// 
        /// 获取Enum类型数据
        /// 
        /// Enum类型
        /// Key值
        /// Value值
        public static T GetEnum(string key) where T : Enum
        {
            return (T)Enum.Parse(typeof(T), PlayerPrefs.GetString(key));
        }
        /// 
        /// 获取Object数据
        /// 
        /// Object类型
        /// Key值
        /// Value值
        public static T GetObject(string key)
        {
            return PlayerPrefs.GetString(key).ToObject();
        }

        /// 
        /// 保存
        /// 
        public static void Save()
        {
            PlayerPrefs.Save();
        }
        /// 
        /// 删除指定数据
        /// 
        /// Key值
        public static void Delete(string key)
        {
            PlayerPrefs.DeleteKey(key);
        }
        /// 
        /// 删除全部数据
        /// 
        public static void DeleteAll()
        {
            PlayerPrefs.DeleteAll();
        }
        /// 
        /// 是否存在Key值
        /// 
        /// Key值
        /// 是否存在
        public static bool HasKey(string key)
        {
            return PlayerPrefs.HasKey(key);
        }
    }
}
关注
打赏
1653184800
查看更多评论
立即登录/注册

微信扫码登录

0.2859s