UIEditorPanel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Blue;
  5. using LitJson;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using UnityEngine;
  9. using UnityEngine.Assertions.Must;
  10. using UnityEngine.UI;
  11. public class UIEditorPanel : AbstractController, EventObserver
  12. {
  13. public event Action<GameObject> OnClickRemoveBtn;
  14. [SerializeField] private Button UCBtn;
  15. [SerializeField] private Button TicBtn;
  16. [SerializeField] private Button RemoveBtn;
  17. [SerializeField] private Image UCElenemt;
  18. [SerializeField] private Button AddSize;
  19. [SerializeField] private Button SubSize;
  20. [SerializeField] private Button SubY;
  21. [SerializeField] private Button AddY;
  22. [SerializeField] private Button SubX;
  23. [SerializeField] private Button AddX;
  24. [SerializeField] private SCButton SubRot;
  25. [SerializeField] private SCButton AddRot;
  26. [SerializeField] private Image TicElenemt;
  27. [SerializeField] private Button ADDXBtn;
  28. [SerializeField] private Button SubXBtn;
  29. [SerializeField] private Button ADDYBtn;
  30. [SerializeField] private Button SubYBtn;
  31. [SerializeField] private Button AddZBtn;
  32. [SerializeField] private Button SubZBtn;
  33. [SerializeField] private Button InitiativeBtn;
  34. [SerializeField] private Button RevocationBtn;
  35. [SerializeField] private Button SaveBtn;
  36. private Transform MaterialObj;
  37. /// <summary>
  38. /// 当前物体移动旋转缩放的操作步骤
  39. /// </summary>
  40. private List<objectData> listAlter;
  41. /// <summary>
  42. /// 当前景点信息
  43. /// </summary>
  44. private SpoitValue listSpoits;
  45. public static UIEditorPanel Instance;
  46. private void Awake()
  47. {
  48. Instance = this;
  49. InitEvent();
  50. MaterialObj = null;
  51. listAlter = new List<objectData>();
  52. listSpoits = new SpoitValue();
  53. EventManager.Instance.Register(this, EventID.EVENT_1);
  54. }
  55. public void Init()
  56. {
  57. }
  58. private void InitEvent()
  59. {
  60. UCBtn.onClick.AddListener(() =>
  61. {
  62. if (UCElenemt.gameObject.activeSelf)
  63. return;
  64. UCElenemt.gameObject.SetActive(true);
  65. TicElenemt.gameObject.SetActive(false);
  66. });
  67. TicBtn.onClick.AddListener(() =>
  68. {
  69. if (TicElenemt.gameObject.activeSelf)
  70. return;
  71. UCElenemt.gameObject.SetActive(false);
  72. TicElenemt.gameObject.SetActive(true);
  73. });
  74. RemoveBtn.onClick.AddListener(() =>
  75. {
  76. if (MaterialObj == null)
  77. return;
  78. if (MaterialObj.tag == "TriggerBox")
  79. return;
  80. if (OnClickRemoveBtn != null)
  81. {
  82. OnClickRemoveBtn(MaterialObj.gameObject);
  83. }
  84. });
  85. //RemoveBtn.gameObject.SetActive(false);
  86. AddSize.onClick.AddListener(() =>
  87. {
  88. if (MaterialObj == null)
  89. return;
  90. SaveAlter();
  91. MaterialObj.localScale += new Vector3(MaterialObj.localScale.x / 10.0f, MaterialObj.localScale.y / 10.0f, MaterialObj.localScale.z / 10.0f);
  92. });
  93. SubSize.onClick.AddListener(() =>
  94. {
  95. if (MaterialObj == null)
  96. return;
  97. SaveAlter();
  98. MaterialObj.localScale -= new Vector3(MaterialObj.localScale.x / 10.0f, MaterialObj.localScale.y / 10.0f, MaterialObj.localScale.z / 10.0f);
  99. });
  100. AddRot.onClick.AddListener(() =>
  101. {
  102. if (MaterialObj == null)
  103. return;
  104. SaveAlter();
  105. MaterialObj.localEulerAngles += new Vector3(0, 10, 0);
  106. });
  107. SubRot.onClick.AddListener(() =>
  108. {
  109. if (MaterialObj == null)
  110. return;
  111. SaveAlter();
  112. MaterialObj.localEulerAngles -= new Vector3(0, 10, 0);
  113. });
  114. AddX.onClick.AddListener(() =>
  115. {
  116. if (MaterialObj == null)
  117. return;
  118. SaveAlter();
  119. MaterialObj.localPosition -= new Vector3(0.1f, 0, 0);
  120. });
  121. SubX.onClick.AddListener(() =>
  122. {
  123. if (MaterialObj == null)
  124. return;
  125. SaveAlter();
  126. MaterialObj.localPosition += new Vector3(0.1f, 0, 0);
  127. });
  128. AddY.onClick.AddListener(() =>
  129. {
  130. if (MaterialObj == null)
  131. return;
  132. SaveAlter();
  133. MaterialObj.localPosition += new Vector3(0, 0.1f, 0);
  134. });
  135. SubY.onClick.AddListener(() =>
  136. {
  137. if (MaterialObj == null)
  138. return;
  139. SaveAlter();
  140. MaterialObj.localPosition -= new Vector3(0, 0.1f, 0);
  141. });
  142. ADDXBtn.onClick.AddListener(() =>
  143. {
  144. if (MaterialObj == null)
  145. return;
  146. SaveAlter();
  147. MaterialObj.localPosition -= new Vector3(0.1f, 0, 0);
  148. });
  149. SubXBtn.onClick.AddListener(() =>
  150. {
  151. if (MaterialObj == null)
  152. return;
  153. SaveAlter();
  154. MaterialObj.localPosition += new Vector3(0.1f, 0, 0);
  155. });
  156. ADDYBtn.onClick.AddListener(() =>
  157. {
  158. if (MaterialObj == null)
  159. return;
  160. SaveAlter();
  161. MaterialObj.localPosition += new Vector3(0, 0.1f, 0);
  162. });
  163. SubYBtn.onClick.AddListener(() =>
  164. {
  165. if (MaterialObj == null)
  166. return;
  167. SaveAlter();
  168. MaterialObj.localPosition -= new Vector3(0, 0.1f, 0);
  169. });
  170. AddZBtn.onClick.AddListener(() =>
  171. {
  172. if (MaterialObj == null)
  173. return;
  174. SaveAlter();
  175. MaterialObj.localPosition += new Vector3(0, 0, 0.1f);
  176. });
  177. SubZBtn.onClick.AddListener(() =>
  178. {
  179. if (MaterialObj == null)
  180. return;
  181. SaveAlter();
  182. MaterialObj.localPosition -= new Vector3(0, 0, 0.1f);
  183. });
  184. InitiativeBtn.onClick.AddListener(() =>
  185. {
  186. if (MaterialObj == null)
  187. return;
  188. InitMaterialObj();
  189. });
  190. RevocationBtn.onClick.AddListener(() =>
  191. {
  192. GameManager.Instance.SetPosRotScale();
  193. if (MaterialObj == null)
  194. return;
  195. if (listAlter.Count >= 1)
  196. {
  197. MaterialObj.localPosition = listAlter[listAlter.Count - 1].pos;
  198. MaterialObj.localEulerAngles = listAlter[listAlter.Count - 1].rot;
  199. MaterialObj.localScale = listAlter[listAlter.Count - 1].scale;
  200. listAlter.RemoveAt(listAlter.Count - 1);
  201. }
  202. });
  203. SaveBtn.onClick.AddListener(() =>
  204. {
  205. /*
  206. if (MaterialObj == null)
  207. {
  208. InstantiateCommand Command = new InstantiateCommand(
  209. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  210. "保存失败\n服务器异常,请稍后重试");
  211. CommandSystem.Instance.Send(Command);
  212. //this.GetService<ISendLogService>().SendLog("Blue","请选择素材");
  213. return;
  214. }
  215. GameManager.Instance.SaveSpoitObjs();
  216. */
  217. // TODO 需要再保存一次
  218. GameManager.Instance.GetPosRotScale();
  219. StartCoroutine(SetSpoitInfo());
  220. });
  221. }
  222. public IEnumerator SetSpoitInfo()
  223. {
  224. countSplit =0;saveData.Clear();
  225. int count = GameManager.Instance.ObjsParent.childCount;
  226. for (int i = 1; i <= count-1;i++) // 第一个LocalNULLShowObj是无用的
  227. {
  228. var spoit = GameManager.Instance.SpoitValues[i-1]; // 某个景点
  229. for (int j = 0; j < spoit.material.Count; j++) // 更新 ObjectTransform
  230. {
  231. for (int k = 0;k< spoit.material[j].materialList.Count; k++)
  232. {
  233. spoit.material[j].select = true;
  234. if(GameManager.Instance.allobjs.ContainsKey(spoit.id.ToString()))
  235. {
  236. if (GameManager.Instance.allobjs[spoit.id.ToString()].ContainsKey(spoit.material[j].id.ToString()))
  237. {
  238. if (GameManager.Instance.allobjs[spoit.id.ToString()][spoit.material[j].id.ToString()].ContainsKey(spoit.material[j].materialList[k].id.ToString()))
  239. {
  240. spoit.material[j].materialList[k].ObjectTransform.nowPos = GameManager.Instance.allobjs[spoit.id.ToString()][spoit.material[j].id.ToString()][spoit.material[j].materialList[k].id.ToString()].transform.localPosition;// spoit.material[j].materialList[k].obj.transform.localPosition;// GameManager.Instance.ObjsParent.GetChild(i).GetChild(j).GetChild(k).localPosition;
  241. spoit.material[j].materialList[k].ObjectTransform.nowRot = GameManager.Instance.allobjs[spoit.id.ToString()][spoit.material[j].id.ToString()][spoit.material[j].materialList[k].id.ToString()].transform.localEulerAngles;//spoit.material[j].materialList[k].obj.transform.localEulerAngles;// GameManager.Instance.ObjsParent.GetChild(i).GetChild(j).GetChild(k).localEulerAngles;
  242. spoit.material[j].materialList[k].ObjectTransform.nowScale = GameManager.Instance.allobjs[spoit.id.ToString()][spoit.material[j].id.ToString()][spoit.material[j].materialList[k].id.ToString()].transform.localScale;//spoit.material[j].materialList[k].obj.transform.localScale;// GameManager.Instance.ObjsParent.GetChild(i).GetChild(j).GetChild(k).localScale;
  243. }
  244. }
  245. }
  246. // Debug.Log("HJJSSSSSSSSSSSSSSSS保存 ===》"+ spoit.id.ToString()+"_"+ spoit.material[j].id.ToString() + "_"+ spoit.material[j].materialList[k].id.ToString());
  247. }
  248. }
  249. SplitSpoitData(spoit,count-1); //// 第一个LocalNULLShowObj是无用的 ,so -1
  250. yield return null;
  251. }
  252. }
  253. private List<SendSaveData> saveData = new List<SendSaveData>();
  254. private void SplitSpoitData(SpoitValue spoit,int spoitCount)
  255. {
  256. countSplit++;
  257. // GameManager.Instance.UpdateSpoitValue();
  258. SendSaveData data = new SendSaveData();
  259. data.id = GameManager.Instance.SpoitsValueDetail[spoit.id].id;
  260. data.name = GameManager.Instance.SpoitsValueDetail[spoit.id].name;
  261. data.projectId = DataManager.Instance.ProjectID;
  262. data.material = spoit.material;
  263. data.spoittf = GameManager.Instance.SpoitsValueDetail[spoit.id].spoitTf;
  264. data.position = GameManager.Instance.SpoitsValueDetail[spoit.id].position;
  265. data.spoittf.nowPos = GameManager.Instance.spoitposObjDic[spoit.id].transform.localPosition - GameManager.Instance.spoitposDic[spoit.id];
  266. Debug.Log("最新保存====》保存的POS" + data.spoittf.nowPos);
  267. Debug.Log("最新保存====》物體的POS" + GameManager.Instance.spoitposObjDic[spoit.id].transform.localPosition);
  268. Debug.Log("最新保存====》原始的POS" + GameManager.Instance.spoitposDic[spoit.id]);
  269. data.spoittf.nowRot = GameManager.Instance.spoitposObjDic[spoit.id].transform.localEulerAngles;
  270. data.spoittf.nowScale = GameManager.Instance.spoitposObjDic[spoit.id].transform.localScale;
  271. data.triggerImageId = GameManager.Instance.SpoitsValueDetail[spoit.id].triggerImageId;
  272. data.triggerRange = GameManager.Instance.SpoitsValueDetail[spoit.id].triggerRange;
  273. saveData.Add(data);
  274. if (countSplit==spoitCount)
  275. {
  276. string sendData = JsonConvert.SerializeObject(saveData);
  277. JsonData jsdata = JsonMapper.ToObject(sendData);
  278. for (int i = 0; i < jsdata.Count; i++)
  279. {
  280. if(jsdata[i].Keys.Contains("material")&&jsdata[i]["material"].IsArray)
  281. {
  282. for (int j = 0; j < jsdata[i]["material"].Count; j++)
  283. {
  284. if (jsdata[i]["material"][j].Keys.Contains("materialList") && jsdata[i]["material"][j]["materialList"].IsArray)
  285. {
  286. for (int k = 0; k < jsdata[i]["material"][j]["materialList"].Count; k++)
  287. {
  288. jsdata[i]["material"][j]["materialList"][k]["textBJ"] = jsdata[i]["material"][j]["materialList"][k]["TextBJ"];
  289. jsdata[i]["material"][j]["materialList"][k]["downloadPath"] = jsdata[i]["material"][j]["materialList"][k]["DownloadPath"];
  290. jsdata[i]["material"][j]["materialList"][k]["objectTransform"] = jsdata[i]["material"][j]["materialList"][k]["ObjectTransform"];
  291. }
  292. }
  293. }
  294. }
  295. }
  296. Debug.LogError(countSplit + "==" + spoitCount);
  297. Debug.LogError("sendDatasendDatasendData====>" + jsdata.ToJson());
  298. HttpTool.Instance.PostTest("/viewpoint/update", jsdata.ToJson(), SaveCallBack);
  299. }
  300. }
  301. private int countSplit =0;
  302. private void SaveCallBack(string message)
  303. {
  304. try
  305. {
  306. JsonData jd = JsonMapper.ToObject(message);
  307. if (jd.Keys.Contains("message"))
  308. {
  309. if (jd["message"].ToString() == "success")
  310. {
  311. Debug.Log("更新成功");
  312. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.SaveSpoitDataEnd);
  313. ((LoadingPanel)UIManager.Instance.GetUI(UINameConfig.LoadingPanel)).TextStr = "更新成功";
  314. InstantiateCommand Command = new InstantiateCommand(
  315. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  316. InstantiateSystem.Instance.BlueObject.SuccessText);
  317. CommandSystem.Instance.Send(Command);
  318. GameManager.Instance.saveOldData();
  319. return;
  320. }
  321. else
  322. {
  323. InstantiateCommand ErrorCommand = new InstantiateCommand(
  324. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  325. InstantiateSystem.Instance.BlueObject.NetErrorText);
  326. CommandSystem.Instance.Send(ErrorCommand);
  327. //this.GetService<ISendLogService>().SendLog("Blue","UnityWebRequest Error");
  328. return;
  329. }
  330. }
  331. else
  332. {
  333. InstantiateCommand ErrorCommand = new InstantiateCommand(
  334. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  335. InstantiateSystem.Instance.BlueObject.NetErrorText);
  336. CommandSystem.Instance.Send(ErrorCommand);
  337. //this.GetService<ISendLogService>().SendLog("Blue","UnityWebRequest Error");
  338. return;
  339. }
  340. }
  341. catch
  342. {
  343. InstantiateCommand ErrorCommand = new InstantiateCommand(
  344. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  345. InstantiateSystem.Instance.BlueObject.NetErrorText);
  346. CommandSystem.Instance.Send(ErrorCommand);
  347. //this.GetService<ISendLogService>().SendLog("Blue","UnityWebRequest Error");
  348. return;
  349. }
  350. }
  351. /// <summary>
  352. /// 选择素材
  353. /// </summary>
  354. /// <param name="MaterialObj"></param>
  355. public void SelectMaterial(Transform MaterialObj)
  356. {
  357. Debug.LogError("MaterialObjName:"+MaterialObj.name);
  358. this.MaterialObj = MaterialObj;
  359. listAlter.Clear();
  360. listAlter = new List<objectData>();
  361. }
  362. /// <summary>
  363. /// 初始化素材
  364. /// 只初始化当前素材
  365. /// </summary>
  366. private void InitMaterialObj()
  367. {
  368. if (MaterialObj == null)
  369. return;
  370. string name = MaterialObj.name;
  371. listAlter.Clear();
  372. listAlter = new List<objectData>();
  373. GameManager.Instance.InitSpoitObjs();
  374. }
  375. private void SaveAlter()
  376. {
  377. objectData data = new objectData();
  378. data.pos = MaterialObj.localPosition;
  379. data.rot = MaterialObj.localEulerAngles;
  380. data.scale = MaterialObj.localScale;
  381. listAlter.Add(data);
  382. }
  383. public void HandleEvent(EventData data)
  384. {
  385. switch (data.ID)
  386. {
  387. case EventID.EVENT_1:
  388. EventData1 data1 = (EventData1)data;
  389. if (data1.SelectObj != null)
  390. {
  391. SelectMaterial(data1.SelectObj.transform);
  392. }
  393. else
  394. {
  395. MaterialObj = null;
  396. listAlter.Clear();
  397. }
  398. break;
  399. case EventID.EVENT_2:
  400. break;
  401. default:
  402. break;
  403. }
  404. }
  405. private void OnDestroy()
  406. {
  407. //EventManager.Instance.Remove(this);
  408. }
  409. }