UIPanelEditor.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.SceneManagement;
  4. namespace FairyGUIEditor
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. [CustomEditor(typeof(FairyGUI.UIPanel))]
  10. public class UIPanelEditor : Editor
  11. {
  12. SerializedProperty packageName;
  13. SerializedProperty componentName;
  14. SerializedProperty packagePath;
  15. SerializedProperty renderMode;
  16. SerializedProperty renderCamera;
  17. SerializedProperty sortingOrder;
  18. SerializedProperty position;
  19. SerializedProperty scale;
  20. SerializedProperty rotation;
  21. SerializedProperty fairyBatching;
  22. SerializedProperty fitScreen;
  23. SerializedProperty touchDisabled;
  24. SerializedProperty hitTestMode;
  25. SerializedProperty setNativeChildrenOrder;
  26. string[] propertyToExclude;
  27. void OnEnable()
  28. {
  29. packageName = serializedObject.FindProperty("packageName");
  30. componentName = serializedObject.FindProperty("componentName");
  31. packagePath = serializedObject.FindProperty("packagePath");
  32. renderMode = serializedObject.FindProperty("renderMode");
  33. renderCamera = serializedObject.FindProperty("renderCamera");
  34. sortingOrder = serializedObject.FindProperty("sortingOrder");
  35. position = serializedObject.FindProperty("position");
  36. scale = serializedObject.FindProperty("scale");
  37. rotation = serializedObject.FindProperty("rotation");
  38. fairyBatching = serializedObject.FindProperty("fairyBatching");
  39. fitScreen = serializedObject.FindProperty("fitScreen");
  40. touchDisabled = serializedObject.FindProperty("touchDisabled");
  41. hitTestMode = serializedObject.FindProperty("hitTestMode");
  42. setNativeChildrenOrder = serializedObject.FindProperty("setNativeChildrenOrder");
  43. propertyToExclude = new string[] { "m_Script", "packageName", "componentName", "packagePath", "renderMode",
  44. "renderCamera", "sortingOrder", "position", "scale", "rotation", "fairyBatching", "fitScreen","touchDisabled",
  45. "hitTestMode","cachedUISize","setNativeChildrenOrder"
  46. };
  47. }
  48. public override void OnInspectorGUI()
  49. {
  50. serializedObject.Update();
  51. FairyGUI.UIPanel panel = target as FairyGUI.UIPanel;
  52. DrawPropertiesExcluding(serializedObject, propertyToExclude);
  53. EditorGUILayout.BeginHorizontal();
  54. EditorGUILayout.PrefixLabel("Package Name");
  55. if (GUILayout.Button(packageName.stringValue, "ObjectField"))
  56. EditorWindow.GetWindow<PackagesWindow>(true, "Select a UI Component").SetSelection(packageName.stringValue, componentName.stringValue);
  57. if (GUILayout.Button("Clear", GUILayout.Width(50)))
  58. {
  59. #if UNITY_2018_3_OR_NEWER
  60. bool isPrefab = PrefabUtility.GetPrefabAssetType(panel) != PrefabAssetType.NotAPrefab;
  61. #else
  62. bool isPrefab = PrefabUtility.GetPrefabType(panel) == PrefabType.Prefab;
  63. #endif
  64. panel.SendMessage("OnUpdateSource", new object[] { null, null, null, !isPrefab });
  65. EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  66. }
  67. EditorGUILayout.EndHorizontal();
  68. EditorGUILayout.BeginHorizontal();
  69. EditorGUILayout.PrefixLabel("Component Name");
  70. if (GUILayout.Button(componentName.stringValue, "ObjectField"))
  71. EditorWindow.GetWindow<PackagesWindow>(true, "Select a UI Component").SetSelection(packageName.stringValue, componentName.stringValue);
  72. EditorGUILayout.EndHorizontal();
  73. EditorGUILayout.BeginHorizontal();
  74. EditorGUILayout.PrefixLabel("Package Path");
  75. EditorGUILayout.LabelField(packagePath.stringValue, (GUIStyle)"helpbox");
  76. EditorGUILayout.EndHorizontal();
  77. if (Application.isPlaying)
  78. EditorGUILayout.EnumPopup("Render Mode", panel.container.renderMode);
  79. else
  80. EditorGUILayout.PropertyField(renderMode);
  81. if ((RenderMode)renderMode.enumValueIndex != RenderMode.ScreenSpaceOverlay)
  82. EditorGUILayout.PropertyField(renderCamera);
  83. int oldSortingOrder = panel.sortingOrder;
  84. EditorGUILayout.PropertyField(sortingOrder);
  85. EditorGUILayout.PropertyField(fairyBatching);
  86. EditorGUILayout.PropertyField(hitTestMode);
  87. EditorGUILayout.PropertyField(touchDisabled);
  88. EditorGUILayout.PropertyField(setNativeChildrenOrder);
  89. EditorGUILayout.Separator();
  90. EditorGUILayout.LabelField("UI Transform", (GUIStyle)"OL Title");
  91. EditorGUILayout.Separator();
  92. EditorGUI.BeginChangeCheck();
  93. EditorGUILayout.PropertyField(position);
  94. EditorGUILayout.PropertyField(rotation);
  95. EditorGUILayout.PropertyField(scale);
  96. EditorGUILayout.Space();
  97. FairyGUI.FitScreen oldFitScreen = (FairyGUI.FitScreen)fitScreen.enumValueIndex;
  98. EditorGUILayout.PropertyField(fitScreen);
  99. if (serializedObject.ApplyModifiedProperties())
  100. {
  101. #if UNITY_2018_3_OR_NEWER
  102. bool isPrefab = PrefabUtility.GetPrefabAssetType(panel) != PrefabAssetType.NotAPrefab;
  103. #else
  104. bool isPrefab = PrefabUtility.GetPrefabType(panel) == PrefabType.Prefab;
  105. #endif
  106. if (!isPrefab)
  107. {
  108. panel.ApplyModifiedProperties(sortingOrder.intValue != oldSortingOrder, (FairyGUI.FitScreen)fitScreen.enumValueIndex != oldFitScreen);
  109. }
  110. }
  111. }
  112. void OnSceneGUI()
  113. {
  114. FairyGUI.UIPanel panel = (target as FairyGUI.UIPanel);
  115. if (panel.container == null)
  116. return;
  117. Vector3 pos = panel.GetUIWorldPosition();
  118. float sizeFactor = HandleUtility.GetHandleSize(pos);
  119. #if UNITY_2022_2_OR_NEWER
  120. Vector3 newPos = Handles.FreeMoveHandle(pos, sizeFactor, Vector3.one, Handles.ArrowHandleCap);
  121. #elif UNITY_2017_1_OR_NEWER
  122. Vector3 newPos = Handles.FreeMoveHandle(pos, Quaternion.identity, sizeFactor, Vector3.one, Handles.ArrowHandleCap);
  123. #else
  124. Vector3 newPos = Handles.FreeMoveHandle(pos, Quaternion.identity, sizeFactor, Vector3.one, Handles.ArrowCap);
  125. #endif
  126. if (newPos != pos)
  127. {
  128. Vector2 v1 = HandleUtility.WorldToGUIPoint(pos);
  129. Vector2 v2 = HandleUtility.WorldToGUIPoint(newPos);
  130. Vector3 delta = v2 - v1;
  131. delta.x = (int)delta.x;
  132. delta.y = (int)delta.y;
  133. panel.MoveUI(delta);
  134. }
  135. }
  136. }
  137. }