using AX.MessageSystem; using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; [Serializable] public class DeviceCloneData { public string name; public int floorId; public DeviceType type; public Vector3 postion; } public class DeviceObjManager : MonoBehaviour { public static DeviceObjManager Instance; public GameObject Point; public List NowPointList = new List(); private string objId = "objlistid"; private void Awake() { MessageDispatcher.AddListener("ConfigLoadOver", ConfigLoadOver); Instance = this; } private void Start() { } private void OnDestroy() { MessageDispatcher.RemoveListener("ConfigLoadOver", ConfigLoadOver); } private void ConfigLoadOver(IMessage obj) { LoadDeviceObjs(); } private void Update() { if (Input.GetKey(KeyCode.Q)) { if (Input.GetKeyDown(KeyCode.Delete)) { if (DevicePanelManager.Instance.MenuType == DeviceType.空) { NowPointList.Clear(); SaveObjs(); return; } for (int i = 0; i < NowPointList.Count; i++) { if (NowPointList[i].type == DevicePanelManager.Instance.MenuType) { NowPointList.RemoveAt(i); i--; } } SaveObjs(); } } } public DeviceObj GetDeviceByName(string dname) { DeviceObj result = null; foreach (Transform item in transform) { if (item.name == dname) { result = item.GetComponent(); break; } } return result; } public void AddObj(string objName) { foreach (Transform item in transform) { if (item.name == objName && item.GetComponent()) { var obj = new DeviceCloneData(); obj.name = item.name; obj.postion = item.position; obj.floorId = item.GetComponent().floorId; obj.type = item.GetComponent().BindType; bool has = false; foreach (var data in NowPointList) { if (data.name == obj.name) { data.postion = obj.postion; data.type = obj.type; has = true; } } if (!has) { NowPointList.Add(obj); } break; } } SaveObjs(); } public void DelObj(string objName) { foreach (var item in DevicePanelManager.Instance.BindObjectList) { if (item.ObjName == objName) { item.ObjName = ""; break; } } DevicePanelManager.Instance.SaveDeviceObjConfig(); for (int i = 0; i < NowPointList.Count; i++) { if (NowPointList[i].name == objName) { NowPointList.Remove(NowPointList[i]); break; } } SaveObjs(); } public void SaveObjs() { var json = JsonConvert.SerializeObject(NowPointList); string url = string.Format(HttpManager.Instance.PostIOilTankById, objId); if (Application.platform == RuntimePlatform.WebGLPlayer) { url = url.Substring(url.IndexOf("api") - 1); } HttpManager.Instance.Post(url, json, () => { Debug.Log("保存点位成功!"); }); } private void LoadDeviceObjs() { try { string url = string.Format(HttpManager.Instance.GetOilTankById, objId); Debug.Log($"the url is :{ url}"); if (Application.platform == RuntimePlatform.WebGLPlayer) { url = url.Substring(url.IndexOf("api") - 1); } HttpManager.Instance.Get(url, d => { NowPointList = JsonConvert.DeserializeObject>(d); foreach (Transform item in transform) { Destroy(item.gameObject); } foreach (var item in NowPointList) { GameObject go = Instantiate(Point, item.postion, Quaternion.identity, transform); go.name = item.name; go.GetComponent().CreateIcon(item.type, item.floorId); SelectionManager.Instance.Sets.Add(go); } }); } catch { Debug.LogError("暂无数据!"); } } }