UIVerticalLayoutGroupEditor.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #if UNITY_EDITOR
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace AdaptUI
  6. {
  7. [CustomEditor(typeof(UIVerticalLayoutGroup))]
  8. [CanEditMultipleObjects]
  9. public class UIVerticalLayoutGroupEditor : Editor
  10. {
  11. private SerializedProperty defaultLayout;
  12. private SerializedProperty iPhoneLayout;
  13. private SerializedProperty iPadLayout;
  14. private void OnEnable()
  15. {
  16. defaultLayout = serializedObject.FindProperty("defaultLayout");
  17. iPhoneLayout = serializedObject.FindProperty("iPhoneLayout");
  18. iPadLayout = serializedObject.FindProperty("iPadLayout");
  19. }
  20. public override void OnInspectorGUI()
  21. {
  22. UIVerticalLayoutGroup script = (UIVerticalLayoutGroup)target;
  23. VerticalLayoutGroup vertical = script.GetComponent<VerticalLayoutGroup>();
  24. EditorGUILayout.Space();
  25. EditorGUILayout.LabelField("UI 垂直布局", EditorStyles.boldLabel);
  26. DrawLayoutEditor(script, "默认布局", ref script.defaultLayout, vertical);
  27. DrawLayoutEditor(script, "iPhone 布局", ref script.iPhoneLayout, vertical);
  28. DrawLayoutEditor(script, "iPad 布局", ref script.iPadLayout, vertical);
  29. EditorGUILayout.Space();
  30. if (GUILayout.Button("应用当前布局"))
  31. {
  32. script.ApplyLayout();
  33. }
  34. serializedObject.ApplyModifiedProperties();
  35. }
  36. private void DrawLayoutEditor(UIVerticalLayoutGroup script, string title, ref UILayoutData layout, VerticalLayoutGroup verticalLayout)
  37. {
  38. EditorGUILayout.LabelField(title, EditorStyles.boldLabel);
  39. EditorGUI.indentLevel++;
  40. EditorGUILayout.LabelField("Padding", EditorStyles.boldLabel);
  41. layout.paddingLeft = EditorGUILayout.IntField("Left", layout.paddingLeft);
  42. layout.paddingRight = EditorGUILayout.IntField("Right", layout.paddingRight);
  43. layout.paddingTop = EditorGUILayout.IntField("Top", layout.paddingTop);
  44. layout.paddingBottom = EditorGUILayout.IntField("Bottom", layout.paddingBottom);
  45. layout.spacing = EditorGUILayout.FloatField("Spacing", layout.spacing);
  46. layout.childAlignment = (TextAnchor)EditorGUILayout.EnumPopup("Child Alignment", layout.childAlignment);
  47. layout.reverseArrangement = EditorGUILayout.Toggle("Reverse Arrangement", layout.reverseArrangement);
  48. layout.controlChildWidth = EditorGUILayout.Toggle("Control Child Width", layout.controlChildWidth);
  49. layout.controlChildHeight = EditorGUILayout.Toggle("Control Child Height", layout.controlChildHeight);
  50. layout.useChildScaleWidth = EditorGUILayout.Toggle("Use Child Scale Width", layout.useChildScaleWidth);
  51. layout.useChildScaleHeight = EditorGUILayout.Toggle("Use Child Scale Height", layout.useChildScaleHeight);
  52. layout.childForceExpandWidth = EditorGUILayout.Toggle("Child Force Expand Width", layout.childForceExpandWidth);
  53. layout.childForceExpandHeight = EditorGUILayout.Toggle("Child Force Expand Height", layout.childForceExpandHeight);
  54. if (GUILayout.Button("应用 " + title + " 布局"))
  55. {
  56. verticalLayout.padding.left = layout.paddingLeft;
  57. verticalLayout.padding.right = layout.paddingRight;
  58. verticalLayout.padding.top = layout.paddingTop;
  59. verticalLayout.padding.bottom = layout.paddingBottom;
  60. verticalLayout.spacing = layout.spacing;
  61. verticalLayout.childAlignment = layout.childAlignment;
  62. verticalLayout.reverseArrangement = layout.reverseArrangement;
  63. verticalLayout.childControlWidth = layout.controlChildWidth;
  64. verticalLayout.childControlHeight = layout.controlChildHeight;
  65. verticalLayout.childScaleWidth = layout.useChildScaleWidth;
  66. verticalLayout.childScaleHeight = layout.useChildScaleHeight;
  67. verticalLayout.childForceExpandWidth = layout.childForceExpandWidth;
  68. verticalLayout.childForceExpandHeight = layout.childForceExpandHeight;
  69. Undo.RecordObject(script, "Set " + title + " Layout");
  70. EditorUtility.SetDirty(script);
  71. }
  72. EditorGUI.indentLevel--;
  73. EditorGUILayout.Space();
  74. }
  75. }
  76. }
  77. #endif