MachineRoomManager.cs 26 KB

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