UIPanelEditor.cs 6.7 KB

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