MachineRoomManager.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. using System.Linq;
  2. using Newtonsoft.Json;
  3. using SC.XR.Unity.Module_InputSystem;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. using UnityEngine.XR;
  11. using Ximmerse.XR.Tag;
  12. using Newtonsoft.Json.Linq;
  13. using LitJson;
  14. /// <summary>
  15. /// 大场景生成和Tag管理
  16. /// </summary>
  17. public class MachineRoomManager : MonoSingleton<MachineRoomManager>
  18. {
  19. public MachineRoomData data;
  20. public GameObject cabinetObj;
  21. public GameObject bladeServer;
  22. public bool isInit = false;
  23. public MachineRoomTagData tagData;
  24. public List<EditorTag> listEditorTag;
  25. GameObject tagObjPoint;
  26. private List<BladeServerItem> listBSItem;
  27. public TagTracking foundTracking;
  28. public GameObject cubeY;
  29. public Transform leftLine;
  30. public Transform rightLine;
  31. bool isSelect = false;
  32. private Transform selectTrigger;
  33. private Vector3 selectTOldPos;
  34. private Vector3 selectTOldRot;
  35. public bool isDataInit = false;
  36. public Transform startShowDevice;
  37. /// <summary>
  38. /// 临时创建 的tagid
  39. /// </summary>
  40. private List<int> listTagid;
  41. /// <summary>
  42. /// 临时创建 刀片机信息
  43. /// </summary>
  44. private DeviceParameters dp1;
  45. private DeviceParameters dp2;
  46. /// <summary>
  47. /// 机房列表
  48. /// </summary>
  49. public List<string> listRoomName;
  50. /// <summary>
  51. /// 在机房内的位置
  52. /// </summary>
  53. public Transform player;
  54. public void Init()
  55. {
  56. tagObjPoint = new GameObject();
  57. listBSItem = new List<BladeServerItem>();
  58. listRoomName = new List<string>();
  59. listRoomName.Add("4A-23");
  60. listRoomName.Add("4A-24");
  61. listRoomName.Add("4A-25");
  62. listRoomName.Add("4A-26");
  63. listRoomName.Add("4A-27");
  64. listTagid = new List<int>();
  65. listTagid.Add(6);
  66. listTagid.Add(15);
  67. listTagid.Add(26);
  68. listTagid.Add(27);
  69. listTagid.Add(28);
  70. listTagid.Add(30);
  71. listTagid.Add(31);
  72. listTagid.Add(32);
  73. listTagid.Add(35);
  74. listTagid.Add(36);
  75. listTagid.Add(38);
  76. listTagid.Add(41);
  77. //listTagid.Add(42);
  78. //listTagid.Add(43);
  79. //listTagid.Add(44);
  80. //listTagid.Add(50);
  81. //listTagid.Add(53);
  82. //listTagid.Add(55);
  83. //listTagid.Add(58);
  84. //listTagid.Add(61);
  85. //listTagid.Add(62);
  86. //listTagid.Add(64);
  87. //listTagid.Add(31);
  88. //listTagid.Add(61);
  89. //listTagid.Add(64);
  90. //listTagid.Add(43);
  91. //listTagid.Add(3161);
  92. //listTagid.Add(3164);
  93. //listTagid.Add(4361);
  94. //listTagid.Add(4364);
  95. #region 原初始化
  96. // //PlayerPrefs.DeleteAll();
  97. // if (PlayerPrefs.HasKey("MachineRoomData"))
  98. // {
  99. // string msg = PlayerPrefs.GetString("MachineRoomData");
  100. // UILogManager.Instance.text1.text = msg;
  101. // Debug.Log("MachineRoomData " + msg);
  102. // data = JsonConvert.DeserializeObject<MachineRoomData>(msg);
  103. // // Debug.Log("PlayerPrefs.HasKey(MachineRoomData) ");
  104. // //data = new MachineRoomData();
  105. // //data.line = 4;
  106. // //data.list = 3;
  107. // //data.intervalW = 1;
  108. // //data.intervalL = 2;
  109. // //data.cabinetW = 1.2f;
  110. // //data.cabinetH = 2f;
  111. // //data.bladeSeverNumber = 4;
  112. // //data.bIntervalH = 0.2f;
  113. // //data.firstBladeSeverH = 0.4f;
  114. // //data.TagOffestPosY = 0;
  115. // //data.CameraFar = 3;
  116. // //data.roomName = "4A-23";
  117. // //data.totalCapacity = 8520;
  118. // //data.usedCapacity = 4420;
  119. // //data.totalPower = 1278;
  120. // //data.usedPower = 846.24f;
  121. // //data.totalLoadBearing = 85.2f;
  122. // //data.usedBearing = 46.83f;
  123. // }
  124. // else
  125. // {
  126. // data = new MachineRoomData();
  127. // data.line = 4;
  128. // data.list = 3;
  129. // data.intervalW = 0.1f;
  130. // data.intervalL = 1f;
  131. // data.cabinetW = 0.6f;
  132. // data.cabinetH = 2f;
  133. // data.bladeSeverNumber = 4;
  134. // data.bIntervalH = 0.2f;
  135. // data.firstBladeSeverH = 0.4f;
  136. // data.tagOffestPosY = 1f;
  137. // data.cameraFar = 10;
  138. // data.roomName = "4A-23";
  139. // data.totalCapacity = 8520;
  140. // data.usedCapacity = 4420;
  141. // data.totalPower = 1278;
  142. // data.usedPower = 846.24f;
  143. // data.totalLoadBearing = 85.2f;
  144. // data.usedBearing = 46.83f;
  145. // }
  146. // // data.listCabinetData = null;
  147. // data.listEquipmentData = null;
  148. // data.listTagData = null;
  149. //// PlayerPrefs.DeleteAll();
  150. // if (PlayerPrefs.HasKey("MachineRoomTagData"))
  151. // {
  152. // string msg = PlayerPrefs.GetString("MachineRoomTagData");
  153. // Debug.Log("MachineRoomTagData " + msg);
  154. // data.listTagData = JsonConvert.DeserializeObject<List<MachineRoomTagData>>(msg);
  155. // }
  156. // else
  157. // {
  158. // data.listTagData = new List<MachineRoomTagData>();
  159. // MachineRoomTagData data1 = new MachineRoomTagData();
  160. // data1.id = 65;
  161. // //data1.pos = new Vector3(4.5f, 0, 0);
  162. // data1.pos = Vector3.zero;
  163. // data1.rot = new Vector3(0, 180, 0);
  164. // data1.offestPos = Vector3.zero;
  165. // data1.offestrot = Vector3.zero;
  166. // MachineRoomTagData data4 = new MachineRoomTagData();
  167. // data4.id = 141;
  168. // // data4.pos = new Vector3(4.5f, 2.5f, 3.7f);
  169. // data4.pos = Vector3.zero;
  170. // data4.rot = new Vector3(0, 180, 0);
  171. // data4.offestPos = Vector3.zero;
  172. // data4.offestrot = Vector3.zero;
  173. // MachineRoomTagData data5 = new MachineRoomTagData();
  174. // data5.id = 142;
  175. // // data5.pos = new Vector3(4.5f, 2.5f, 3.7f);
  176. // data5.pos = Vector3.zero;
  177. // data5.rot = new Vector3(0, 180, 0);
  178. // data5.offestPos = Vector3.zero;
  179. // data5.offestrot = Vector3.zero;
  180. // data.listTagData.Add(data1);
  181. // data.listTagData.Add(data4);
  182. // data.listTagData.Add(data5);
  183. // }
  184. // if (PlayerPrefs.HasKey("MachineRoomCabinetData"))
  185. // {
  186. // string msg = PlayerPrefs.GetString("MachineRoomCabinetData");
  187. // // data.listCabinetData = JsonConvert.DeserializeObject<List<MachineRoomCabinetData>>(msg);
  188. // data.listEquipmentData = JsonConvert.DeserializeObject<List<MachineParameters>>(msg);
  189. // }
  190. #endregion
  191. }
  192. private void Start()
  193. {
  194. Init();
  195. //Init();
  196. // Init();
  197. #if UNITY_EDITOR
  198. // Init();
  199. #endif
  200. }
  201. public void SceneInitCallBack(string msg)
  202. {
  203. Debug.Log(msg);
  204. JObject json = JObject.Parse(msg);
  205. UILogManager.Instance.text1.text = msg;
  206. if (json["code"].ToString() == "200")
  207. {
  208. string data = json["data"]["room"].ToString();
  209. this.data = JsonConvert.DeserializeObject<MachineRoomData>(data);
  210. ShowRoomName.Instance.roomName.text = this.data.roomName;
  211. UICallManager.Instance.MachineRoomInit();
  212. }
  213. else
  214. {
  215. Debug.LogError(" 请求场景参数出错 !!! " + HttpActionLang.sceneInit);
  216. }
  217. }
  218. public void SceneInit()
  219. {
  220. SettingMachineRoomData.Instance.Init(data);
  221. OpenXRCamera.Instance.head.GetComponent<Camera>().farClipPlane = data.cameraFar;
  222. MachineInit();
  223. // ServerInit();
  224. UICallManager.Instance.ShowDeviceTransform(startShowDevice);
  225. isDataInit = true;
  226. }
  227. private void Update()
  228. {
  229. if (state && tagData != null)
  230. {
  231. for (int i = 0; i < listEditorTag.Count; i++)
  232. {
  233. listEditorTag[i].gameObject.SetActive(!state);
  234. if (listEditorTag[i].id == tagData.id)
  235. listEditorTag[i].gameObject.SetActive(true);
  236. }
  237. }
  238. if (tagData != null && foundTracking != null && DragManager.Instance.triggerState == TriggerState.No && !state)
  239. {
  240. tagObjPoint.transform.SetParent(foundTracking.transform);
  241. tagObjPoint.transform.localEulerAngles = new Vector3(0, tagData.offestrot == Vector3.zero ? tagData.rot.y : tagData.offestrot.y, 0);
  242. tagObjPoint.transform.localPosition = tagData.offestPos == Vector3.zero ? tagData.pos : tagData.offestPos;
  243. // Debug.Log(" MachineRoom T " + tagData.offestPos);
  244. transform.eulerAngles = new Vector3(0, tagObjPoint.transform.eulerAngles.y, 0);
  245. transform.position = new Vector3(tagObjPoint.transform.position.x, foundTracking.transform.position.y + data.tagOffestPosY, tagObjPoint.transform.position.z);
  246. }
  247. if (Input.GetKeyDown(KeyCode.L))
  248. {
  249. OnTrackingFound(foundTracking);
  250. }
  251. }
  252. public List<MachineObj> listObjes = new List<MachineObj>();
  253. public void MachineInit()
  254. {
  255. //listObjes.Clear();
  256. //listObjes = new List<MachineObj>();
  257. //// 重新生成机柜组
  258. //if(data.listEquipmentData == null||data.listEquipmentData.Count!=(data.line*data.list))
  259. //{
  260. // data.listEquipmentData = new List<MachineParameters>();
  261. // for (int i = 0; i < data.line; i++)
  262. // {
  263. // for (int j = 0; j < data.list; j++)
  264. // {
  265. // MachineParameters mp1 = new MachineParameters();
  266. // mp1.deviceId = (i * data.list + j + 1).ToString();
  267. // mp1.tagID = -1000;
  268. // // mp1.offsetPos = new Vector3(-0.4f, 0, -0.3f);
  269. // mp1.offsetPos = Vector3.zero;
  270. // mp1.eulerAxle = 3;
  271. // mp1.deviceName = "设备" + (i * data.list + j + 1).ToString();
  272. // mp1.carryingSystem = "承载系统1";
  273. // mp1.deviceLocation = "4A-23机房";
  274. // mp1.equipmentType = "机柜";
  275. // mp1.equipmentModel = null;
  276. // mp1.factory = null;
  277. // mp1.specification = null;
  278. // mp1.listDevicesParameters = new List<DeviceParameters>();
  279. // if (int.Parse( mp1.deviceId) % 2 == 0)
  280. // mp1.listDevicesParameters.Add(dp1);
  281. // else
  282. // mp1.listDevicesParameters.Add(dp2);
  283. // data.listEquipmentData.Add(mp1);
  284. // }
  285. // }
  286. // for (int i = 0; i < listTagid.Count; i++)
  287. // {
  288. // if ((i + 1) < data.listEquipmentData.Count)
  289. // {
  290. // data.listEquipmentData[i].tagID = listTagid[i];
  291. // }
  292. // else
  293. // break;
  294. // }
  295. //}
  296. GameObject rDataItem = Resources.Load<GameObject>("DataItem");
  297. for (int i = 0; i < data.listEquipmentData.Count; i++)
  298. {
  299. GameObject obj = GameObject.Instantiate(cabinetObj, transform);
  300. obj.transform.localScale = new Vector3(data.cabinetW, data.cabinetH, obj.transform.localScale.z);
  301. // Debug.Log(data.cabinetH + " " + data.cabinetW);
  302. obj.name =i.ToString();
  303. // Debug.Log(obj.name + " " + new Vector3(j * (data.cabinetW + data.intervalW), 0, i * data.intervalL));
  304. obj.transform.localPosition = new Vector3((i%data.list)*(data.cabinetW + data.intervalW), 0, (i/data.list) * data.intervalL);
  305. obj.GetComponent<MachineObj>().Setting((i / data.list) % 2 == 0);
  306. listObjes.Add(obj.GetComponent<MachineObj>());
  307. listObjes[i].deviceParameters = data.listEquipmentData[i];
  308. if(data.listEquipmentData[i]!=null)
  309. {
  310. GameObject item = GameObject.Instantiate(rDataItem);
  311. DataItem dataItem = item.GetComponent<DataItem>();
  312. dataItem.Init(data.listEquipmentData[i]);
  313. // item.SetActive(true);
  314. for (int j = 0; j < data.listEquipmentData[i].listDevicesParameters.Count; j++)
  315. {
  316. dataItem.SetDeviceParametersData(data.listEquipmentData[i].listDevicesParameters);
  317. }
  318. listObjes[i].dataItem = dataItem;
  319. dataItem.transform.SetParent(listObjes[i].transform);
  320. dataItem.transform.localPosition = listObjes[i].xTag.localPosition;
  321. dataItem.transform.localEulerAngles = new Vector3(0, listObjes[i].isSingle ? 180 : 0, 0);
  322. }
  323. }
  324. //Debug.Log(data.line);
  325. Vector3 leftPos = new Vector3(-1 * (data.cabinetW + data.intervalW), 0, 0);
  326. Vector3 rightPos = new Vector3((data.cabinetW * data.list + data.intervalW * data.list + data.cabinetW + data.intervalW), 0, 0);
  327. WayFindingManager.Instance.SetLine(leftPos, rightPos);
  328. SaveAllData();
  329. isInit = true;
  330. }
  331. public IEnumerator RandomRange()
  332. {
  333. yield return 2f;
  334. int randomNum = UnityEngine.Random.Range(0, listObjes.Count-1);
  335. // WayFindingManager.Instance.SetServerItem(listObjes[randomNum].transform);
  336. List<MachineObj> listMachineObj = new List<MachineObj>();
  337. for (int i = 0; i < 3; i++)
  338. {
  339. if(i<listObjes.Count-1)
  340. {
  341. listMachineObj.Add(listObjes[i]);
  342. }
  343. }
  344. WayFindingManager.Instance.NavigatorServer(listMachineObj);
  345. }
  346. public void ServerInit()
  347. {
  348. // if (data.listCabinetData == null)
  349. // return;
  350. //// GameObject rDataItem = Resources.Load<GameObject>("DataItem");
  351. // for (int i = 0; i < data.listCabinetData.Count; i++)
  352. // {
  353. // if (data.listCabinetData[i].listBladeServerData == null)
  354. // continue;
  355. // for (int j = 0; j < data.listCabinetData[i].listBladeServerData.Count; j++)
  356. // {
  357. // GameObject item = GameObject.Instantiate(rDataItem);
  358. // DataItem dataItem = item.GetComponent<DataItem>();
  359. // dataItem.Init(data.listCabinetData[i].listBladeServerData[j].tagID, data.listCabinetData[i].listBladeServerData[j].pos, data.listCabinetData[i].listBladeServerData[j].eulerAxle , data.listCabinetData[i].listBladeServerData[j], data.listCabinetData[i].listBladeServerData[j].width, data.listCabinetData[i].listBladeServerData[j].heigth);
  360. // }
  361. // }
  362. if (data.listEquipmentData == null)
  363. return;
  364. GameObject rDataItem = Resources.Load<GameObject>("DataItem");
  365. for (int i = 0; i < data.listEquipmentData.Count; i++)
  366. {
  367. GameObject item = GameObject.Instantiate(rDataItem);
  368. DataItem dataItem = item.GetComponent<DataItem>();
  369. dataItem.Init(data.listEquipmentData[i]);
  370. // item.SetActive(true);
  371. for (int j = 0; j < data.listEquipmentData[i].listDevicesParameters.Count;j++)
  372. {
  373. dataItem.SetDeviceParametersData(data.listEquipmentData[i].listDevicesParameters);
  374. }
  375. }
  376. }
  377. public void SettingMachine( )
  378. {
  379. if (listObjes != null)
  380. {
  381. for (int i = 0; i < listObjes.Count; i++)
  382. {
  383. listObjes[i].gameObject.SetActive(false);
  384. }
  385. listObjes.Clear();
  386. listObjes = new List<MachineObj>();
  387. }
  388. if(BladeServerManager.Instance.listTagTracking!=null)
  389. {
  390. for (int i = 0; i < BladeServerManager.Instance.listTagTracking.Count; i++)
  391. {
  392. BladeServerManager.Instance.listTagTracking[i].gameObject.SetActive(false);
  393. }
  394. BladeServerManager.Instance.listTagTracking.Clear();
  395. }
  396. //data.listEquipmentData = new List<MachineParameters>();
  397. //for (int i = 0; i < data.line; i++)
  398. //{
  399. // for (int j = 0; j < data.list; j++)
  400. // {
  401. // MachineParameters mp1 = new MachineParameters();
  402. // mp1.deviceId = (i * data.list + j + 1).ToString();
  403. // mp1.tagID = -1000;
  404. // mp1.offsetPos = new Vector3(0f, 0, 0f);
  405. // mp1.eulerAxle = 3;
  406. // mp1.deviceName = "设备" + (i * data.list + j + 1).ToString();
  407. // mp1.carryingSystem = "承载系统1";
  408. // mp1.deviceLocation = "4A-23机房";
  409. // mp1.equipmentType = "机柜";
  410. // mp1.equipmentModel = null;
  411. // mp1.factory = null;
  412. // mp1.specification = null;
  413. // mp1.listDevicesParameters = new List<DeviceParameters>();
  414. // if (int.Parse( mp1.deviceId) % 2 == 0)
  415. // mp1.listDevicesParameters.Add(dp1);
  416. // else
  417. // mp1.listDevicesParameters.Add(dp2);
  418. // data.listEquipmentData.Add(mp1);
  419. // }
  420. //}
  421. //Debug.Log(data.line);
  422. //for (int i = 0; i < listTagid.Count; i++)
  423. //{
  424. // if (data.listEquipmentData.Count < (i + 1))
  425. // {
  426. // Debug.Log("a "+listTagid);
  427. // Debug.Log(data.listEquipmentData[i].tagID);
  428. // data.listEquipmentData[i].tagID = listTagid[i];
  429. // }
  430. //}
  431. //for (int i = 0; i < data.listEquipmentData.Count; i++)
  432. //{
  433. // GameObject obj = GameObject.Instantiate(cabinetObj, transform);
  434. // obj.transform.localScale = new Vector3(data.cabinetW, data.cabinetH, obj.transform.localScale.z);
  435. // Debug.Log(data.cabinetH + " " + data.cabinetW);
  436. // obj.name = data.listEquipmentData[i].deviceName;
  437. // // Debug.Log(obj.name + " " + new Vector3(j * (data.cabinetW + data.intervalW), 0, i * data.intervalL));
  438. // obj.transform.localPosition = new Vector3((i % data.line) * (data.cabinetW + data.intervalW), 0, i * data.intervalL);
  439. // obj.GetComponent<MachineObj>().Setting((i % data.line) % 2 == 0);
  440. // listObjes.Add(obj.GetComponent<MachineObj>());
  441. // listObjes[i].deviceParameters = data.listEquipmentData[i];
  442. //}
  443. //Vector3 leftPos = new Vector3(-1 * (data.cabinetW + data.intervalW), 0, 0);
  444. //Vector3 rightPos = new Vector3((data.cabinetW * data.list + data.intervalW * data.list), 0, 0);
  445. //WayFindingManager.Instance.SetLine(leftPos, rightPos);
  446. GameObject rDataItem = Resources.Load<GameObject>("DataItem");
  447. for (int i = 0; i < data.listEquipmentData.Count; i++)
  448. {
  449. GameObject obj = GameObject.Instantiate(cabinetObj, transform);
  450. obj.transform.localScale = new Vector3(data.cabinetW, data.cabinetH, obj.transform.localScale.z);
  451. Debug.Log(data.cabinetH + " " + data.cabinetW);
  452. obj.name = data.listEquipmentData[i].deviceName;
  453. // Debug.Log(obj.name + " " + new Vector3(j * (data.cabinetW + data.intervalW), 0, i * data.intervalL));
  454. obj.transform.localPosition = new Vector3((i % data.line) * (data.cabinetW + data.intervalW), 0, (i / data.line) * data.intervalL);
  455. obj.GetComponent<MachineObj>().Setting((i / data.line) % 2 == 0);
  456. listObjes.Add(obj.GetComponent<MachineObj>());
  457. listObjes[i].deviceParameters = data.listEquipmentData[i];
  458. GameObject item = GameObject.Instantiate(rDataItem);
  459. DataItem dataItem = item.GetComponent<DataItem>();
  460. dataItem.Init(data.listEquipmentData[i]);
  461. // item.SetActive(true);
  462. for (int j = 0; j < data.listEquipmentData[i].listDevicesParameters.Count; j++)
  463. {
  464. dataItem.SetDeviceParametersData(data.listEquipmentData[i].listDevicesParameters);
  465. }
  466. listObjes[i].dataItem = dataItem;
  467. dataItem.transform.SetParent(listObjes[i].transform);
  468. dataItem.transform.localPosition = listObjes[i].xTag.localPosition;
  469. dataItem.transform.localEulerAngles = new Vector3(0, listObjes[i].isSingle ? 180 : 0, 0);
  470. }
  471. //Debug.Log(data.line);
  472. Vector3 leftPos = new Vector3(-1 * (data.cabinetW + data.intervalW), 0, 0);
  473. Vector3 rightPos = new Vector3((data.cabinetW * data.list + data.intervalW * data.list + data.cabinetW + data.intervalW), 0, 0);
  474. WayFindingManager.Instance.SetLine(leftPos, rightPos);
  475. SaveAllData();
  476. isInit = true;
  477. }
  478. public void SettingCameraFar()
  479. {
  480. OpenXRCamera.Instance.head.GetComponent<Camera>().farClipPlane = data.cameraFar;
  481. }
  482. public void OnTrackingFirst(TagTracking tracking)
  483. {
  484. if(isDataInit)
  485. QueryTagData(tracking);
  486. }
  487. public void OnTrackingStay(TagTracking tracking)
  488. {
  489. }
  490. public void OnTrackingFound(TagTracking tracking)
  491. {
  492. // UILogManager.Instance.SendLogText(2, tracking.TrackId.ToString());
  493. if (isDataInit)
  494. QueryTagData(tracking);
  495. }
  496. public void OnTrackingLost(TagTracking tracking)
  497. {
  498. if (isDataInit)
  499. {
  500. if (foundTracking == tracking)
  501. foundTracking = null;
  502. }
  503. }
  504. private void QueryTagData( TagTracking tracking)
  505. {
  506. if (isDataInit)
  507. {
  508. foundTracking = tracking;
  509. tagData = null;
  510. Debug.Log("DGJ ===> QueryTagData" + data.listTagData.Count);
  511. for (int i = 0; i < data.listTagData.Count; i++)
  512. {
  513. if (data.listTagData[i].id == tracking.TrackId)
  514. {
  515. tagData = data.listTagData[i];
  516. SynPlayerTransform.Instance.transform.SetParent(tracking.transform);
  517. break;
  518. }
  519. }
  520. }
  521. //if (state)
  522. //{
  523. // for (int i = 0; i < listEditorTag.Count; i++)
  524. // {
  525. // listEditorTag[i].gameObject.SetActive(!state);
  526. // if (listEditorTag[i].id == tracking.TrackId)
  527. // listEditorTag[i].gameObject.SetActive(true);
  528. // }
  529. //}
  530. }
  531. bool state = false;
  532. public void EditorTag(Text text)
  533. {
  534. text.text = state ? "打开" : "关闭";
  535. text.text += "场景编辑";
  536. state = !state;
  537. DragManager.Instance.state = state;
  538. cubeY.SetActive(state);
  539. for (int i = 0; i < listObjes.Count; i++)
  540. {
  541. listObjes[i].MeshState(state);
  542. }
  543. if (!state && foundTracking!=null)
  544. {
  545. for (int i = 0; i < listEditorTag.Count; i++)
  546. {
  547. listEditorTag[i].gameObject.SetActive(state);
  548. }
  549. for (int i = 0; i < data.listTagData.Count; i++)
  550. {
  551. //data.listTagData[i].offestPos = data.listTagData[i].tagModel.localPosition;
  552. //data.listTagData[i].offestrot = data.listTagData[i].tagModel.localEulerAngles;
  553. for (int j = 0; j < listEditorTag.Count; j++)
  554. {
  555. if (listEditorTag[j].id == data.listTagData[i].id)
  556. {
  557. //data.listTagData[i].offestPos+= listEditorTag[j].transform.localPosition;
  558. //data.listTagData[i].offestrot+= listEditorTag[j].transform.localEulerAngles;
  559. transform.SetParent(listEditorTag[j].transform.parent);
  560. data.listTagData[i].offestPos = transform.localPosition;
  561. data.listTagData[i].offestrot = transform.localEulerAngles;
  562. transform.parent = null;
  563. Debug.Log("DGJ ===> 保存 Tag " + listEditorTag[j].id);
  564. }
  565. }
  566. }
  567. UpdataTagData tagData = new UpdataTagData();
  568. tagData.listTagData = data.listTagData;
  569. string jsonDate = JsonConvert.SerializeObject(tagData);
  570. Debug.Log(jsonDate);
  571. HttpTool.Instance.Post(HttpActionLang.updateTag, jsonDate, UpdateTagCallBack);
  572. selectTOldPos = Vector3.zero;
  573. selectTOldRot = Vector3.zero;
  574. DragManager.Instance.triggerState = TriggerState.No;
  575. selectTrigger = null;
  576. }
  577. if(!state)
  578. {
  579. SaveAllData();
  580. }
  581. }
  582. private void UpdateTagCallBack(string msg)
  583. {
  584. Debug.Log(msg);
  585. JObject json = JObject.Parse(msg);
  586. if (json["code"].ToString() == "200")
  587. {
  588. Debug.Log(" 更新场景Tag参数成功 ");
  589. }
  590. else
  591. {
  592. Debug.LogError(" 更新场景Tag参数出错 !!! " + HttpActionLang.updateCabinet);
  593. }
  594. }
  595. public void SaveAllData()
  596. {
  597. PlayerPrefs.SetString("MachineRoomTagData", JsonConvert.SerializeObject(data.listTagData));
  598. PlayerPrefs.SetString("MachineRoomCabinetData", JsonConvert.SerializeObject(data.listEquipmentData));
  599. PlayerPrefs.SetString("MachineRoomData", JsonConvert.SerializeObject(data));
  600. Debug.Log(" 保存成功 " + JsonConvert.SerializeObject(data));
  601. }
  602. private void SaveData()
  603. {
  604. if(data!=null)
  605. {
  606. string _str = JsonConvert.SerializeObject(data);
  607. System.IO.Directory.CreateDirectory(Application.persistentDataPath+"/Data");
  608. string NowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_").Replace("/", "_").Replace(":", "_");
  609. Debug.Log(Application.persistentDataPath + "/Data");
  610. using (System.IO.StreamWriter writer = System.IO.File.CreateText(Application.persistentDataPath + "/Data" + "/"+ NowTime + "_data.txt"))
  611. {
  612. writer.Write(_str);
  613. //writer.
  614. }
  615. //UnityEditor.AssetDatabase.Refresh();
  616. }
  617. }
  618. private void OnDisable()
  619. {
  620. //PlayerPrefs.SetString("MachineRoomTagData", JsonConvert.SerializeObject(data.listTagData));
  621. //PlayerPrefs.SetString("MachineRoomCabinetData", JsonConvert.SerializeObject(data.listEquipmentData));
  622. //PlayerPrefs.SetString("MachineRoomData", JsonConvert.SerializeObject(data));
  623. // SaveData();
  624. }
  625. }
  626. public class UpdataTagData
  627. {
  628. public UpdataTagData()
  629. {
  630. listTagData = new List<MachineRoomTagData>();
  631. }
  632. public List<MachineRoomTagData> listTagData { get; set; }
  633. }