UIMaterialsPanel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. item.SetData(matObj[m], objvalue.ChildrenObj[m]);
  157. }
  158. }
  159. //if (collider != null)
  160. //{
  161. // m_Btns.Add(collider);
  162. // collider.transform.SetAsLastSibling();
  163. //}
  164. m_AddMaterBtn.transform.SetAsLastSibling();
  165. }
  166. public void ClosePanel()
  167. {
  168. m_ShowObj = null;
  169. m_ShowObjValues = null;
  170. m_CurrentValue = null;
  171. HideBtns();
  172. }
  173. /// <summary>
  174. /// 初始化ObjectValue数据
  175. /// </summary>
  176. public void InitObjTransForm()
  177. {
  178. m_ShowObjValues.InitObjTransform();
  179. for (int i = 0; i < m_Btns.Count; i++)
  180. {
  181. m_Btns[i].InitMatObj();
  182. }
  183. for (int i = 0; i < m_ShowObjValues.ChildrenObj.Count; i++)
  184. {
  185. var obj1 = m_ShowObjValues.ChildrenObj[i];
  186. for (int m = 0; m < obj1.ChildrenObj.Count; m++)
  187. {
  188. var obj2 = obj1.ChildrenObj[m];
  189. obj2.InitObjTransform();
  190. }
  191. }
  192. }
  193. /// <summary>
  194. /// 保存ObjectValue数据
  195. /// </summary>
  196. public void SaveMatObj()
  197. {
  198. m_ShowObjValues.SaveObjTransfoem();
  199. for (int i = 0; i < m_Btns.Count; i++)
  200. {
  201. m_Btns[i].SaveMatObj();
  202. }
  203. for (int i = 0; i < m_ShowObjValues.ChildrenObj.Count; i++)
  204. {
  205. var obj1 = m_ShowObjValues.ChildrenObj[i];
  206. for (int m = 0; m < obj1.ChildrenObj.Count; m++)
  207. {
  208. var obj2 = obj1.ChildrenObj[m];
  209. obj2.SaveObjTransfoem();
  210. }
  211. }
  212. GameManager.Instance.SaveSpoitObjectValue(m_ShowObjValues);
  213. GameManager.Instance.UpdateSpoitValue();
  214. SaveSpoits();
  215. }
  216. public void SaveSpoits()
  217. {
  218. Debug.Log("向服务器发送景点更新信息");
  219. if (m_ShowObj == null)
  220. {
  221. return;
  222. }
  223. Debug.Log("m_ShowObj存在");
  224. UIManager.Instance.ChangeCanvasPos();
  225. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.SaveSpoitData);
  226. var spoit = GameManager.Instance.SpoitValues[GameManager.Instance.SpoitIndex];
  227. List<SendSaveData> saveData = new List<SendSaveData>();
  228. SendSaveData data = new SendSaveData();
  229. data.id = spoit.id;
  230. data.name = spoit.name;
  231. data.projectId = DataManager.Instance.ProjectID;
  232. data.material = spoit.material;
  233. data.spoittf = spoit.spoitTf;
  234. //data.triggerRange = spoit.SpoitTransform[0];
  235. saveData.Add(data);
  236. string sendData = JsonConvert.SerializeObject(saveData);
  237. //string sendData = JsonMapper.ToJson(saveData);
  238. Debug.Log("更新景点数据: " + sendData);
  239. HttpTool.Instance.PostTest("/viewpoint/update", sendData, SaveCallBack);
  240. //string str = JsonMapper.ToJson(Spoits);
  241. //FileManager.WriteFile(sendData, Application.persistentDataPath + "/a.json");
  242. //Debug.Log(str);
  243. }
  244. private void SaveCallBack(string message)
  245. {
  246. if (message == "UnityWebRequest Error")
  247. {
  248. InstantiateCommand Command =new InstantiateCommand(
  249. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  250. InstantiateSystem.Instance.BlueObject.NetErrorText);
  251. CommandExtensionSystem.Instance.Send(Command);
  252. return;
  253. }
  254. Debug.Log("SaveCallBack: " + message);
  255. JObject jObject = JObject.Parse(message);
  256. string str = jObject["message"].ToString();
  257. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.SaveSpoitDataEnd);
  258. ((LoadingPanel)UIManager.Instance.GetUI(UINameConfig.LoadingPanel)).TextStr = str;
  259. }
  260. public void RemoveMat(GameObject obj)
  261. {
  262. StartCoroutine(WaitRemove(obj));
  263. }
  264. IEnumerator WaitRemove(GameObject obj)
  265. {
  266. Debug.Log("删除整个景点数据");
  267. //删除整个景点数据
  268. if (obj == m_ShowObj)
  269. {
  270. for (int i = 0; i < obj.transform.childCount; i++)
  271. {
  272. if (obj.transform.GetChild(i) != obj)
  273. {
  274. Destroy(obj.transform.GetChild(i).gameObject);
  275. }
  276. }
  277. yield return new WaitForEndOfFrame();
  278. //m_ShowObj = null;
  279. m_ShowObjValues.ChildrenObj.Clear();
  280. m_IsSelect = false;
  281. var spoit = GameManager.Instance.SpoitValues[GameManager.Instance.SpoitIndex];
  282. spoit.material = new List<MaterialObl>();
  283. GameManager.Instance.SaveSpoitObjectValue(m_ShowObjValues);
  284. GameManager.Instance.SpoitValues[GameManager.Instance.SpoitIndex] = spoit;
  285. SaveSpoits();
  286. }
  287. else
  288. {
  289. for (int i = 0; i < m_Btns.Count; i++)
  290. {
  291. m_Btns[i].SaveMatObj();
  292. if (m_Btns[i].MaterialObj == obj)
  293. {
  294. m_Btns[i].IsSelect = false;
  295. //var objvalue = m_Btns[i].ObjValue;
  296. Destroy(obj);
  297. yield return new WaitForEndOfFrame();
  298. }
  299. }
  300. SaveMatObj();
  301. }
  302. GameManager.Instance.UpdateEditorUI();
  303. }
  304. public void HandleEvent(EventData data)
  305. {
  306. switch (data.ID)
  307. {
  308. case EventID.EVENT_1:
  309. EventData1 data1 = (EventData1)data;
  310. if (data1.SelectObj == m_ShowObj)
  311. {
  312. m_IsSelect = true;
  313. }
  314. else
  315. {
  316. m_IsSelect = false;
  317. }
  318. break;
  319. case EventID.EVENT_2:
  320. break;
  321. default:
  322. break;
  323. }
  324. }
  325. }