WorldDlgEditor.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor.UI;
  5. using UnityEditor;
  6. using XRTool.Util;
  7. namespace XRTool.WorldUI
  8. {
  9. [InitializeOnLoad]
  10. [CustomEditor(typeof(WorldDlg))]
  11. public class WorldDlgEditor : Editor
  12. {
  13. private WorldDlg worldDlg;
  14. private Vector2 size;
  15. private SerializedProperty isShowTitle;
  16. private SerializedProperty titleDis;
  17. [MenuItem("GameObject/XRUI/WorldDlg", priority = 0)]
  18. static void Init()
  19. {
  20. var obj = Instantiate(Resources.Load<WorldDlg>(typeof(WorldDlg).Name));
  21. obj.name = (typeof(WorldDlg).Name);
  22. if (obj)
  23. {
  24. var parent = Selection.activeGameObject;
  25. if (!parent)
  26. {
  27. var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
  28. if (canvas != null)
  29. {
  30. for (int i = 0; i < canvas.Length; i++)
  31. {
  32. if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
  33. {
  34. parent = (canvas[i] as Canvas).gameObject;
  35. break;
  36. }
  37. }
  38. }
  39. }
  40. obj.transform.SetParent(parent.transform);
  41. Selection.activeGameObject = obj.gameObject;
  42. }
  43. }
  44. private void OnEnable()
  45. {
  46. isShowTitle = serializedObject.FindProperty("isShowTitle");
  47. titleDis = serializedObject.FindProperty("titleDis");
  48. worldDlg = target as WorldDlg;
  49. if (worldDlg)
  50. {
  51. if(worldDlg.BG)
  52. worldDlg.BG.InitComponent();
  53. size = worldDlg.DlgTrans.rect.size;
  54. worldDlg.AutoSetScale(worldDlg.DlgTrans, size);
  55. worldDlg.BG.UpdateSize(size * WorldDlg.UIScale);
  56. worldDlg.SetBoundSize(worldDlg.BG.Back, worldDlg.dragOffset);
  57. worldDlg.XRTitle.gameObject.SetActive(worldDlg.isShowTitle);
  58. }
  59. }
  60. public override void OnInspectorGUI()
  61. {
  62. base.OnInspectorGUI();
  63. EditorGUILayout.PropertyField(isShowTitle, new GUIContent("是否显示标题"));
  64. if (worldDlg.isShowTitle)
  65. {
  66. EditorGUILayout.PropertyField(titleDis, new GUIContent("标题板距离"));
  67. }
  68. serializedObject.ApplyModifiedProperties();
  69. if (worldDlg.DlgTrans.rect.size != size)
  70. {
  71. worldDlg.SetScale(worldDlg.DlgTrans.rect.size);
  72. }
  73. if (GUI.changed)
  74. {
  75. worldDlg.XRTitle.gameObject.SetActive(worldDlg.isShowTitle);
  76. if (worldDlg.isShowTitle)
  77. {
  78. var pos = worldDlg.BG.Back.anchoredPosition3D;
  79. pos.y = worldDlg.titleDis + worldDlg.XRTitle.rectTransform.rect.size.y / 2;
  80. worldDlg.BG.Back.anchoredPosition3D = pos;
  81. }
  82. else
  83. {
  84. worldDlg.BG.Back.anchoredPosition3D = Vector3.zero;
  85. }
  86. worldDlg.SetBoundSize(worldDlg.BG.Back, worldDlg.dragOffset);
  87. worldDlg.XRTitle.gameObject.SetActive(worldDlg.isShowTitle);
  88. worldDlg.AutoSetTitle(size);
  89. }
  90. }
  91. }
  92. }