Tz2/Assets/Zion/Scripts/配送/Manager/MyJsonManager.cs

69 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using TMPro;
using UnityEngine;
public static class MyJsonManager
{
/// <summary>
/// 保存数据 PC端
/// </summary>
/// <param name="data">需要存储的数据对象</param>
/// <param name="fileName">存储为的文件名</param>
/// <param name="type">存储方式默认LitJson</param>
public static void SaveData(object data, string fileName)
{
string path = Application.streamingAssetsPath + "/" + fileName + ".json";
string json = "";
json = JsonConvert.SerializeObject(data);
File.WriteAllText(path, json);
}
/// <summary>
/// 读取数据 PC端
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="path"></param>
/// <returns></returns>
public static T LoadData<T>(string fileName) where T : new()
{
string path = Application.streamingAssetsPath + "/" + fileName + ".json";
if (!File.Exists(path))
{
path = Application.persistentDataPath + "/" + fileName + ".json";
}
if (!File.Exists(path))
{
return new T();
}
string jsonStr = File.ReadAllText(path);
T data = new T();
data = JsonConvert.DeserializeObject<T>(jsonStr);
return data;
}
/// <summary>
/// 加载数据列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fileName"></param>
/// <returns></returns>
public static List<T> LoadListData<T>(string fileName) where T : new()
{
string path = Application.streamingAssetsPath + "/" + fileName + ".json";
if (!File.Exists(path))
{
path = Application.persistentDataPath + "/" + fileName + ".json";
}
if (!File.Exists(path))
{
return new List<T>();
}
string jsonStr = File.ReadAllText(path);
List<T> data = new List<T>();
data = JsonConvert.DeserializeObject<List<T>>(jsonStr);
return data;
}
}