using LitJson;
using System.IO;
using UnityEngine;
namespace WS
{
///加载配置文件
public static class ConfigHelper
{
///加载配置文件
public static T LoadConfig(string path)
{
TextAsset asset = Resources.Load(path);
T t = JsonMapper.ToObject(asset.text);
return t;
}
///全路径加载
public static T FileLoadConfig(string path) where T : new()
{
using (StreamReader sr = new StreamReader(path))
{
T config = JsonMapper.ToObject(sr.ReadToEnd());
if (config == null)
{
config = new T();
}
return config;
}
}
/// 保存Config
public static void SaveConfig(object config, string path)
{
using (FileStream fileStream = new FileStream(path, FileMode.Create))
{
byte[] bytes = JsonMapper.ToJson(config).ToByteArray();
fileStream.Write(bytes, 0, bytes.Length);
}
}
/// 文件是否存在 不存在则创建
public static bool ConfigExists(string path)
{
if (File.Exists(path))
{
return true;
}
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
fs.Close();
return true;
}
}
}