230 lines
7.5 KiB
C#
230 lines
7.5 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System;
|
|
using Newtonsoft.Json;
|
|
using System.Threading.Tasks;
|
|
using static DeviceQuery;
|
|
using System.Xml.Serialization;
|
|
using UnityEditor;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class GameManager : MonoBehaviour
|
|
{
|
|
static GameManager _inst;
|
|
public static GameManager Inst;
|
|
|
|
public DeviceQuery.Root root;
|
|
/// <summary>
|
|
/// 场景所有机柜
|
|
/// </summary>
|
|
public List<DeviceQuery> Cabinets;
|
|
/// <summary>
|
|
/// 场景所有机框
|
|
/// </summary>
|
|
public List<DeviceQuery> Racks;
|
|
/// <summary>
|
|
/// 场景所有机槽
|
|
/// </summary>
|
|
public List<DeviceQuery> MachineSlots;
|
|
/// <summary>
|
|
/// 场景所有板卡
|
|
/// </summary>
|
|
public List<DeviceQuery> TmsCards;
|
|
|
|
private void Awake()
|
|
{
|
|
if (_inst != null && _inst != this)
|
|
{
|
|
Destroy(this.gameObject);
|
|
}
|
|
else
|
|
{
|
|
_inst = this;
|
|
DontDestroyOnLoad(this.gameObject);
|
|
}
|
|
|
|
}
|
|
private async void OnEnable()
|
|
{
|
|
#region 正式
|
|
//await initAsync("");
|
|
if (root.data == null) return;
|
|
#region 加载场景所有设备
|
|
//加载机柜
|
|
if (Cabinets != null)
|
|
{
|
|
for (int i = 0; i < Cabinets.Count; i++)
|
|
{
|
|
for (int j = 0; j < root.data.Count; j++)
|
|
{
|
|
if (Cabinets[i].deviceList.deviceCode == root.data[j].deviceCode)
|
|
{
|
|
Cabinets[i].deviceList = root.data[j];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//分类获取设备
|
|
foreach (var item in root.data)
|
|
{
|
|
//(机框)
|
|
if (item.type == 2 && item.deviceType == "1")
|
|
{
|
|
DeviceQuery deviceQuery = new DeviceQuery();
|
|
deviceQuery.deviceList = item;
|
|
Racks.Add(deviceQuery);
|
|
}
|
|
//(机槽)
|
|
else if (item.type == 2 && item.deviceType == "2")
|
|
{
|
|
DeviceQuery deviceQuery = new DeviceQuery();
|
|
deviceQuery.deviceList = item;
|
|
MachineSlots.Add(deviceQuery);
|
|
}
|
|
//(板卡)
|
|
else if (item.type == 2 && item.deviceType == "3")
|
|
{
|
|
DeviceQuery deviceQuery = new DeviceQuery();
|
|
deviceQuery.deviceList = item;
|
|
TmsCards.Add(deviceQuery);
|
|
}
|
|
}
|
|
//加载设备
|
|
for (int i = 0; i < Cabinets.Count; i++)
|
|
{
|
|
foreach (var item in root.data)
|
|
{
|
|
if (item.type == 2 && item.deviceType == "1" && item.rackId == Cabinets[i].deviceList.id)
|
|
{
|
|
//获取模型编号
|
|
|
|
//获取U位
|
|
var U = item.devicePosition;
|
|
|
|
GameObject go = Instantiate(Resources.Load<GameObject>("古泉站换流站机房/23"));
|
|
go.transform.SetParent(Cabinets[i].transform.Find("U位/" + U));
|
|
Renderer renderer = go.GetComponentInChildren<Renderer>();
|
|
if (renderer)
|
|
{
|
|
var bounds = renderer.bounds;
|
|
var a = go.AddComponent<BoxCollider>();
|
|
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 = Cabinets[i].transform.Find("U位/" + U).position;
|
|
go.transform.rotation = Cabinets[i].transform.Find("U位/" + U).rotation;
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#endregion
|
|
////Debug.Log(transform.Find("22 (2)").childCount);
|
|
//GameObject go = GameObject.Instantiate(Resources.Load<GameObject>("23 (1)"));
|
|
////go.name = go.name.Replace("(Clone)", "");
|
|
////var deviceQuery = go.AddComponent<DeviceQuery>();
|
|
////deviceQuery.deviceList = root.data[0];
|
|
//go.transform.SetParent(Cabinets[0].transform);
|
|
//Renderer renderer = go.GetComponentInChildren<Renderer>();
|
|
//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);
|
|
}
|
|
|
|
#region MyRegion
|
|
// 将 PlayerPrefs 数据保存为 JSON 文件
|
|
public static void SavePlayerPrefsToJson(string 文件路径)
|
|
{
|
|
Dictionary<string, object> playerPrefsData = new Dictionary<string, object>();
|
|
|
|
// 遍历所有可能的键,将存在的值存储在字典中
|
|
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<string> keys = new List<string>();
|
|
|
|
// 在这里添加所有可能的键,例如:
|
|
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<string, object> playerPrefsData = JsonUtility.FromJson<Dictionary<string, object>>(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
|
|
|
|
|
|
/// <summary>
|
|
/// 查询所有设备
|
|
/// </summary>
|
|
/// <param name="token"></param>
|
|
/// <returns></returns>
|
|
public async Task initAsync(string token)
|
|
{
|
|
var jsonResult = await CombineJSON.GetJson_POST("https://jsonplaceholder.typicode.com/posts", token);
|
|
|
|
root = JsonConvert.DeserializeObject<DeviceQuery.Root>(jsonResult);
|
|
|
|
//return root;
|
|
}
|
|
|
|
}
|