GraphicEditor.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System.Linq;
  2. using UnityEditor.AnimatedValues;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace UnityEditor.UI
  6. {
  7. /// <summary>
  8. /// Editor class used to edit UI Graphics.
  9. /// Extend this class to write your own graphic editor.
  10. /// </summary>
  11. [CustomEditor(typeof(MaskableGraphic), false)]
  12. [CanEditMultipleObjects]
  13. public class GraphicEditor : Editor
  14. {
  15. protected SerializedProperty m_Script;
  16. protected SerializedProperty m_Color;
  17. protected SerializedProperty m_Material;
  18. protected SerializedProperty m_RaycastTarget;
  19. protected SerializedProperty m_RaycastPadding;
  20. protected SerializedProperty m_Maskable;
  21. private GUIContent m_CorrectButtonContent;
  22. protected AnimBool m_ShowNativeSize;
  23. GUIContent m_PaddingContent;
  24. GUIContent m_LeftContent;
  25. GUIContent m_RightContent;
  26. GUIContent m_TopContent;
  27. GUIContent m_BottomContent;
  28. static private bool m_ShowPadding = false;
  29. protected virtual void OnDisable()
  30. {
  31. Tools.hidden = false;
  32. m_ShowNativeSize.valueChanged.RemoveListener(Repaint);
  33. }
  34. protected virtual void OnEnable()
  35. {
  36. m_CorrectButtonContent = EditorGUIUtility.TrTextContent("Set Native Size", "Sets the size to match the content.");
  37. m_PaddingContent = EditorGUIUtility.TrTextContent("Raycast Padding");
  38. m_LeftContent = EditorGUIUtility.TrTextContent("Left");
  39. m_RightContent = EditorGUIUtility.TrTextContent("Right");
  40. m_TopContent = EditorGUIUtility.TrTextContent("Top");
  41. m_BottomContent = EditorGUIUtility.TrTextContent("Bottom");
  42. m_Script = serializedObject.FindProperty("m_Script");
  43. m_Color = serializedObject.FindProperty("m_Color");
  44. m_Material = serializedObject.FindProperty("m_Material");
  45. m_RaycastTarget = serializedObject.FindProperty("m_RaycastTarget");
  46. m_RaycastPadding = serializedObject.FindProperty("m_RaycastPadding");
  47. m_Maskable = serializedObject.FindProperty("m_Maskable");
  48. m_ShowNativeSize = new AnimBool(false);
  49. m_ShowNativeSize.valueChanged.AddListener(Repaint);
  50. }
  51. public override void OnInspectorGUI()
  52. {
  53. serializedObject.Update();
  54. EditorGUILayout.PropertyField(m_Script);
  55. AppearanceControlsGUI();
  56. RaycastControlsGUI();
  57. MaskableControlsGUI();
  58. serializedObject.ApplyModifiedProperties();
  59. }
  60. /// <summary>
  61. /// Set if the 'Set Native Size' button should be visible for this editor.
  62. /// </summary>
  63. /// <param name="show">Are we showing or hiding the AnimBool for the size.</param>
  64. /// <param name="instant">Should the size AnimBool change instantly.</param>
  65. protected void SetShowNativeSize(bool show, bool instant)
  66. {
  67. if (instant)
  68. m_ShowNativeSize.value = show;
  69. else
  70. m_ShowNativeSize.target = show;
  71. }
  72. /// <summary>
  73. /// GUI for showing a button that sets the size of the RectTransform to the native size for this Graphic.
  74. /// </summary>
  75. protected void NativeSizeButtonGUI()
  76. {
  77. if (EditorGUILayout.BeginFadeGroup(m_ShowNativeSize.faded))
  78. {
  79. EditorGUILayout.BeginHorizontal();
  80. {
  81. GUILayout.Space(EditorGUIUtility.labelWidth);
  82. if (GUILayout.Button(m_CorrectButtonContent, EditorStyles.miniButton))
  83. {
  84. foreach (Graphic graphic in targets.Select(obj => obj as Graphic))
  85. {
  86. Undo.RecordObject(graphic.rectTransform, "Set Native Size");
  87. graphic.SetNativeSize();
  88. EditorUtility.SetDirty(graphic);
  89. }
  90. }
  91. }
  92. EditorGUILayout.EndHorizontal();
  93. }
  94. EditorGUILayout.EndFadeGroup();
  95. }
  96. protected void MaskableControlsGUI()
  97. {
  98. EditorGUILayout.PropertyField(m_Maskable);
  99. }
  100. /// <summary>
  101. /// GUI related to the appearance of the Graphic. Color and Material properties appear here.
  102. /// </summary>
  103. protected void AppearanceControlsGUI()
  104. {
  105. EditorGUILayout.PropertyField(m_Color);
  106. EditorGUILayout.PropertyField(m_Material);
  107. }
  108. /// <summary>
  109. /// GUI related to the Raycasting settings for the graphic.
  110. /// </summary>
  111. protected void RaycastControlsGUI()
  112. {
  113. EditorGUILayout.PropertyField(m_RaycastTarget);
  114. m_ShowPadding = EditorGUILayout.Foldout(m_ShowPadding, m_PaddingContent);
  115. if (m_ShowPadding)
  116. {
  117. using (var check = new EditorGUI.ChangeCheckScope())
  118. {
  119. EditorGUI.indentLevel++;
  120. Vector4 newPadding = m_RaycastPadding.vector4Value;
  121. newPadding.x = EditorGUILayout.FloatField(m_LeftContent, newPadding.x);
  122. newPadding.z = EditorGUILayout.FloatField(m_RightContent, newPadding.z);
  123. newPadding.w = EditorGUILayout.FloatField(m_TopContent, newPadding.w);
  124. newPadding.y = EditorGUILayout.FloatField(m_BottomContent, newPadding.y);
  125. if (check.changed)
  126. {
  127. m_RaycastPadding.vector4Value = newPadding;
  128. }
  129. EditorGUI.indentLevel--;
  130. }
  131. }
  132. }
  133. }
  134. }