using Newtonsoft.Json;
using PublicTools.XMLDataBase;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace XRTool.Util
{
///
/// 读取系统配置
/// 系统配置是json文件,默认会放在steammingpath下面
/// 应用发布时会生成到sd卡目录或者可读写文件下面
///
public class SystemSettingMgr : Singleton
{
private TableInterface table;
public SystemSettings settings;
public SystemSettingMgr()
{
InitSettings();
}
///
/// 实例化Dlg窗口
///
public void InstanceDlg()
{
GameObject dlg = ResourcesManager.Instance.DataLoader.Load("SystemSetDlg") as GameObject;
if (dlg)
{
GameObject obj = GameObject.Instantiate(dlg) as GameObject;
GameNode.Instance.SetParent(ObjNode.WorldCanvas, obj.transform, new Vector3(0, 0, 2), Vector3.zero, new Vector3(0.001f, 0.001f, 0.001f));
}
}
public void OpenSql()
{
if (XSql.Instance.Open(Application.persistentDataPath))
{
table = XSql.Instance.OpenTable(typeof(SystemSettings).Name, ".xml", true);
if (!table.Open())
{
table.Create(typeof(SystemSettings).Name);
}
ReadSetting();
}
else
{
UnityLog.Instance.LogError("数据库打开失败,请检查权限");
}
}
public SystemSettings ReadSetting()
{
settings = table.FindData("SystemSetting");
if (settings == null)
{
settings = new SystemSettings();
table.InsertData(settings);
table.Save();
}
return settings;
}
///
/// 获取配置
///
public void InitSettings()
{
if (table == null)
{
OpenSql();
}
else
{
ReadSetting();
}
SetLogLevel();
}
private void SetLogLevel()
{
#if !UNITY_EDITOR
if (settings != null)
{
UnityLog.Instance.logLevel = settings.LogLevel;
}
#endif
}
public void SaveSettings()
{
if (table == null)
{
OpenSql();
if (table != null)
{
table.UpdateData(this.settings);
table.Save();
}
}
else
{
UnityLog.Instance.Log(JsonConvert.SerializeObject(this.settings));
bool isChange = table.UpdateData(this.settings);
UnityLog.Instance.Log(isChange);
table.Save();
UnityLog.Instance.Log(JsonConvert.SerializeObject(this.settings));
UnityLog.Instance.Log(settings.IfOpenFPS);
}
SetLogLevel();
}
public void SetLogLevel(int level)
{
if (this.settings != null)
{
this.settings.LogLevel = level;
SaveSettings();
}
}
public void SetBoolFPS(bool ifOpenFPS)
{
if (this.settings != null)
{
this.settings.IfOpenFPS = ifOpenFPS;
SaveSettings();
}
}
public void SetBoolLogText(bool ifOpenLog)
{
if (this.settings != null)
{
this.settings.IfOpenLog = ifOpenLog;
SaveSettings();
}
}
public void SetQuilty(int level)
{
if (this.settings != null)
{
QualitySettings.SetQualityLevel(level);
settings.QualityLevel = level;
SaveSettings();
}
}
public void SetShadowType(int shadowType)
{
if (this.settings != null)
{
settings.ShadowType = shadowType;
SaveSettings();
}
}
public void ChangeIPAddress(string ip)
{
if (this.settings != null)
{
settings.Ip = ip;
SaveSettings();
}
}
public void ChangePort(string port)
{
if (this.settings != null)
{
settings.Port = port;
SaveSettings();
}
}
}
}