SideSlipGuideStepContentPropertyDrawer.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #if UNITY_EDITOR
  2. using AppUI.View.Component;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace AppUI.Editor
  6. {
  7. [CustomPropertyDrawer(typeof(SideSlipGuideStepContent))]
  8. public class SideSlipGuideStepContentPropertyDrawer : PropertyDrawer
  9. {
  10. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  11. {
  12. if (!property.isExpanded)
  13. return EditorGUIUtility.singleLineHeight;
  14. float height = EditorGUIUtility.singleLineHeight;
  15. height += GetToggleBlockHeight(property, "useImage", "image");
  16. height += GetToggleBlockHeight(property, "useDescription", "description");
  17. height += GetToggleBlockHeight(property, "useDescriptionKey", "descriptionKey");
  18. height += GetToggleBlockHeight(property, "useDeviceVisualRoot", "deviceVisualRoot");
  19. return height;
  20. }
  21. static float GetToggleBlockHeight(SerializedProperty property, string toggleName, string fieldName)
  22. {
  23. float height = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  24. SerializedProperty toggle = property.FindPropertyRelative(toggleName);
  25. if (toggle != null && toggle.boolValue)
  26. {
  27. SerializedProperty field = property.FindPropertyRelative(fieldName);
  28. if (field != null)
  29. height += EditorGUI.GetPropertyHeight(field, true) + EditorGUIUtility.standardVerticalSpacing;
  30. }
  31. return height;
  32. }
  33. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  34. {
  35. EditorGUI.BeginProperty(position, label, property);
  36. Rect rect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
  37. property.isExpanded = EditorGUI.Foldout(rect, property.isExpanded, label, true);
  38. if (!property.isExpanded)
  39. {
  40. EditorGUI.EndProperty();
  41. return;
  42. }
  43. EditorGUI.indentLevel++;
  44. rect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  45. rect = DrawToggleField(rect, property, "useImage", "image", "Image");
  46. rect = DrawToggleField(rect, property, "useDescription", "description", "Description");
  47. rect = DrawToggleField(rect, property, "useDescriptionKey", "descriptionKey", "Description Key");
  48. DrawToggleField(rect, property, "useDeviceVisualRoot", "deviceVisualRoot", "Device Visual Root");
  49. EditorGUI.indentLevel--;
  50. EditorGUI.EndProperty();
  51. }
  52. static Rect DrawToggleField(
  53. Rect rect,
  54. SerializedProperty property,
  55. string toggleName,
  56. string fieldName,
  57. string label)
  58. {
  59. SerializedProperty toggle = property.FindPropertyRelative(toggleName);
  60. SerializedProperty field = property.FindPropertyRelative(fieldName);
  61. float lineHeight = EditorGUIUtility.singleLineHeight;
  62. Rect toggleRect = new Rect(rect.x, rect.y, rect.width, lineHeight);
  63. EditorGUI.PropertyField(toggleRect, toggle, new GUIContent(label));
  64. rect.y += lineHeight + EditorGUIUtility.standardVerticalSpacing;
  65. if (toggle.boolValue && field != null)
  66. {
  67. float fieldHeight = EditorGUI.GetPropertyHeight(field, true);
  68. Rect fieldRect = new Rect(rect.x, rect.y, rect.width, fieldHeight);
  69. EditorGUI.PropertyField(fieldRect, field, GUIContent.none, true);
  70. rect.y += fieldHeight + EditorGUIUtility.standardVerticalSpacing;
  71. }
  72. return rect;
  73. }
  74. }
  75. }
  76. #endif