UIMaterialsPanel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. using Blue;
  2. using LitJson;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. /// <summary>
  11. /// 素材显示面板
  12. /// </summary>
  13. public class UIMaterialsPanel : UIPanelBase, EventObserver
  14. {
  15. /// <summary>
  16. /// 当前按钮代表景点父物体
  17. /// </summary>
  18. private GameObject m_ShowObj;
  19. private bool m_IsSelect;
  20. private ObjectValue m_ShowObjValues;
  21. private Transform m_Content;
  22. private Toggle m_BtnPrefab;
  23. /// <summary>
  24. /// 添加其他素材按钮
  25. /// </summary>
  26. private Toggle m_AddMaterBtn;
  27. /// <summary>
  28. /// 整体调整按钮
  29. /// </summary>
  30. private Toggle m_AllBtn;
  31. private ToggleGroup m_ToggleGroup;
  32. /// <summary>
  33. /// 当前景点信息
  34. /// </summary>
  35. private SpoitValue m_CurrentValue;
  36. private List<MaterialBtnItem> m_Btns;
  37. public EditorPanel EditorPanel { get; set; }
  38. protected override void OnAwake()
  39. {
  40. base.OnAwake();
  41. m_IsSelect = false;
  42. m_Btns = new List<MaterialBtnItem>();
  43. m_Content = CacheTransform.Find("Scroll View/Viewport/UIMaterialContent");
  44. m_ToggleGroup = m_Content.GetComponent<ToggleGroup>();
  45. m_AddMaterBtn = m_Content.Find("AddMaterialBtn").GetComponent<Toggle>();
  46. m_AllBtn = m_Content.Find("ALLBtn").GetComponent<Toggle>();
  47. m_BtnPrefab = m_Content.Find("MaterialBtn").gameObject.GetComponent<Toggle>();
  48. m_AddMaterBtn.onValueChanged.AddListener(OnClickAddMaterialBtn);
  49. m_AllBtn.onValueChanged.AddListener(OnClickAllBtn);
  50. }
  51. private void OnClickAddMaterialBtn(bool arg0)
  52. {
  53. EventData1 data1 = new EventData1(EventID.EVENT_1, null);
  54. data1.Send();
  55. EditorPanel.ChangeShowUI(false);
  56. }
  57. private void OnClickAllBtn(bool b)
  58. {
  59. if (b)
  60. {
  61. EventData1 data1 = new EventData1(EventID.EVENT_1, m_ShowObj);
  62. data1.Send();
  63. for (int i = 0; i < m_Btns.Count; i++)
  64. {
  65. if (m_Btns[i].gameObject.activeSelf && !m_Btns[i].IsCollider)
  66. {
  67. m_Btns[i].SelectObj(true);
  68. }
  69. }
  70. }
  71. }
  72. public override void Hide()
  73. {
  74. base.Hide();
  75. }
  76. /// <summary>
  77. /// 隐藏所有按钮,并清除数据
  78. /// </summary>
  79. private void HideBtns()
  80. {
  81. for (int i = 0; i < m_Btns.Count; i++)
  82. {
  83. m_Btns[i].ClearData();
  84. m_Btns[i].gameObject.SetActive(false);
  85. }
  86. }
  87. /// <summary>
  88. /// 当前场景的素材
  89. /// </summary>
  90. /// <param name="materialObjs"></param>
  91. public void SetMaterial(SpoitValue spoitValue)
  92. {
  93. m_ShowObj = null;
  94. m_ShowObjValues = null;
  95. m_AllBtn.gameObject.SetActive(false);
  96. HideBtns();
  97. m_CurrentValue = spoitValue;
  98. if (m_CurrentValue == null)
  99. {
  100. return;
  101. }
  102. ObjectValue objvalues = GameManager.Instance.GetSpoitObjs(spoitValue.id);
  103. if (objvalues != null)
  104. {
  105. m_ShowObj = objvalues.Object;
  106. m_ShowObjValues = objvalues;
  107. m_ShowObj.transform.localPosition = m_ShowObjValues.nowPos;
  108. m_ShowObj.transform.localEulerAngles = m_ShowObjValues.nowRot;
  109. m_ShowObj.transform.localScale = m_ShowObjValues.nowScale;
  110. if (m_ShowObjValues.ChildrenObj.Count > 0)
  111. {
  112. m_AllBtn.gameObject.SetActive(true);
  113. }
  114. }
  115. ////先设置物体触发器
  116. //MaterialBtnItem collider = null;
  117. //if (m_Btns.Count >= 1)
  118. //{
  119. // collider = m_Btns[0];
  120. // m_Btns.RemoveAt(0);
  121. //}
  122. //else
  123. //{
  124. // collider = Instantiate(m_BtnPrefab, m_Content).gameObject.AddComponent<MaterialBtnItem>();
  125. //}
  126. //if (collider != null)
  127. //{
  128. // collider.gameObject.SetActive(true);
  129. // collider.SetData(GameManager.Instance.GetCurrentColliderValue());
  130. // collider.transform.localScale = new Vector3(1, 1, 1);
  131. // collider.transform.localRotation = Quaternion.identity;
  132. //}
  133. var sucais = m_CurrentValue.material;
  134. var objValue = objvalues.ChildrenObj;
  135. int num = 0;
  136. for (int i = 0; i < sucais.Count; i++)
  137. {
  138. var matObj = sucais[i].materialList;
  139. var objvalue = objValue[i];
  140. for (int m = 0; m < matObj.Count; m++)
  141. {
  142. MaterialBtnItem item = null;
  143. num++;
  144. if (num < m_Btns.Count)
  145. {
  146. item = m_Btns[num];
  147. }
  148. else
  149. {
  150. item = Instantiate(m_BtnPrefab, m_Content).gameObject.GetComponent<MaterialBtnItem>();
  151. m_Btns.Add(item);
  152. }
  153. item.gameObject.SetActive(true);
  154. item.transform.localScale = new Vector3(1, 1, 1);
  155. item.transform.localRotation = Quaternion.identity;
  156. Debug.Log("objvalue.ChildrenObj===》"+objvalue.ChildrenObj.Count);
  157. item.SetData(matObj[m], objvalue.ChildrenObj[m]);
  158. }
  159. }
  160. //if (collider != null)
  161. //{
  162. // m_Btns.Add(collider);
  163. // collider.transform.SetAsLastSibling();
  164. //}
  165. m_AddMaterBtn.transform.SetAsLastSibling();
  166. }
  167. public void ClosePanel()
  168. {
  169. m_ShowObj = null;
  170. m_ShowObjValues = null;
  171. m_CurrentValue = null;
  172. HideBtns();
  173. }
  174. /// <summary>
  175. /// 初始化ObjectValue数据
  176. /// </summary>
  177. public void InitObjTransForm()
  178. {
  179. m_ShowObjValues.InitObjTransform();
  180. for (int i = 0; i < m_Btns.Count; i++)
  181. {
  182. m_Btns[i].InitMatObj();
  183. }
  184. for (int i = 0; i < m_ShowObjValues.ChildrenObj.Count; i++)
  185. {
  186. var obj1 = m_ShowObjValues.ChildrenObj[i];
  187. for (int m = 0; m < obj1.ChildrenObj.Count; m++)
  188. {
  189. var obj2 = obj1.ChildrenObj[m];
  190. obj2.InitObjTransform();
  191. }
  192. }
  193. }
  194. /// <summary>
  195. /// 保存ObjectValue数据
  196. /// </summary>
  197. public void SaveMatObj()
  198. {
  199. m_ShowObjValues.SaveObjTransfoem();
  200. for (int i = 0; i < m_Btns.Count; i++)
  201. {
  202. m_Btns[i].SaveMatObj();
  203. }
  204. for (int i = 0; i < m_ShowObjValues.ChildrenObj.Count; i++)
  205. {
  206. var obj1 = m_ShowObjValues.ChildrenObj[i];
  207. for (int m = 0; m < obj1.ChildrenObj.Count; m++)
  208. {
  209. var obj2 = obj1.ChildrenObj[m];
  210. obj2.SaveObjTransfoem();
  211. }
  212. }
  213. GameManager.Instance.SaveSpoitObjectValue(m_ShowObjValues);
  214. GameManager.Instance.UpdateSpoitValue();
  215. SaveSpoits();
  216. }
  217. public void SaveSpoits()
  218. {
  219. Debug.Log("向服务器发送景点更新信息");
  220. if (m_ShowObj == null)
  221. {
  222. return;
  223. }
  224. Debug.Log("m_ShowObj存在");
  225. UIManager.Instance.ChangeCanvasPos();
  226. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.SaveSpoitData);
  227. var spoit = GameManager.Instance.SpoitValues[GameManager.Instance.SpoitIndex];
  228. List<SendSaveData> saveData = new List<SendSaveData>();
  229. SendSaveData data = new SendSaveData();
  230. data.id = spoit.id;
  231. data.name = spoit.name;
  232. data.projectId = DataManager.Instance.ProjectID;
  233. data.material = spoit.material;
  234. data.spoittf = spoit.spoitTf;
  235. //data.triggerRange = spoit.SpoitTransform[0];
  236. saveData.Add(data);
  237. string sendData = JsonConvert.SerializeObject(saveData);
  238. //string sendData = JsonMapper.ToJson(saveData);
  239. Debug.Log("更新景点数据: " + sendData);
  240. HttpTool.Instance.PostTest("/viewpoint/update", sendData, SaveCallBack);
  241. //string str = JsonMapper.ToJson(Spoits);
  242. //FileManager.WriteFile(sendData, Application.persistentDataPath + "/a.json");
  243. //Debug.Log(str);
  244. }
  245. private void SaveCallBack(string message)
  246. {
  247. if (message == "UnityWebRequest Error")
  248. {
  249. InstantiateCommand Command =new InstantiateCommand(
  250. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  251. InstantiateSystem.Instance.BlueObject.NetErrorText);
  252. CommandSystem.Instance.Send(Command);
  253. return;
  254. }
  255. //Debug.LogError("SaveCallBack: " + message);
  256. JObject jObject = JObject.Parse(message);
  257. string str = jObject["message"].ToString();
  258. if(str!="更新成功")
  259. {
  260. InstantiateCommand Command =new InstantiateCommand(
  261. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  262. InstantiateSystem.Instance.BlueObject.NetErrorText);
  263. CommandSystem.Instance.Send(Command);
  264. return;
  265. }
  266. else
  267. {
  268. Debug.Log("更新成功");
  269. }
  270. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.SaveSpoitDataEnd);
  271. ((LoadingPanel)UIManager.Instance.GetUI(UINameConfig.LoadingPanel)).TextStr = str;
  272. }
  273. public void RemoveMat(GameObject obj)
  274. {
  275. StartCoroutine(WaitRemove(obj));
  276. }
  277. IEnumerator WaitRemove(GameObject obj)
  278. {
  279. Debug.Log("删除整个景点数据");
  280. //删除整个景点数据
  281. if (obj == m_ShowObj)
  282. {
  283. for (int i = 0; i < obj.transform.childCount; i++)
  284. {
  285. if (obj.transform.GetChild(i) != obj)
  286. {
  287. Destroy(obj.transform.GetChild(i).gameObject);
  288. }
  289. }
  290. yield return new WaitForEndOfFrame();
  291. //m_ShowObj = null;
  292. m_ShowObjValues.ChildrenObj.Clear();
  293. m_IsSelect = false;
  294. var spoit = GameManager.Instance.SpoitValues[GameManager.Instance.SpoitIndex];
  295. spoit.material = new List<MaterialObl>();
  296. GameManager.Instance.SaveSpoitObjectValue(m_ShowObjValues);
  297. GameManager.Instance.SpoitValues[GameManager.Instance.SpoitIndex] = spoit;
  298. SaveSpoits();
  299. }
  300. else
  301. {
  302. for (int i = 0; i < m_Btns.Count; i++)
  303. {
  304. m_Btns[i].SaveMatObj();
  305. if (m_Btns[i].MaterialObj == obj)
  306. {
  307. m_Btns[i].IsSelect = false;
  308. //var objvalue = m_Btns[i].ObjValue;
  309. Destroy(obj);
  310. yield return new WaitForEndOfFrame();
  311. }
  312. }
  313. SaveMatObj();
  314. }
  315. GameManager.Instance.UpdateEditorUI();
  316. }
  317. public void HandleEvent(EventData data)
  318. {
  319. switch (data.ID)
  320. {
  321. case EventID.EVENT_1:
  322. EventData1 data1 = (EventData1)data;
  323. if (data1.SelectObj == m_ShowObj)
  324. {
  325. m_IsSelect = true;
  326. }
  327. else
  328. {
  329. m_IsSelect = false;
  330. }
  331. break;
  332. case EventID.EVENT_2:
  333. break;
  334. default:
  335. break;
  336. }
  337. }
  338. }