ArtMoreEditor.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. using SC.XR.Unity;
  2. using ShadowStudio.Tool;
  3. using ShadowStudio.UI;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Dynamic;
  8. using System.Text.RegularExpressions;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using XRTool.UI;
  12. using XRTool.Util;
  13. using XRTool.WorldUI;
  14. namespace ShadowStudio.Model
  15. {
  16. /// <summary>
  17. /// 资源的编辑
  18. /// </summary>
  19. [RequireComponent(typeof(WorldDlg))]
  20. public class ArtMoreEditor : UnitySingleton<ArtMoreEditor>
  21. {
  22. private ArtContainer artContainer;
  23. private Button lastStepBtn;
  24. private Button nextStepBtn;
  25. private ToggleButton editToggleBtn;
  26. private Button modelInfoBtn;
  27. private Button copyBtn;
  28. //private Button deleteBtn;
  29. private WorldDlg dlg;
  30. private XRIcon lastIcon;
  31. private XRIcon nextIcon;
  32. public float checkTime = 1;
  33. private float lastTime;
  34. //private Transform modelInfoBtnBack;
  35. //private Transform SetDlg;
  36. private SCInputField posXInputField;
  37. private SCInputField posYInputField;
  38. private SCInputField posZInputField;
  39. private SCInputField rotXInputField;
  40. private SCInputField rotYInputField;
  41. private SCInputField rotZInputField;
  42. private SCInputField scaXInputField;
  43. private SCInputField scaYInputField;
  44. private SCInputField scaZInputField;
  45. private TextMesh distanceText;
  46. private Toggle lineToggle_Switch;
  47. private GameObject Distanceline;
  48. public string defaultShader = "Mixed Reality Toolkit/Standard";
  49. private TextMesh CentreTextMesh;
  50. private Material defMate;
  51. private void Start()
  52. {
  53. //if (!dlg)
  54. //{
  55. // dlg = GetComponent<WorldDlg>();
  56. //}
  57. //XRIcon3D[] bts = dlg.Root.GetComponentsInChildren<XRIcon3D>();
  58. //var combinedMesh = new Mesh();
  59. //for (int i = 0; i < bts.Length; i++)
  60. //{
  61. // var mesh = bts[i].Body;
  62. // //combinedMesh.CombineMeshes
  63. //}
  64. Init();
  65. }
  66. private void Init()
  67. {
  68. if (dlg == null)
  69. {
  70. dlg = GetComponent<WorldDlg>();
  71. lastStepBtn = dlg.GetBreadthChild<Button>("LastStepBtn");
  72. nextStepBtn = dlg.GetBreadthChild<Button>("NextStepBtn");
  73. editToggleBtn = dlg.GetBreadthChild<ToggleButton>("EditToggleBtn");
  74. editToggleBtn.OnValueChanged += OnClickEditValueChange;
  75. modelInfoBtn = dlg.GetBreadthChild<Button>("ModelInfoBtn");
  76. copyBtn = dlg.GetBreadthChild<Button>("CopyBtn");
  77. //deleteBtn = dlg.GetBreadthChild<Button>("DeleteBtn");
  78. lastIcon = dlg.GetBreadthChild<XRIcon>("LastIcon");
  79. nextIcon = dlg.GetBreadthChild<XRIcon>("NextIcon");
  80. lastStepBtn.onClick.AddListener(ClickOnLastStep);
  81. nextStepBtn.onClick.AddListener(ClickOnNextStep);
  82. copyBtn.onClick.AddListener(ClickOnCopy);
  83. //deleteBtn.onClick.AddListener(ClickOnDelete);
  84. //modelInfoBtnBack = dlg.GetBreadthChild<Transform>("ModelInfoBtnBack");
  85. //SetDlg = dlg.GetBreadthChild<Transform>("SetDlg");
  86. posXInputField = dlg.GetBreadthChild<SCInputField>("PosXInputField");
  87. posXInputField.onEndEdit.AddListener(InputPosXEnd);
  88. posYInputField = dlg.GetBreadthChild<SCInputField>("PosYInputField");
  89. posYInputField.onEndEdit.AddListener(InputPosYEnd);
  90. posZInputField = dlg.GetBreadthChild<SCInputField>("PosZInputField");
  91. posZInputField.onEndEdit.AddListener(InputPosZEnd);
  92. rotXInputField = dlg.GetBreadthChild<SCInputField>("RotXInputField");
  93. rotXInputField.onEndEdit.AddListener(InputRotXEnd);
  94. rotYInputField = dlg.GetBreadthChild<SCInputField>("RotYInputField");
  95. rotYInputField.onEndEdit.AddListener(InputRotYEnd);
  96. rotZInputField = dlg.GetBreadthChild<SCInputField>("RotZInputField");
  97. rotZInputField.onEndEdit.AddListener(InputRotZEnd);
  98. scaXInputField = dlg.GetBreadthChild<SCInputField>("ScaXInputField");
  99. scaXInputField.onEndEdit.AddListener(InputScaXEnd);
  100. scaYInputField = dlg.GetBreadthChild<SCInputField>("ScaYInputField");
  101. scaYInputField.onEndEdit.AddListener(InputScaYEnd);
  102. scaZInputField = dlg.GetBreadthChild<SCInputField>("ScaZInputField");
  103. scaZInputField.onEndEdit.AddListener(InputScaZEnd);
  104. distanceText = dlg.GetBreadthChild<TextMesh>("DistanceText");
  105. //modelInfoBtn.onClick.AddListener(ShowSetDlg);
  106. lineToggle_Switch = dlg.GetBreadthChild<Toggle>("LineXRToggle_Switch");
  107. lineToggle_Switch.onValueChanged.AddListener(ShowLine);
  108. RefreshUI();
  109. }
  110. }
  111. private void OnClickEditValueChange(bool ison)
  112. {
  113. Cleartime();
  114. if (artContainer != null)
  115. {
  116. if (ison)
  117. {
  118. artContainer.Transfer.IsJiXingScale = true;
  119. }
  120. else
  121. {
  122. artContainer.Transfer.IsJiXingScale = false;
  123. }
  124. if (ArtEditorContainer.Instance)
  125. {
  126. ArtEditorContainer.Instance.ReFresh();
  127. }
  128. }
  129. }
  130. private bool IsMatch(string input)
  131. {
  132. Regex reg = new Regex(@"^(\-|\+)?\d+(\.\d+)?$");
  133. bool isMatch = reg.IsMatch(input);
  134. return isMatch;
  135. }
  136. private void InputPosXEnd(string PosX)
  137. {
  138. if (artContainer != null)
  139. {
  140. bool ismatch = IsMatch(PosX);
  141. if (ismatch)
  142. {
  143. Vector3 pos = artContainer.Position;
  144. pos.x = float.Parse(PosX);
  145. artContainer.Position = pos;
  146. }
  147. else
  148. {
  149. posXInputField.text = artContainer.Position.x.ToString();
  150. }
  151. }
  152. }
  153. private void InputPosYEnd(string PosY)
  154. {
  155. if (artContainer != null)
  156. {
  157. bool ismatch = IsMatch(PosY);
  158. if (ismatch)
  159. {
  160. Vector3 pos = artContainer.Position;
  161. pos.y = float.Parse(PosY);
  162. artContainer.Position = pos;
  163. }
  164. else
  165. {
  166. posYInputField.text = artContainer.Position.y.ToString();
  167. }
  168. }
  169. }
  170. private void InputPosZEnd(string PosZ)
  171. {
  172. if (artContainer != null)
  173. {
  174. bool ismatch = IsMatch(PosZ);
  175. if (ismatch)
  176. {
  177. Vector3 pos = artContainer.Position;
  178. pos.z = float.Parse(PosZ);
  179. artContainer.Position = pos;
  180. }
  181. else
  182. {
  183. posZInputField.text = artContainer.Position.z.ToString();
  184. }
  185. }
  186. }
  187. private void InputRotXEnd(string RotX)
  188. {
  189. if (artContainer != null)
  190. {
  191. bool ismatch = IsMatch(RotX);
  192. if (ismatch)
  193. {
  194. Vector3 rot = artContainer.Angle;
  195. rot.x = float.Parse(RotX);
  196. artContainer.Angle = rot;
  197. }
  198. else
  199. {
  200. rotXInputField.text = artContainer.Angle.x.ToString();
  201. }
  202. }
  203. }
  204. private void InputRotYEnd(string RotY)
  205. {
  206. if (artContainer != null)
  207. {
  208. bool ismatch = IsMatch(RotY);
  209. if (ismatch)
  210. {
  211. Vector3 rot = artContainer.Angle;
  212. rot.y = float.Parse(RotY);
  213. artContainer.Angle = rot;
  214. }
  215. else
  216. {
  217. rotYInputField.text = artContainer.Angle.y.ToString();
  218. }
  219. }
  220. }
  221. private void InputRotZEnd(string RotZ)
  222. {
  223. if (artContainer != null)
  224. {
  225. bool ismatch = IsMatch(RotZ);
  226. if (ismatch)
  227. {
  228. Vector3 rot = artContainer.Angle;
  229. rot.z = float.Parse(RotZ);
  230. artContainer.Angle = rot;
  231. }
  232. else
  233. {
  234. rotZInputField.text = artContainer.Angle.z.ToString();
  235. }
  236. }
  237. }
  238. private void InputScaXEnd(string ScaX)
  239. {
  240. if (artContainer != null)
  241. {
  242. bool ismatch = IsMatch(ScaX);
  243. if (ismatch)
  244. {
  245. Vector3 sca = artContainer.Scale;
  246. sca.x = float.Parse(ScaX);
  247. artContainer.Scale = sca;
  248. }
  249. else
  250. {
  251. scaXInputField.text = artContainer.Scale.x.ToString();
  252. }
  253. }
  254. }
  255. private void InputScaYEnd(string ScaY)
  256. {
  257. if (artContainer != null)
  258. {
  259. bool ismatch = IsMatch(ScaY);
  260. if (ismatch)
  261. {
  262. Vector3 sca = artContainer.Scale;
  263. sca.y = float.Parse(ScaY);
  264. artContainer.Scale = sca;
  265. }
  266. else
  267. {
  268. scaYInputField.text = artContainer.Scale.y.ToString();
  269. }
  270. }
  271. }
  272. private void InputScaZEnd(string ScaZ)
  273. {
  274. if (artContainer != null)
  275. {
  276. bool ismatch = IsMatch(ScaZ);
  277. if (ismatch)
  278. {
  279. Vector3 sca = artContainer.Scale;
  280. sca.z = float.Parse(ScaZ);
  281. artContainer.Scale = sca;
  282. }
  283. else
  284. {
  285. scaZInputField.text = artContainer.Scale.z.ToString();
  286. }
  287. }
  288. }
  289. bool isshow;
  290. //private void ShowSetDlg()
  291. //{
  292. // Cleartime();
  293. // if (!isshow)
  294. // {
  295. // SetDlg.gameObject.SetActive(true);
  296. // modelInfoBtnBack.gameObject.SetActive(true);
  297. // isshow = true;
  298. // lineToggle_Switch.onValueChanged.RemoveListener(ShowLine);
  299. // lineToggle_Switch.onValueChanged.AddListener(ShowLine);
  300. // }
  301. // else
  302. // {
  303. // SetDlg.gameObject.SetActive(false);
  304. // modelInfoBtnBack.gameObject.SetActive(false);
  305. // lineToggle_Switch.onValueChanged.RemoveListener(ShowLine);
  306. // isshow = false;
  307. // }
  308. //}
  309. private void ShowLine(bool ison)
  310. {
  311. if (artContainer != null)
  312. {
  313. if (ison)
  314. {
  315. ShowLine();
  316. }
  317. else
  318. {
  319. if (Distanceline)
  320. {
  321. if (CentreTextMesh)
  322. {
  323. CentreTextMesh.gameObject.SetActive(false);
  324. }
  325. Destroy(Distanceline);
  326. }
  327. }
  328. }
  329. }
  330. private void ShowLine()
  331. {
  332. if (Distanceline)
  333. {
  334. Destroy(Distanceline);
  335. }
  336. Distanceline = new GameObject("Distanceline", typeof(LineRenderer));
  337. LineRenderer line = Distanceline.GetComponent<LineRenderer>();
  338. if (!line)
  339. {
  340. Distanceline.AddComponent<LineRenderer>();
  341. }
  342. if (!defMate)
  343. {
  344. defMate = new Material(Shader.Find(defaultShader));
  345. }
  346. if (line.material)
  347. {
  348. UnityUtil.CopyMate(line.material, defMate);
  349. }
  350. else
  351. {
  352. line.material = defMate;
  353. }
  354. if (artContainer != null)
  355. {
  356. Vector3 Startpos = GameSession.Instance.BoadyPosition;
  357. Vector3 Endpos = GameNode.Instance.transform.TransformPoint(artContainer.Position);
  358. line.SetPosition(0, Startpos);
  359. line.SetPosition(1, Endpos);
  360. line.startWidth = 0.002f;
  361. line.endWidth = 0.002f;
  362. UnityUtil.ChangeMateColor(line, Color.blue);
  363. if (!CentreTextMesh)
  364. {
  365. CentreTextMesh = InstanceTextmesh();
  366. }
  367. else
  368. {
  369. CentreTextMesh.gameObject.SetActive(true);
  370. }
  371. CentreTextMesh.transform.position = BetweenPos(Startpos, Endpos) + new Vector3(0f, 0.03f, 0f);
  372. CentreTextMesh.transform.LookAt(Endpos);
  373. CentreTextMesh.text = distanceText.text;
  374. }
  375. GameNode.Instance.SetParent(ObjNode.World, Distanceline.transform,
  376. Vector3.zero, Vector3.zero, Vector3.one, true);
  377. if (CentreTextMesh.transform.parent == null)
  378. {
  379. CentreTextMesh.transform.parent = Distanceline.transform.parent;
  380. }
  381. }
  382. /// <summary>
  383. /// 实例化TextMesh
  384. /// </summary>
  385. /// <returns></returns>
  386. private TextMesh InstanceTextmesh()
  387. {
  388. TextMesh centreTextMesh = null;
  389. var textmesh = Resources.Load("CentreTextMesh");
  390. if (textmesh)
  391. {
  392. GameObject obj = GameObject.Instantiate(textmesh) as GameObject;
  393. centreTextMesh = obj.transform.GetComponent<TextMesh>();
  394. }
  395. return centreTextMesh;
  396. }
  397. /// <summary>
  398. /// 返回两个点中心点的坐标
  399. /// </summary>
  400. /// <param name="pos1"></param>
  401. /// <param name="pos2"></param>
  402. /// <returns></returns>
  403. private Vector3 BetweenPos(Vector3 pos1, Vector3 pos2)
  404. {
  405. return (pos1 + pos2) / 2;
  406. }
  407. //private void ClickOnDelete()
  408. //{
  409. // Cleartime();
  410. // if (artContainer != null)
  411. // {
  412. // if (FileListDlg.ChoiceKuang)
  413. // {
  414. // FileListDlg.ChoiceKuang.transform.SetParent(FileListDlg.Instance.transform);
  415. // FileListDlg.ChoiceKuang.SetActive(false);
  416. // }
  417. // if (ItemFileList.Instance)
  418. // {
  419. // ItemFileList.Instance.DeleteFileIcon(this.artContainer);
  420. // }
  421. // }
  422. //}
  423. private void ClickOnCopy()
  424. {
  425. Cleartime();
  426. if (artContainer != null)
  427. {
  428. artContainer.CloneContainer();
  429. }
  430. }
  431. private void ClickOnNextStep()
  432. {
  433. Cleartime();
  434. if (artContainer != null)
  435. {
  436. if (artContainer.AheadCount() > 0)
  437. {
  438. artContainer.GoAhead();
  439. }
  440. RefreshUI();
  441. }
  442. }
  443. private void ClickOnLastStep()
  444. {
  445. Cleartime();
  446. if (artContainer != null)
  447. {
  448. if (artContainer.BackCount() > 0)
  449. {
  450. artContainer.GoBack();
  451. }
  452. RefreshUI();
  453. }
  454. }
  455. public void SetTarget(ArtContainer art, bool isDel)
  456. {
  457. Init();
  458. if (isDel)
  459. {
  460. try
  461. {
  462. if (this.artContainer != null && artContainer.Transfer)
  463. {
  464. artContainer.Transfer.TransferStateChange -= OnTransferStateChange;
  465. }
  466. }
  467. catch
  468. {
  469. UnityLog.Instance.LogError("菜单编辑器错误");
  470. }
  471. this.artContainer = null;
  472. //gameObject.SetActive(false);
  473. return;
  474. }
  475. this.artContainer = art;
  476. if (art.Transfer)
  477. {
  478. art.Transfer.TransferStateChange += OnTransferStateChange;
  479. }
  480. var container = art.GetEditorCenter();
  481. if (container)
  482. {
  483. //transform.position = container.position;
  484. //transform.rotation = container.rotation;
  485. RefreshUI();
  486. }
  487. RefreshInfo();
  488. Refreshline();
  489. ReFresh();
  490. }
  491. public void ReFresh()
  492. {
  493. if (artContainer != null && artContainer.Transfer)
  494. {
  495. if (artContainer.Transfer.IsJiXingScale)
  496. {
  497. editToggleBtn.isOn = true;
  498. editToggleBtn.image.sprite = editToggleBtn.openImg;
  499. }
  500. else
  501. {
  502. editToggleBtn.isOn = false;
  503. editToggleBtn.image.sprite = editToggleBtn.closeImg;
  504. }
  505. }
  506. }
  507. private void OnTransferStateChange(TransferState state)
  508. {
  509. try
  510. {
  511. if (artContainer != null && artContainer.GetInstace())
  512. {
  513. if (state == TransferState.Stop)
  514. {
  515. lastTime = 0;
  516. //if (!gameObject.activeSelf)
  517. //{
  518. // gameObject.SetActive(true);
  519. //}
  520. RefreshUI();
  521. }
  522. else
  523. {
  524. //gameObject.SetActive(false);
  525. }
  526. }
  527. }
  528. catch
  529. {
  530. //gameObject.SetActive(false);
  531. }
  532. }
  533. /// <summary>
  534. /// 编辑同步和
  535. /// </summary>
  536. private void Update()
  537. {
  538. if (artContainer != null)
  539. {
  540. try
  541. {
  542. if (!artContainer.GetInstace())
  543. {
  544. artContainer = null;
  545. return;
  546. }
  547. }
  548. catch
  549. {
  550. artContainer = null;
  551. return;
  552. }
  553. if (Input.GetKeyDown(KeyCode.Space))
  554. {
  555. UnityLog.Instance.Log("Back:" + artContainer.BackCount() + " Ahead:" + artContainer.AheadCount());
  556. artContainer.CloneContainer();
  557. }
  558. //if (Time.time - lastTime > checkTime)
  559. //{
  560. // lastTime = Time.time;
  561. // artContainer.ResetEditorPosition();
  562. // var container = artContainer.GetEditorCenter();
  563. // if (container)
  564. // {
  565. // transform.position = container.position;
  566. // transform.rotation = container.rotation;
  567. // }
  568. //}
  569. }
  570. }
  571. private void RefreshInfo()
  572. {
  573. if (artContainer != null)
  574. {
  575. posXInputField.text = artContainer.Position.x.ToString();
  576. posYInputField.text = artContainer.Position.y.ToString();
  577. posZInputField.text = artContainer.Position.z.ToString();
  578. rotXInputField.text = artContainer.Angle.x.ToString();
  579. rotYInputField.text = artContainer.Angle.y.ToString();
  580. rotZInputField.text = artContainer.Angle.z.ToString();
  581. scaXInputField.text = artContainer.Scale.x.ToString();
  582. scaYInputField.text = artContainer.Scale.y.ToString();
  583. scaZInputField.text = artContainer.Scale.z.ToString();
  584. distanceText.text = artContainer.Distance.ToString("f2");//保留两位小数
  585. }
  586. }
  587. private Color activeColor = Color.white;
  588. private Color disActiveColor = Color.gray;
  589. private void RefreshUI()
  590. {
  591. bool isSelect = artContainer != null;
  592. Color color = isSelect ? activeColor : disActiveColor;
  593. lastStepBtn.enabled = isSelect && artContainer.BackCount() > 0;
  594. lastStepBtn.image.color = isSelect && artContainer.BackCount() > 0? activeColor: disActiveColor;
  595. nextStepBtn.enabled = isSelect && artContainer.AheadCount() > 0;
  596. nextStepBtn.image.color = isSelect && artContainer.AheadCount() > 0 ? activeColor : disActiveColor;
  597. copyBtn.enabled = isSelect;
  598. copyBtn.image.color = color;
  599. lineToggle_Switch.enabled = isSelect;
  600. editToggleBtn.enabled = isSelect;
  601. editToggleBtn.image.color = color;
  602. if (!isSelect && lineToggle_Switch.isOn)
  603. {
  604. lineToggle_Switch.isOn = false;
  605. }
  606. if (!isSelect && editToggleBtn.isOn)
  607. {
  608. editToggleBtn.isOn = false;
  609. }
  610. }
  611. private void Refreshline()
  612. {
  613. if (artContainer != null && lineToggle_Switch.isOn)
  614. {
  615. ShowLine();
  616. }
  617. }
  618. private void Cleartime()
  619. {
  620. if (artContainer != null)
  621. {
  622. artContainer.Transfer.ClearTime();
  623. }
  624. }
  625. public void DeleteRefesh()
  626. {
  627. lastStepBtn.enabled = false;
  628. lastIcon.color = Color.gray;
  629. nextStepBtn.enabled = false;
  630. nextIcon.color = Color.gray;
  631. lineToggle_Switch.isOn = false;
  632. ShowLine(false);
  633. posXInputField.text = "0";
  634. posYInputField.text = "0";
  635. posZInputField.text = "0";
  636. rotXInputField.text = "0";
  637. rotYInputField.text = "0";
  638. rotZInputField.text = "0";
  639. scaXInputField.text = "0";
  640. scaYInputField.text = "0";
  641. scaZInputField.text = "0";
  642. distanceText.text = "";
  643. }
  644. }
  645. }