UIEditorPanel.cs 12 KB

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