WorldDlgEditor.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. {
  53. worldDlg.BG.InitComponent();
  54. //worldDlg.BG.UpdateSize(size * WorldDlg.UIScale);
  55. worldDlg.SetBoundSize(worldDlg.BG.Back, worldDlg.dragOffset);
  56. }
  57. size = worldDlg.DlgTrans.rect.size;
  58. worldDlg.AutoSetScale(worldDlg.DlgTrans, size);
  59. if (worldDlg.XRTitle)
  60. {
  61. worldDlg.XRTitle.gameObject.SetActive(worldDlg.isShowTitle);
  62. }
  63. }
  64. }
  65. public override void OnInspectorGUI()
  66. {
  67. base.OnInspectorGUI();
  68. EditorGUILayout.PropertyField(isShowTitle, new GUIContent("是否显示标题"));
  69. if (worldDlg.isShowTitle)
  70. {
  71. EditorGUILayout.PropertyField(titleDis, new GUIContent("标题板距离"));
  72. }
  73. serializedObject.ApplyModifiedProperties();
  74. if (worldDlg.DlgTrans.rect.size != size)
  75. {
  76. worldDlg.SetScale(worldDlg.DlgTrans.rect.size);
  77. }
  78. if (GUI.changed)
  79. {
  80. worldDlg.XRTitle.gameObject.SetActive(worldDlg.isShowTitle);
  81. if (worldDlg.isShowTitle)
  82. {
  83. var pos = worldDlg.BG.Back.anchoredPosition3D;
  84. pos.y = worldDlg.titleDis + worldDlg.XRTitle.rectTransform.rect.size.y / 2;
  85. worldDlg.BG.Back.anchoredPosition3D = pos;
  86. }
  87. else
  88. {
  89. worldDlg.BG.Back.anchoredPosition3D = Vector3.zero;
  90. }
  91. worldDlg.SetBoundSize(worldDlg.BG.Back, worldDlg.dragOffset);
  92. worldDlg.XRTitle.gameObject.SetActive(worldDlg.isShowTitle);
  93. worldDlg.AutoSetTitle(size);
  94. }
  95. }
  96. }
  97. }