UIEditorPanel.cs 12 KB

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