using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 本地数据持久化
/// </summary>
public class PlayerPrefsManager
{
    #region 固定KEY
    /// <summary>
    /// 启动引导
    /// </summary>
    public const string QC_STARTUP_GUIDE = "startup_guide";

    #endregion
    public static bool HasKey(string key)
    {
        return PlayerPrefs.HasKey(key);
    }
    public static float GetFloat(string key)
    {
        string diffAccount = GetDifferentAccount(key);
        return HasKey(diffAccount) ? PlayerPrefs.GetFloat(diffAccount) : -1f;     
    }
    public static int GetInt(string key)
    {
        string diffAccount = GetDifferentAccount(key);
        return HasKey(diffAccount) ? PlayerPrefs.GetInt(diffAccount) : 0;
    }
    public static string GetString(string key)
    {
        string diffAccount = GetDifferentAccount(key);
        return HasKey(diffAccount) ? PlayerPrefs.GetString(diffAccount) : string.Empty;
    }
    public static bool GetBoolean(string key)
    {
        string diffAccount = GetDifferentAccount(key);
        if (HasKey(diffAccount))
        {
            return PlayerPrefs.GetInt(diffAccount) == 1 ? true : false;
        }
        else
        {
            return false;
        }
    }

    public static void SetFloat(string key, float value)
    {
        PlayerPrefs.SetFloat(GetDifferentAccount(key), value);
        PlayerPrefs.Save();
    }
    public static void SetInt(string key, int value)
    {
        PlayerPrefs.SetInt(GetDifferentAccount(key), value);
        PlayerPrefs.Save();
    }
    public static void SetString(string key, string value)
    {
        PlayerPrefs.SetString(GetDifferentAccount(key), value);
        PlayerPrefs.Save();
    }
    public static void SetBoolean(string key, bool value)
    {
        PlayerPrefs.SetInt(GetDifferentAccount(key), (value ? 1 : 0));
        PlayerPrefs.Save();
    }
    /// <summary>
    /// 获取自身帐号相关Key
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    static string GetDifferentAccount(string key)
    {
        //唯一ID + key
        return key;
    }
    /// <summary>
    /// 删除指定的Key
    /// </summary>
    /// <param name="key"></param>
    public static void DeleteKey(string key)
    {
        if (PlayerPrefs.HasKey(key))
        {
            PlayerPrefs.DeleteKey(key);
        }
    }
}
Last modification:January 18th, 2021 at 03:00 am
如本文“对您有用”,请作者喝杯咖啡吧!