using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class ComponentConfig
{
public static QualityLevel qualityLevel = QualityLevel.Perfect; // 设置质量(默认最高)
public static bool FullScreen = true; //全屏
public static float TouchZoomSpeed = 0.01f; //缩放速度
public static void GetConfig()
{
//读取配置
JObject res = GetFyData(Application.persistentDataPath + "/Setting.json");
JToken resToken = (JToken)res;
// 扩展JToken ,获取属性值
string version = res.GetValueEx("Version", "");
FullScreen = res.GetValueEx("FullScreen", false);
qualityLevel = (QualityLevel)res.GetValueEx("qualityLevel", 0);
TouchZoomSpeed = res.GetValueEx("TouchZoomSpeed", 0.01f);
}
static void SetFyDataJson(string JsonDataPath, JObject jobj)
{
var path = Path.GetDirectoryName(JsonDataPath);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
Debug.LogError($"缓存数据:{JsonDataPath}");
using (FileStream fsWrite = new FileStream(JsonDataPath, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(jobj));
fsWrite.Write(buffer, 0, buffer.Length);
}
}
static void SetFyDataJson(string JsonDataPath, KeyValuePair<string, JSONNode> data)
{
var path = Path.GetDirectoryName(JsonDataPath);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
JObject jobj = new JObject();
jobj[data.Key] = data.Value.ToString();
Debug.LogError($"缓存数据:{JsonDataPath}");
using (FileStream fsWrite = new FileStream(JsonDataPath, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(jobj));
fsWrite.Write(buffer, 0, buffer.Length);
}
}
/// <summary>
/// 缓存数据到本地
/// </summary>
/// <param name="fileName">文件名</param>
/// <returns></returns>
public static JObject GetFyData(string filePath)
{
string r = "";
if (!File.Exists(filePath))
{
Debug.LogError($"not found config file in path : { filePath}");
SetFyDataJson(filePath, new JObject());
return new JObject();
}
using (System.IO.StreamReader file = System.IO.File.OpenText(filePath))
{
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject o = (JObject)JToken.ReadFrom(reader);
r = o.ToString();
}
}
return JObject.Parse(r);
}
}
public enum QualityLevel
{
Low,
Mid,
High,
Perfect
}
JToken 扩展
using Newtonsoft.Json.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class JObjectExtension
{
public static string GetValueEx(this JToken token, string key, string defaultValue)
{
return (token[key] != null) ? token[key].ToString() : defaultValue;
}
public static float GetValueEx(this JToken token, string key, float defaultValue)
{
return (token[key] != null) ? float.Parse(token[key].ToString()) : defaultValue;
}
public static int GetValueEx(this JToken token, string key, int defaultValue)
{
return (token[key] != null) ? int.Parse(token[key].ToString()) : defaultValue;
}
public static bool GetValueEx(this JToken token, string key, bool defaultValue)
{
return (token[key] != null) ? bool.Parse(token[key].ToString()) : defaultValue;
}
}