暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LayoutElementEditor.cs 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEditorInternal;
  4. using UnityEditor.AnimatedValues;
  5. namespace UnityEditor.UI
  6. {
  7. [CustomEditor(typeof(LayoutElement), true)]
  8. [CanEditMultipleObjects]
  9. /// <summary>
  10. /// Custom editor for the LayoutElement component
  11. /// Extend this class to write a custom editor for a component derived from LayoutElement.
  12. /// </summary>
  13. public class LayoutElementEditor : Editor
  14. {
  15. SerializedProperty m_IgnoreLayout;
  16. SerializedProperty m_MinWidth;
  17. SerializedProperty m_MinHeight;
  18. SerializedProperty m_PreferredWidth;
  19. SerializedProperty m_PreferredHeight;
  20. SerializedProperty m_FlexibleWidth;
  21. SerializedProperty m_FlexibleHeight;
  22. SerializedProperty m_LayoutPriority;
  23. protected virtual void OnEnable()
  24. {
  25. m_IgnoreLayout = serializedObject.FindProperty("m_IgnoreLayout");
  26. m_MinWidth = serializedObject.FindProperty("m_MinWidth");
  27. m_MinHeight = serializedObject.FindProperty("m_MinHeight");
  28. m_PreferredWidth = serializedObject.FindProperty("m_PreferredWidth");
  29. m_PreferredHeight = serializedObject.FindProperty("m_PreferredHeight");
  30. m_FlexibleWidth = serializedObject.FindProperty("m_FlexibleWidth");
  31. m_FlexibleHeight = serializedObject.FindProperty("m_FlexibleHeight");
  32. m_LayoutPriority = serializedObject.FindProperty("m_LayoutPriority");
  33. }
  34. public override void OnInspectorGUI()
  35. {
  36. serializedObject.Update();
  37. EditorGUILayout.PropertyField(m_IgnoreLayout);
  38. if (!m_IgnoreLayout.boolValue)
  39. {
  40. EditorGUILayout.Space();
  41. LayoutElementField(m_MinWidth, 0);
  42. LayoutElementField(m_MinHeight, 0);
  43. LayoutElementField(m_PreferredWidth, t => t.rect.width);
  44. LayoutElementField(m_PreferredHeight, t => t.rect.height);
  45. LayoutElementField(m_FlexibleWidth, 1);
  46. LayoutElementField(m_FlexibleHeight, 1);
  47. }
  48. EditorGUILayout.PropertyField(m_LayoutPriority);
  49. serializedObject.ApplyModifiedProperties();
  50. }
  51. void LayoutElementField(SerializedProperty property, float defaultValue)
  52. {
  53. LayoutElementField(property, _ => defaultValue);
  54. }
  55. void LayoutElementField(SerializedProperty property, System.Func<RectTransform, float> defaultValue)
  56. {
  57. Rect position = EditorGUILayout.GetControlRect();
  58. // Label
  59. GUIContent label = EditorGUI.BeginProperty(position, null, property);
  60. // Rects
  61. Rect fieldPosition = EditorGUI.PrefixLabel(position, label);
  62. Rect toggleRect = fieldPosition;
  63. toggleRect.width = 16;
  64. Rect floatFieldRect = fieldPosition;
  65. floatFieldRect.xMin += 16;
  66. // Checkbox
  67. EditorGUI.BeginChangeCheck();
  68. bool enabled = EditorGUI.ToggleLeft(toggleRect, GUIContent.none, property.floatValue >= 0);
  69. if (EditorGUI.EndChangeCheck())
  70. {
  71. // This could be made better to set all of the targets to their initial width, but mimizing code change for now
  72. property.floatValue = (enabled ? defaultValue((target as LayoutElement).transform as RectTransform) : -1);
  73. }
  74. if (!property.hasMultipleDifferentValues && property.floatValue >= 0)
  75. {
  76. // Float field
  77. EditorGUIUtility.labelWidth = 4; // Small invisible label area for drag zone functionality
  78. EditorGUI.BeginChangeCheck();
  79. float newValue = EditorGUI.FloatField(floatFieldRect, new GUIContent(" "), property.floatValue);
  80. if (EditorGUI.EndChangeCheck())
  81. {
  82. property.floatValue = Mathf.Max(0, newValue);
  83. }
  84. EditorGUIUtility.labelWidth = 0;
  85. }
  86. EditorGUI.EndProperty();
  87. }
  88. }
  89. }