using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Unity.VisualScripting; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UIElements; using static DeviceQuery; using static GameManager; using static PortQuery; public class GameManager : MonoBehaviour { static GameManager _inst; public static GameManager Inst => _inst; /// /// 接口地址 /// public webURL Jk_URL; /// /// 登录后返回 /// public string token; [Header("设备-查询接口")] public DeviceQuery.Root root_AllDevice; ///// ///// 场景所有机柜 ///// // public List Cabinets_go; /// /// 场景所有机柜信息 /// [Header("场景所有机柜")] public List Cabinets; /// /// 场景所有机框 /// [Header("场景所有机框信息")] public List Racks_go; /// /// 场景所有机框信息 /// public List Racks; /// /// 场景所有机槽 /// [Header("场景所有机槽信息")] public List MachineSlots_go; /// /// 场景所有机槽信息 /// public List MachineSlots; /// /// 场景所有板卡 /// [Header("场景所有板卡信息")] public List TmsCards_go; /// /// 场景所有板卡信息 /// public List TmsCards; [Header(""), Header("设备-查询接口")] public PortQuery.Root root_AllPort; /// /// 场景所有端口 /// [Header("场景所有端口信息")] public List TmsPorts_go; /// /// 场景所有端口信息 /// public List TmsPorts; private void Awake() { if (_inst != null && _inst != this) { Destroy(this.gameObject); } else { _inst = this; DontDestroyOnLoad(this.gameObject); } Jk_URL = new webURL(); } public IEnumerator dsaklj() { yield return StartCoroutine(LoadJSON(Application.streamingAssetsPath + "/设备查询.json")); #region 正式 deleteInit(); if (root_AllDevice.data == null) yield break; #region 加载场景所有设备 //读取机柜配置 if (Cabinets != null) { for (int i = 0; i < Cabinets.Count; i++) { for (int j = 0; j < root_AllDevice.data.Count; j++) { if (Cabinets[i].deviceList.deviceCode == root_AllDevice.data[j].deviceCode && root_AllDevice.data[j].type == "1") { Cabinets[i].deviceList = root_AllDevice.data[j]; break; } } } } //设备信息分类-保存 foreach (var item in root_AllDevice.data) { //(机框) if (item.type == "2" && item.deviceType == "1") { Racks.Add(item); } //(机槽) else if (item.type == "2" && item.deviceType == "2") { MachineSlots.Add(item); } //(板卡) else if (item.type == "2" && item.deviceType == "3") { TmsCards.Add(item); } } //动态加载设备(机框) for (int i = 0; i < Racks.Count; i++) { foreach (var item in Cabinets) { /*if (item.deviceType == "1") { }*/ if (item.deviceList.id == Racks[i].rackId) { //获取模型编号 string modelNum = Racks[i].modelNum; //获取U位 var U = Racks[i].devicePosition; GameObject go; try { go = Instantiate(Resources.Load("古泉站换流站机房/" + modelNum)); } catch (Exception e) { Debug.Log("模型生成错误:" + e.Message); break; } //写入机框配置 go.GetComponent().deviceList = Racks[i]; Racks_go.Add(go); go.transform.SetParent(item.transform.Find("U位/" + U)); Renderer renderer = go.GetComponentInChildren(); if (renderer) { var bounds = renderer.bounds; var a = go.AddComponent(); a.isTrigger = false; a.center = bounds.center - go.transform.position; a.size = bounds.size; a.transform.rotation = Quaternion.identity; //重置其旋转为默认值 } Vector3 center = renderer.bounds.center; go.transform.position = item.transform.Find("U位/" + U).position; go.transform.rotation = item.transform.Find("U位/" + U).rotation; break; } } } //动态加载设备(机槽) for (int i = 0; i < Racks_go.Count; i++)//遍历机框 { //机框下所有设备 var jc = Racks_go[i].transform.GetComponentsInChildren().ToList(); for (int j = 0; j < jc.Count; j++) { //如果是机槽 if (jc[j].deviceList.deviceType == "2") { MachineSlots_go.Add(jc[j].gameObject); } } } //读取机槽配置 for (int i = 0; i < MachineSlots_go.Count; i++) { DeviceQuery DP; DP = MachineSlots_go[i].transform.parent.GetComponent();//机槽父物体 if (!DP) continue; foreach (var item in MachineSlots) { //机槽在机框里 /*if (item.deviceType == "2") { }*/ if (DP.deviceList.deviceType == "1" && item.shelfId == DP.deviceList.id) { MachineSlots_go[i].GetComponent().deviceList = item; break; } } } //动态加载设备(板卡) for (int i = 0; i < MachineSlots_go.Count; i++)//遍历机槽 { //机槽下所有设备 var bk = MachineSlots_go[i].transform.GetComponentsInChildren().ToList(); for (int j = 0; j < bk.Count; j++) { //如果是板卡 if (bk[j].deviceList.deviceType == "3") { TmsCards_go.Add(bk[j].gameObject); } } } for (int i = 0; i < Racks_go.Count; i++)//遍历机框 { //机框下所有设备 var bk = Racks_go[i].transform.GetComponentsInChildren().ToList(); for (int j = 0; j < bk.Count; j++) { //如果是板卡 if (bk[j].deviceList.deviceType == "3") { TmsCards_go.Add(bk[j].gameObject); } } } //读取板卡配置 for (int i = 0; i < TmsCards_go.Count; i++) { DeviceQuery DP; DeviceQuery D; DP = TmsCards_go[i].transform.parent.GetComponent();//板卡父物体 //D = TmsCards_go[i].transform.GetComponent();//板卡 if (!DP) continue; foreach (var item in TmsCards) { /*if (item.deviceType == "3") { }*/ //板卡在机槽里 if (DP.deviceList.deviceType == "2" && item.slotId == DP.deviceList.id) { TmsCards_go[i].GetComponent().deviceList = item; break; } //板卡在机框里 else if (DP.deviceList.deviceType == "1" && item.shelfId == DP.deviceList.id) { TmsCards_go[i].GetComponent().deviceList = item; break; } } } /***********************************************************/ //端口信息-保存 foreach (var item in root_AllPort.data) { TmsPorts.Add(item); } //动态加载设备(端口) for (int i = 0; i < TmsCards_go.Count; i++)//遍历板卡 { //板卡下所有设备 var dk = TmsCards_go[i].transform.GetComponentsInChildren().ToList(); for (int j = 0; j < dk.Count; j++) { TmsPorts_go.Add(dk[j].gameObject); } } for (int i = 0; i < Racks_go.Count; i++)//遍历机框 { //机框下所有设备 var dk = Racks_go[i].transform.GetComponentsInChildren().ToList(); for (int j = 0; j < dk.Count; j++) { TmsPorts_go.Add(dk[j].gameObject); } } //读取端口配置 for (int i = 0; i < TmsPorts_go.Count; i++) { DeviceQuery DP; DP = TmsPorts_go[i].transform.parent.GetComponent(); if (!DP) continue; foreach (var item in TmsPorts) { if (item.deviceId == DP.deviceList.id) { TmsPorts_go[i].GetComponent().portList = item; break; } } } #endregion #endregion } private async void OnEnable() { //StartCoroutine(dsaklj());//测试 //return;//测试 #region 正式 deleteInit(); await initAsync();//正式 if (root_AllDevice.data == null) return; #region 加载场景所有设备 //读取机柜配置 if (Cabinets != null) { for (int i = 0; i < Cabinets.Count; i++) { for (int j = 0; j < root_AllDevice.data.Count; j++) { if (Cabinets[i].deviceList.deviceCode == root_AllDevice.data[j].deviceCode && root_AllDevice.data[j].type == "1") { Cabinets[i].deviceList = root_AllDevice.data[j]; break; } } } } //设备信息分类-保存 foreach (var item in root_AllDevice.data) { //(机框) if (item.type == "2" && item.deviceType == "1") { Racks.Add(item); } //(机槽) else if (item.type == "2" && item.deviceType == "2") { MachineSlots.Add(item); } //(板卡) else if (item.type == "2" && item.deviceType == "3") { TmsCards.Add(item); } } //动态加载设备(机框) for (int i = 0; i < Racks.Count; i++) { foreach (var item in Cabinets) { /*if (item.deviceType == "1") { }*/ if (item.deviceList.id == Racks[i].rackId) { //获取模型编号 string modelNum = Racks[i].modelNum; //获取U位 var U = Racks[i].devicePosition; GameObject go; try { go = Instantiate(Resources.Load("古泉站换流站机房/" + modelNum)); } catch (Exception e) { Debug.Log("模型生成错误:" + e.Message); break; } //写入机框配置 go.GetComponent().deviceList = Racks[i]; Racks_go.Add(go); go.transform.SetParent(item.transform.Find("U位/" + U)); Renderer renderer = go.GetComponentInChildren(); if (renderer) { var bounds = renderer.bounds; var a = go.AddComponent(); a.isTrigger = false; a.center = bounds.center - go.transform.position; a.size = bounds.size; a.transform.rotation = Quaternion.identity; //重置其旋转为默认值 } Vector3 center = renderer.bounds.center; go.transform.position = item.transform.Find("U位/" + U).position; go.transform.rotation = item.transform.Find("U位/" + U).rotation; break; } } } //动态加载设备(机槽) for (int i = 0; i < Racks_go.Count; i++)//遍历机框 { //机框下所有设备 var jc = Racks_go[i].transform.GetComponentsInChildren().ToList(); for (int j = 0; j < jc.Count; j++) { //如果是机槽 if (jc[j].deviceList.deviceType == "2") { MachineSlots_go.Add(jc[j].gameObject); } } } //读取机槽配置 for (int i = 0; i < MachineSlots_go.Count; i++) { DeviceQuery DP; DP = MachineSlots_go[i].transform.parent.GetComponent();//机槽父物体 if (!DP) continue; foreach (var item in MachineSlots) { //机槽在机框里 /*if (item.deviceType == "2") { }*/ if (DP.deviceList.deviceType == "1" && item.shelfId == DP.deviceList.id) { MachineSlots_go[i].GetComponent().deviceList = item; break; } } } //动态加载设备(板卡) for (int i = 0; i < MachineSlots_go.Count; i++)//遍历机槽 { //机槽下所有设备 var bk = MachineSlots_go[i].transform.GetComponentsInChildren().ToList(); for (int j = 0; j < bk.Count; j++) { //如果是板卡 if (bk[j].deviceList.deviceType == "3") { TmsCards_go.Add(bk[j].gameObject); } } } for (int i = 0; i < Racks_go.Count; i++)//遍历机框 { //机框下所有设备 var bk = Racks_go[i].transform.GetComponentsInChildren().ToList(); for (int j = 0; j < bk.Count; j++) { //如果是板卡 if (bk[j].deviceList.deviceType == "3") { TmsCards_go.Add(bk[j].gameObject); } } } //读取板卡配置 for (int i = 0; i < TmsCards_go.Count; i++) { DeviceQuery DP; DeviceQuery D; DP = TmsCards_go[i].transform.parent.GetComponent();//板卡父物体 //D = TmsCards_go[i].transform.GetComponent();//板卡 if (!DP) continue; foreach (var item in TmsCards) { /*if (item.deviceType == "3") { }*/ //板卡在机槽里 if (DP.deviceList.deviceType == "2" && item.slotId == DP.deviceList.id) { TmsCards_go[i].GetComponent().deviceList = item; break; } //板卡在机框里 else if (DP.deviceList.deviceType == "1" && item.shelfId == DP.deviceList.id) { TmsCards_go[i].GetComponent().deviceList = item; break; } } } /***********************************************************/ //端口信息-保存 foreach (var item in root_AllPort.data) { TmsPorts.Add(item); } //动态加载设备(端口) for (int i = 0; i < TmsCards_go.Count; i++)//遍历板卡 { //板卡下所有设备 var dk = TmsCards_go[i].transform.GetComponentsInChildren().ToList(); for (int j = 0; j < dk.Count; j++) { TmsPorts_go.Add(dk[j].gameObject); } } for (int i = 0; i < Racks_go.Count; i++)//遍历机框 { //机框下所有设备 var dk = Racks_go[i].transform.GetComponentsInChildren().ToList(); for (int j = 0; j < dk.Count; j++) { TmsPorts_go.Add(dk[j].gameObject); } } //读取端口配置 for (int i = 0; i < TmsPorts_go.Count; i++) { DeviceQuery DP; DP = TmsPorts_go[i].transform.parent.GetComponent(); if (!DP) continue; foreach (var item in TmsPorts) { if (item.deviceId == DP.deviceList.id) { TmsPorts_go[i].GetComponent().portList = item; break; } } } #endregion #endregion ////Debug.Log(transform.Find("22 (2)").childCount); //GameObject go = GameObject.Instantiate(Resources.Load("23 (1)")); ////go.name = go.name.Replace("(Clone)", ""); ////var deviceQuery = go.AddComponent(); ////deviceQuery.deviceList = root.data[0]; //go.transform.SetParent(Cabinets[0].transform); //Renderer renderer = go.GetComponentInChildren(); //Vector3 center = renderer.bounds.center; //go.transform.position = Cabinets[0].transform.position; //go.transform.rotation = Cabinets[0].transform.rotation; ////go.transform.position = new Vector3(go.transform.position.x, go.transform.position.y- center.y, go.transform.position.z); } /// /// 场景模型数据初始化 /// void deleteInit() { //Cabinets_go.Clear(); for (int i = 0; i < Racks_go.Count; i++) { DestroyImmediate(Racks_go[i]); } for (int i = 0; i < MachineSlots_go.Count; i++) { DestroyImmediate(MachineSlots_go[i]); } for (int i = 0; i < TmsCards_go.Count; i++) { DestroyImmediate(TmsCards_go[i]); } for (int i = 0; i < TmsPorts_go.Count; i++) { DestroyImmediate(TmsPorts_go[i]); } Racks_go.Clear(); Racks.Clear(); MachineSlots_go.Clear(); MachineSlots.Clear(); TmsCards_go.Clear(); TmsCards.Clear(); TmsPorts_go.Clear(); TmsPorts.Clear(); } IEnumerator LoadJSON(string url) { UnityWebRequest www = UnityWebRequest.Get(url); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.Log(www.error); } else { string jsonData = www.downloadHandler.text; // 解析JSON数据 ParseJSON(jsonData); } } void ParseJSON(string jsonString) { // 使用JsonUtility类解析JSON数据到自定义的类对象 root_AllDevice = JsonConvert.DeserializeObject(jsonString); // 现在你可以访问dataObject中的数据了 //Debug.Log("Name: " + dataObject.name); //Debug.Log("Age: " + dataObject.age); } #region MyRegion // 将 PlayerPrefs 数据保存为 JSON 文件 public static void SavePlayerPrefsToJson(string 文件路径) { Dictionary playerPrefsData = new Dictionary(); // 遍历所有可能的键,将存在的值存储在字典中 foreach (string key in GetAllPossibleKeys()) { if (PlayerPrefs.HasKey(key)) { if (PlayerPrefs.GetInt(key) == 1) playerPrefsData.Add(key, PlayerPrefs.GetInt(key)); else if (PlayerPrefs.GetFloat(key) != 0f) playerPrefsData.Add(key, PlayerPrefs.GetFloat(key)); else playerPrefsData.Add(key, PlayerPrefs.GetString(key)); } } // 将字典转换为 JSON 字符串 string jsonData = JsonUtility.ToJson(playerPrefsData, true); // 使用 Application.persistentDataPath 保存 JSON 数据到指定文件路径 string 完整路径 = Application.persistentDataPath + "/" + 文件路径; PlayerPrefs.SetString(文件路径, jsonData); } // 获取所有可能的 PlayerPrefs 键 private static string[] GetAllPossibleKeys() { List keys = new List(); // 在这里添加所有可能的键,例如: keys.Add("HighScore"); keys.Add("PlayerName"); // ... return keys.ToArray(); } // 从 JSON 文件中读取数据并设置到 PlayerPrefs public static void LoadPlayerPrefsFromJson(string 文件路径) { // 检查 PlayerPrefs 中是否存在 JSON 数据 if (PlayerPrefs.HasKey(文件路径)) { // 从 PlayerPrefs 中获取 JSON 数据 string jsonData = PlayerPrefs.GetString(文件路径); // 将 JSON 字符串转换为字典 Dictionary playerPrefsData = JsonUtility.FromJson>(jsonData); // 将字典中的值设置到 PlayerPrefs foreach (var kvp in playerPrefsData) { string key = kvp.Key; object value = kvp.Value; if (value is int intValue) PlayerPrefs.SetInt(key, intValue); else if (value is float floatValue) PlayerPrefs.SetFloat(key, floatValue); else if (value is string stringValue) PlayerPrefs.SetString(key, stringValue); } } else { Debug.LogError("在 PlayerPrefs 中未找到指定文件的 JSON 数据:" + 文件路径); } } #endregion /// /// 查询所有设备 /// /// /// public async Task initAsync() { var jsonResult = await CombineJSON.GetJson_POST(Jk_URL.sb_cx, token); root_AllDevice = JsonConvert.DeserializeObject(jsonResult); var jsonResult1 = await CombineJSON.GetJson_POST(Jk_URL.dk_cx, token); root_AllPort = JsonConvert.DeserializeObject(jsonResult1); //return root; } /// /// 接口地址 /// [System.Serializable] public class webURL { /// /// 设备-查询URL /// [Header("设备-查询URL")] public string sb_cx; /// /// 设备-新增URL /// [Header("设备-新增URL")] public string sb_xz; /// /// 设备-删除URL /// [Header("设备-删除URL")] public string sb_sc; /// /// 设备-编辑URL /// [Header("设备-编辑URL")] public string sb_bj; /// /// 端口-查询URL /// [Header("端口-查询URL")] public string dk_cx; /// /// 端口-新增URL /// [Header("端口-新增URL")] public string dk_xz; /// /// 端口-删除URL /// [Header("端口-删除URL")] public string dk_sc; /// /// 端口-编辑URL /// [Header("端口-编辑URL")] public string dk_bj; /// /// 环境-查询URL /// [Header("环境-查询URL")] public string hj_cx; /// /// 告警-查询URL /// [Header("告警-查询URL")] public string gj_cx; /// /// 工作票-查询URL /// [Header("工作票-查询URL")] public string gzp_cx; /// /// 检修票-查询URL /// [Header("检修票-查询URL")] public string jxp_cx; public webURL() { string ct = "http://k7bbvt.natappfree.cc/"; sb_cx = ct + "machineRoom/device/queryList"; sb_xz = ct + "machineRoom/device/save"; sb_sc = ct + "machineRoom/device/delete"; sb_bj = ct + "machineRoom/device/updateById"; dk_cx = ct + "machineRoom/port/queryList"; dk_xz = ct + "machineRoom/port/save"; dk_sc = ct + "machineRoom/port/delete"; dk_bj = ct + "machineRoom/port/updateById"; hj_cx = ct + "machineRoom/tmsPoint/queryList"; gj_cx = ct + "machineRoom/tmsAlarm/queryList"; gzp_cx = ct + "machineRoom/tmsWorkTicket/queryList"; jxp_cx = ct + "machineRoom/tmsOverhaulTicket/queryList"; } } }