LayoutPropertiesPreview.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.Globalization;
  6. namespace UnityEditor.Events
  7. {
  8. [CustomPreview(typeof(GameObject))]
  9. /// <summary>
  10. /// Custom preview drawing that will draw the layout properties of a given object.
  11. /// </summary>
  12. class LayoutPropertiesPreview : ObjectPreview
  13. {
  14. private const float kLabelWidth = 110;
  15. private const float kValueWidth = 100;
  16. class Styles
  17. {
  18. public GUIStyle labelStyle = new GUIStyle(EditorStyles.label);
  19. public GUIStyle headerStyle = new GUIStyle(EditorStyles.boldLabel);
  20. public Styles()
  21. {
  22. Color fontColor = new Color(0.7f, 0.7f, 0.7f);
  23. labelStyle.padding.right += 4;
  24. labelStyle.normal.textColor = fontColor;
  25. headerStyle.padding.right += 4;
  26. headerStyle.normal.textColor = fontColor;
  27. }
  28. }
  29. private GUIContent m_Title;
  30. private Styles m_Styles = new Styles();
  31. public override void Initialize(UnityEngine.Object[] targets)
  32. {
  33. base.Initialize(targets);
  34. }
  35. public override GUIContent GetPreviewTitle()
  36. {
  37. if (m_Title == null)
  38. {
  39. m_Title = EditorGUIUtility.TrTextContent("Layout Properties");
  40. }
  41. return m_Title;
  42. }
  43. public override bool HasPreviewGUI()
  44. {
  45. GameObject go = target as GameObject;
  46. if (!go)
  47. return false;
  48. // Prevent allocations in the editor by using TryGetComponent
  49. ILayoutElement layoutElement;
  50. return go.TryGetComponent(out layoutElement);
  51. }
  52. public override void OnPreviewGUI(Rect r, GUIStyle background)
  53. {
  54. if (Event.current.type != EventType.Repaint)
  55. return;
  56. if (m_Styles == null)
  57. m_Styles = new Styles();
  58. GameObject go = target as GameObject;
  59. RectTransform rect = go.transform as RectTransform;
  60. if (rect == null)
  61. return;
  62. // Apply padding
  63. RectOffset previewPadding = new RectOffset(-5, -5, -5, -5);
  64. r = previewPadding.Add(r);
  65. // Prepare rects for columns
  66. r.height = EditorGUIUtility.singleLineHeight;
  67. Rect labelRect = r;
  68. Rect valueRect = r;
  69. Rect sourceRect = r;
  70. labelRect.width = kLabelWidth;
  71. valueRect.xMin += kLabelWidth;
  72. valueRect.width = kValueWidth;
  73. sourceRect.xMin += kLabelWidth + kValueWidth;
  74. // Headers
  75. GUI.Label(labelRect, "Property", m_Styles.headerStyle);
  76. GUI.Label(valueRect, "Value", m_Styles.headerStyle);
  77. GUI.Label(sourceRect, "Source", m_Styles.headerStyle);
  78. labelRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  79. valueRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  80. sourceRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  81. // Prepare reusable variable for out argument
  82. ILayoutElement source = null;
  83. // Show properties
  84. ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Min Width", LayoutUtility.GetLayoutProperty(rect, e => e.minWidth, 0, out source).ToString(CultureInfo.InvariantCulture.NumberFormat), source);
  85. ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Min Height", LayoutUtility.GetLayoutProperty(rect, e => e.minHeight, 0, out source).ToString(CultureInfo.InvariantCulture.NumberFormat), source);
  86. ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Preferred Width", LayoutUtility.GetLayoutProperty(rect, e => e.preferredWidth, 0, out source).ToString(CultureInfo.InvariantCulture.NumberFormat), source);
  87. ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Preferred Height", LayoutUtility.GetLayoutProperty(rect, e => e.preferredHeight, 0, out source).ToString(CultureInfo.InvariantCulture.NumberFormat), source);
  88. float flexible = 0;
  89. flexible = LayoutUtility.GetLayoutProperty(rect, e => e.flexibleWidth, 0, out source);
  90. ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Flexible Width", flexible > 0 ? ("enabled (" + flexible.ToString(CultureInfo.InvariantCulture.NumberFormat) + ")") : "disabled", source);
  91. flexible = LayoutUtility.GetLayoutProperty(rect, e => e.flexibleHeight, 0, out source);
  92. ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Flexible Height", flexible > 0 ? ("enabled (" + flexible.ToString(CultureInfo.InvariantCulture.NumberFormat) + ")") : "disabled", source);
  93. if (!rect.GetComponent<LayoutElement>())
  94. {
  95. Rect noteRect = new Rect(labelRect.x, labelRect.y + 10, r.width, EditorGUIUtility.singleLineHeight);
  96. GUI.Label(noteRect, "Add a LayoutElement to override values.", m_Styles.labelStyle);
  97. }
  98. }
  99. private void ShowProp(ref Rect labelRect, ref Rect valueRect, ref Rect sourceRect, string label, string value, ILayoutElement source)
  100. {
  101. GUI.Label(labelRect, label, m_Styles.labelStyle);
  102. GUI.Label(valueRect, value, m_Styles.labelStyle);
  103. GUI.Label(sourceRect, source == null ? "none" : source.GetType().Name, m_Styles.labelStyle);
  104. labelRect.y += EditorGUIUtility.singleLineHeight;
  105. valueRect.y += EditorGUIUtility.singleLineHeight;
  106. sourceRect.y += EditorGUIUtility.singleLineHeight;
  107. }
  108. }
  109. }