1.具体代码
先贴代码
using LitJson;
using System.IO;
using UnityEngine;
/// <summary>
/// 序列化和反序列化Json时 使用的是哪种方案 有两种 JsonUtility 不能直接序列化字典 ligJson可以序列化字典
/// </summary>
public enum JsonType
{
JsonUtility,
LitJson,
Newtonsoft,
}
/// <summary>
/// Json数据管理类 主要用于进行 Json的序列化存储到硬盘 和 反序列化从硬盘中读取到内存中
/// </summary>
public class JsonMgr : SingleTon<JsonMgr>
{
public JsonMgr()
{
}
//存储Json数据 序列化
public void SaveData(object data, string fileName, string directPath = "", JsonType type = JsonType.Newtonsoft)
{
//确定存储路径
string directoryPath = Application.persistentDataPath + "/" + directPath;
string filePath = directoryPath + fileName + ".json";
//序列化 得到Json字符串
string jsonStr = "";
switch (type)
{
case JsonType.JsonUtility:
jsonStr = JsonUtility.ToJson(data);
break;
case JsonType.LitJson:
jsonStr = JsonMapper.ToJson(data);
break;
case JsonType.Newtonsoft:
jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(data);
break;
}
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
//把序列化的Json字符串 存储到指定路径的文件中
File.WriteAllText(filePath, jsonStr);
}
//读取指定文件中的 Json数据 反序列化
public T LoadData<T>(string filePath, JsonType type = JsonType.Newtonsoft) where T : new()
{
//数据对象
T data = new T();
//确定从哪个路径读取
//首先先判断 默认数据文件夹中是否有我们想要的数据 如果有 就从中获取
string path = Application.streamingAssetsPath + "/" + filePath + ".json";
//先判断 是否存在这个文件
//如果不存在默认文件 就从 读写文件夹中去寻找
if (!File.Exists(path))
path = Application.persistentDataPath + "/" + filePath + ".json";
//如果读写文件夹中都还没有 那就返回一个默认对象
if (!File.Exists(path))
return data;
//进行反序列化
string jsonStr = File.ReadAllText(path);
switch (type)
{
case JsonType.JsonUtility:
data = JsonUtility.FromJson<T>(jsonStr);
break;
case JsonType.LitJson:
data = JsonMapper.ToObject<T>(jsonStr);
break;
case JsonType.Newtonsoft:
data = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(jsonStr);
break;
}
//把对象返回出去
return data;
}
}
2.使用示例
保存玩家数据
// 定义数据类
[Serializable]
public class PlayerData {
public string name;
public int level;
}
// 保存数据
PlayerData data = new PlayerData { name = "Alice", level = 10 };
JsonMgr.Instance.SaveData(data, "player", "Save/", JsonType.Newtonsoft);
加载玩家数据
// 加载数据
PlayerData loadedData = JsonMgr.Instance.LoadData<PlayerData>("Save/player", JsonType.Newtonsoft);
Debug.Log($"Name: {loadedData.name}, Level: {loadedData.level}");
3.核心功能
-
多方案支持
JsonType
枚举:支持三种 JSON 序列化/反序列化方案:Newtonsoft
(Json.NET):功能强大的第三方库,支持复杂类型和更多特性。LitJson
:第三方库,支持字典等复杂类型。JsonUtility
:Unity 内置方案,不支持字典等复杂类型。
-
数据存储与读取
SaveData
方法:将对象序列化为 JSON 字符串并保存到指定路径。LoadData<T>
方法:从指定路径读取 JSON 文件并反序列化为对象。
4.代码结构解析
1. 构造函数
public JsonMgr() { }
- 单例类的默认构造函数,无特殊逻辑。
2. 保存数据方法 SaveData
public void SaveData(object data, string fileName, string directPath = "", JsonType type = JsonType.Newtonsoft)
-
参数说明:
data
:要保存的对象。fileName
:文件名(不含.json
后缀)。directPath
:可选的子目录路径。type
:序列化方案(默认使用Newtonsoft
)。
-
逻辑流程:
- 确定存储路径:
- 使用
Application.persistentDataPath
(可读写路径)构建完整路径。 - 示例:
/Users/xxx/Library/Application Support/Unity/.../directPath/fileName.json
。
- 使用
- 序列化:
- 根据
type
使用对应方案将对象转换为 JSON 字符串。 JsonUtility
:JsonUtility.ToJson(data)
。LitJson
:JsonMapper.ToJson(data)
。Newtonsoft
:JsonConvert.SerializeObject(data)
。
- 根据
- 创建目录:若目录不存在则创建。
- 写入文件:使用
File.WriteAllText(filePath, jsonStr)
存储数据。
- 确定存储路径:
3. 读取数据方法 LoadData<T>
public T LoadData<T>(string filePath, JsonType type = JsonType.Newtonsoft) where T : new()
-
参数说明:
filePath
:文件路径(不含.json
后缀)。type
:反序列化方案(默认使用Newtonsoft
)。- 泛型约束
where T : new()
:确保类型T
有无参构造函数。
-
逻辑流程:
- 优先从只读路径加载:
- 首先检查
Application.streamingAssetsPath
(只读路径,通常用于默认配置文件)。 - 如果文件不存在,尝试从
Application.persistentDataPath
(用户数据路径)加载。
- 首先检查
- 若文件不存在:返回默认的
T
实例(通过new T()
创建)。 - 反序列化:
- 根据
type
使用对应方案将 JSON 字符串转换为对象。 JsonUtility
:JsonUtility.FromJson<T>(jsonStr)
。LitJson
:JsonMapper.ToObject<T>(jsonStr)
。Newtonsoft
:JsonConvert.DeserializeObject<T>(jsonStr)
。
- 根据
- 优先从只读路径加载:
关键点分析
1. 路径选择与平台兼容性
Application.persistentDataPath
:- 可读写路径,适合保存用户数据(如玩家进度、配置等)。
Application.streamingAssetsPath
:- 只读路径,通常用于打包后的默认资源(如初始配置文件)。
- 注意:在某些平台(如 Android)上,
File.ReadAllText
无法直接访问此路径的文件,需通过UnityWebRequest
或WWW
加载。当前代码可能在此类平台上抛出异常。
2. 默认值处理
- 若文件不存在,返回
new T()
创建的默认对象。适用于需要保证最小功能的场景(如游戏配置加载失败时使用默认值)。
3. 异常处理缺失
- 当前代码未处理文件读写或序列化/反序列化过程中可能出现的异常(如 IO 错误、格式错误)。建议添加
try-catch
块并记录日志。
4. 性能优化建议
- 异步操作:大量数据读写可能阻塞主线程,可改用异步方法(如
File.ReadAllTextAsync
/File.WriteAllTextAsync
)。 - 缓存机制:对频繁读取的数据(如配置文件),可考虑缓存反序列化后的对象。