87 lines
2.3 KiB
C#
87 lines
2.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class LocalStorage
|
|
{
|
|
public static readonly LocalStorage Instance = new LocalStorage();
|
|
|
|
private string user = string.Empty; // 账号
|
|
private string pid = string.Empty; // 玩家ID
|
|
|
|
// 设置账号
|
|
public void SetUser(string _user)
|
|
{
|
|
user = _user;
|
|
}
|
|
|
|
// 设置角色
|
|
public void SetPid(string _pid)
|
|
{
|
|
pid = _pid;
|
|
}
|
|
|
|
// 获取与账号角色相关的key, 以保证本地缓存不同账号不同角色的数据互不干扰 *:在设置账号和角色前, key是一致的
|
|
private string GetRoleKey(string _key)
|
|
{
|
|
return user + "_" + pid + _key;
|
|
}
|
|
|
|
public bool HasKey(string _key)
|
|
{
|
|
string key = GetRoleKey(_key);
|
|
return PlayerPrefs.HasKey(key);
|
|
}
|
|
|
|
// 设置值
|
|
public void SetString(string _key, string _val)
|
|
{
|
|
if(string.IsNullOrEmpty(_key))
|
|
{
|
|
Debug.LogError("SetVal. string.IsNullOrEmpty(_key)");
|
|
return;
|
|
}
|
|
|
|
string key = GetRoleKey(_key);
|
|
PlayerPrefs.SetString(key, _val.Trim());
|
|
PlayerPrefs.Save();
|
|
}
|
|
|
|
// 获取数字值
|
|
public int GetInt(string _key)
|
|
{
|
|
string key = GetRoleKey(_key);
|
|
string retStr = PlayerPrefs.GetString(key, "0");
|
|
|
|
return int.Parse(retStr.Trim());
|
|
}
|
|
|
|
// 获取字符串值
|
|
public string GetString(string _key)
|
|
{
|
|
string key = GetRoleKey(_key);
|
|
string retStr = PlayerPrefs.GetString(key, "");
|
|
|
|
return retStr;
|
|
}
|
|
|
|
public void DeleteKey(string _key)
|
|
{
|
|
string key = GetRoleKey(_key);
|
|
PlayerPrefs.DeleteKey(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 本地缓存Key常量
|
|
/// </summary>
|
|
//public static readonly string KeyLoginUser = "LoginUser"; // 登录账号
|
|
public static readonly string KeyLoginUserPhone = "LoginUserPhone"; // 登录账号_手机账号
|
|
//public static readonly string KeyLoginPwd = "LoginPwd"; // 登录密码
|
|
public static readonly string KeyLoginLastSelectIdx = "KeyLoginLastSelectIdx"; // 上次选择的角色
|
|
public static readonly string KeyLoginLastLoginType = "KeyLoginLastLoginType"; // 上次登录账号类型
|
|
|
|
|
|
public static readonly string KeyLoginQuickItemIDList = "KeyLoginQuickItemIDList"; // 快捷道具id列
|
|
|
|
}
|