Unity 数据存储PlayerPrefs管理类

2023-12-26 06:20:21

Unity 数据存储PlayerPrefs管理类

实现存取实体类对象

存储格式为Json格式

源码如下:

Singleton.cs


//单例模板
public class Singleton<T> where T : new()
{

    private static T _Instance;

    public static T Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = new T();
            }
            return _Instance;
        }
    }

    public Singleton()
    {

    }
}

InventoryEntity.cs

/// <summary>
/// 实体类
/// </summary>
public class InventoryEntity {

    /// <summary>
    /// 金币
    /// </summary>
    public int coins = 0;

    /// <summary>
    /// 钻石
    /// </summary>
    public int gams = 0;

    /// <summary>
    /// 关卡
    /// </summary>
    public int Levels = 1;

    public InventoryEntity()
    {

    }

    public InventoryEntity(int coin, int gam, int level)
    {
        this.coins = coin;
        this.gams = gam;
        this.Levels = level;
    }
}

DataManager.cs

using UnityEngine;

public class DataManager : Singleton<DataManager>
{

    /// <summary>
    /// 保存数据
    /// </summary>
    InventoryEntity m_Inventory;

    /// <summary>
    /// 初始化字典
    /// </summary>
    public void Init()
    {
        m_Inventory = GetObjectData<InventoryEntity>("data");
    }

    #region 人物ID读写

    /// <summary>
    /// 记录人物ID
    /// </summary>
    private string m_playerId = "";

    /// <summary>
    /// 设置人物ID
    /// </summary>
    /// <param name="id"></param>
    public void SetID(string id)
    {
        m_playerId = id;
    }

    /// <summary>
    /// 获取人物ID
    /// </summary>
    /// <returns></returns>
    public string GetID()
    {
        return m_playerId;
    }

    #endregion

    #region 测试该存储

    #region 修改方法

    /// <summary>
    /// 修改金币
    /// </summary>
    public void SetCoins(int coin)
    {
        m_Inventory.coins = coin;
        SaveObjectDate("data", m_Inventory);
    }

    /// <summary>
    /// 获取金币
    /// </summary>
    public int GetCoins()
    {
        return m_Inventory.coins;
    }

    /// <summary>
    /// 修改宝石
    /// </summary>
    public void SetGams(int gam)
    {
        m_Inventory.gams = gam;
        SaveObjectDate("data", m_Inventory);
    }

    /// <summary>
    /// 获取宝石
    /// </summary>
    public int GetGams()
    {
        return m_Inventory.gams;
    }

    /// <summary>
    /// 修改关卡
    /// </summary>
    public void SetLevels(int level)
    {
        m_Inventory.Levels = level;
        SaveObjectDate("data", m_Inventory);
    }

    /// <summary>
    /// 获取关卡
    /// </summary>
    public int GetLevels()
    {
        return m_Inventory.Levels;
    }

    /// <summary>
    /// 修改全部属性
    /// </summary>
    public void SetAll(int coin, int gam, int level)
    {
        m_Inventory.coins = coin;
        m_Inventory.gams = gam;
        m_Inventory.Levels = level;
        SaveObjectDate("data", m_Inventory);
    }

    /// <summary>
    /// 修改全部属性重载
    /// </summary>
    public void SetAll(InventoryEntity entity)
    {
        m_Inventory = entity;
        SaveObjectDate("data", m_Inventory);
    }

    /// <summary>
    /// 奖励使用逻辑
    /// </summary>
    public void SetAward(int awardcoin, int awardgam, int awardlevel)
    {
        m_Inventory.coins += awardcoin;
        m_Inventory.gams += awardgam;
        m_Inventory.Levels += awardlevel;
        SaveObjectDate("data", m_Inventory);
    }

    #endregion

    #endregion

    #region 泛型存取数据

    /// <summary>
    /// 存储指定类型
    /// </summary>
    public void SaveObjectDate<T>(string key, T t, bool common = false)
    {
        if (common)
        {
            PlayerPrefs.SetString(key, JsonUtility.ToJson(t));
        }
        else
        {
            PlayerPrefs.SetString(m_playerId + key, JsonUtility.ToJson(t));
        }
        PlayerPrefs.Save();
    }

    /// <summary>
    /// 获取指定类型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="common"></param>
    /// <returns></returns>
    public T GetObjectData<T>(string key, bool common = false) where T : new()
    {
        string Data = null;
        if (common)
        {
            Data = PlayerPrefs.GetString(key, null);
        }
        else
        {
            Data = PlayerPrefs.GetString(m_playerId + key, null);
        }
        if (string.IsNullOrEmpty(Data))
        {
            return new T();
        }
        else
        {
            return JsonUtility.FromJson<T>(Data);
        }
    }

    #endregion

    #region 存取数据

    /// <summary>
    /// 存储int类型
    /// </summary>
    public void SaveIntDate(string key,int num, bool common = false)
    {
        if (common)
        {
            PlayerPrefs.SetInt(key,num);
        }
        else
        {
            PlayerPrefs.SetInt(m_playerId + key, num);
        }
        PlayerPrefs.Save();
    }

    /// <summary>
    /// 获取int类型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="common"></param>
    /// <returns></returns>
    public int GetIntData<T>(string key, bool common = false) where T : new()
    {
        if (common)
        {
            return PlayerPrefs.GetInt(key, -1);
        }
        else
        {
            return PlayerPrefs.GetInt(m_playerId + key, -1);
        }
    }

    /// <summary>
    /// 存储Float类型
    /// </summary>
    public void SaveFloatDate(string key, float num, bool common = false)
    {
        if (common)
        {
            PlayerPrefs.SetFloat(key, num);
        }
        else
        {
            PlayerPrefs.SetFloat(m_playerId + key, num);
        }
        PlayerPrefs.Save();
    }

    /// <summary>
    /// 获取Float类型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="common"></param>
    /// <returns></returns>
    public float GetFloatData(string key, bool common = false)
    {
        if (common)
        {
            return PlayerPrefs.GetFloat(key, -1);
        }
        else
        {
            return PlayerPrefs.GetFloat(m_playerId + key, -1);
        }
    }

    /// <summary>
    /// 存储Bool类型
    /// </summary>
    public void SaveBoolDate(string key, bool boolean, bool common = false)
    {
        if (common)
        {
            PlayerPrefs.SetInt(key, boolean ? 1 : 0);
        }
        else
        {
            PlayerPrefs.SetInt(m_playerId + key, boolean ? 1 : 0);
        }
        PlayerPrefs.Save();
    }

    /// <summary>
    /// 获取Bool类型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="common"></param>
    /// <returns></returns>
    public bool GetBoolData(string key, bool common = false)
    {
        int? boolean = null;
        if (common)
        {
            boolean = PlayerPrefs.GetInt(key, -1);
        }
        else
        {
            boolean = PlayerPrefs.GetInt(m_playerId + key, -1);
        }
        if (boolean == 1) return true;
        else return false;
    }

    /// <summary>
    /// 存储Bool类型
    /// </summary>
    public void SaveStrDate(string key, string str, bool common = false)
    {
        if (common)
        {
            PlayerPrefs.SetString(key, str);
        }
        else
        {
            PlayerPrefs.SetString(m_playerId + key, str);
        }
        PlayerPrefs.Save();
    }

    /// <summary>
    /// 获取Bool类型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="common"></param>
    /// <returns></returns>
    public string GetStrData(string key, bool common = false)
    {
        if (common)
        {
            return PlayerPrefs.GetString(key, "");
        }
        else
        {
            return PlayerPrefs.GetString(m_playerId + key, "");
        }

    }

    #endregion

}

用法如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DataTest : MonoBehaviour {

	// Use this for initialization
	void Start () {
        DataManager.Instance.Init();
        Debug.LogError("金币"+DataManager.Instance.GetCoins());
        Debug.LogError("宝石"+DataManager.Instance.GetGams());
        Debug.LogError("关卡" + DataManager.Instance.GetLevels());

        DataManager.Instance.SetAward(10,20,3);
	}

}

文章来源:https://blog.csdn.net/qq_42194657/article/details/135192470
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。