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

LayoutPropertiesPreview.cs 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. labelStyle.padding.right += 4;
  23. headerStyle.padding.right += 4;
  24. }
  25. }
  26. private GUIContent m_Title;
  27. private Styles m_Styles;
  28. public override void Initialize(UnityEngine.Object[] targets)
  29. {
  30. base.Initialize(targets);
  31. }
  32. public override GUIContent GetPreviewTitle()
  33. {
  34. if (m_Title == null)
  35. {
  36. m_Title = EditorGUIUtility.TrTextContent("Layout Properties");
  37. }
  38. return m_Title;
  39. }
  40. public override bool HasPreviewGUI()
  41. {
  42. GameObject go = target as GameObject;
  43. if (!go)
  44. return false;
  45. // Prevent allocations in the editor by using TryGetComponent
  46. ILayoutElement layoutElement;
  47. return go.TryGetComponent(out layoutElement);
  48. }
  49. public override void OnPreviewGUI(Rect r, GUIStyle background)
  50. {
  51. if (Event.current.type != EventType.Repaint)
  52. return;
  53. if (m_Styles == null)
  54. m_Styles = new Styles();
  55. GameObject go = target as GameObject;
  56. RectTransform rect = go.transform as RectTransform;
  57. if (rect == null)
  58. return;
  59. // Apply padding
  60. RectOffset previewPadding = new RectOffset(-5, -5, -5, -5);
  61. r = previewPadding.Add(r);
  62. // Prepare rects for columns
  63. r.height = EditorGUIUtility.singleLineHeight;
  64. Rect labelRect = r;
  65. Rect valueRect = r;
  66. Rect sourceRect = r;
  67. labelRect.width = kLabelWidth;
  68. valueRect.xMin += kLabelWidth;
  69. valueRect.width = kValueWidth;
  70. sourceRect.xMin += kLabelWidth + kValueWidth;
  71. // Headers
  72. GUI.Label(labelRect, "Property", m_Styles.headerStyle);
  73. GUI.Label(valueRect, "Value", m_Styles.headerStyle);
  74. GUI.Label(sourceRect, "Source", m_Styles.headerStyle);
  75. labelRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  76. valueRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  77. sourceRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  78. // Prepare reusable variable for out argument
  79. ILayoutElement source = null;
  80. // Show properties
  81. ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Min Width", LayoutUtility.GetLayoutProperty(rect, e => e.minWidth, 0, out source).ToString(CultureInfo.InvariantCulture.NumberFormat), source);
  82. ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Min Height", LayoutUtility.GetLayoutProperty(rect, e => e.minHeight, 0, out source).ToString(CultureInfo.InvariantCulture.NumberFormat), source);
  83. ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Preferred Width", LayoutUtility.GetLayoutProperty(rect, e => e.preferredWidth, 0, out source).ToString(CultureInfo.InvariantCulture.NumberFormat), source);
  84. ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Preferred Height", LayoutUtility.GetLayoutProperty(rect, e => e.preferredHeight, 0, out source).ToString(CultureInfo.InvariantCulture.NumberFormat), source);
  85. float flexible = 0;
  86. flexible = LayoutUtility.GetLayoutProperty(rect, e => e.flexibleWidth, 0, out source);
  87. ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Flexible Width", flexible > 0 ? ("enabled (" + flexible.ToString(CultureInfo.InvariantCulture.NumberFormat) + ")") : "disabled", source);
  88. flexible = LayoutUtility.GetLayoutProperty(rect, e => e.flexibleHeight, 0, out source);
  89. ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Flexible Height", flexible > 0 ? ("enabled (" + flexible.ToString(CultureInfo.InvariantCulture.NumberFormat) + ")") : "disabled", source);
  90. if (!rect.GetComponent<LayoutElement>())
  91. {
  92. Rect noteRect = new Rect(labelRect.x, labelRect.y + 10, r.width, EditorGUIUtility.singleLineHeight);
  93. GUI.Label(noteRect, "Add a LayoutElement to override values.", m_Styles.labelStyle);
  94. }
  95. }
  96. private void ShowProp(ref Rect labelRect, ref Rect valueRect, ref Rect sourceRect, string label, string value, ILayoutElement source)
  97. {
  98. GUI.Label(labelRect, label, m_Styles.labelStyle);
  99. GUI.Label(valueRect, value, m_Styles.labelStyle);
  100. GUI.Label(sourceRect, source == null ? "none" : source.GetType().Name, m_Styles.labelStyle);
  101. labelRect.y += EditorGUIUtility.singleLineHeight;
  102. valueRect.y += EditorGUIUtility.singleLineHeight;
  103. sourceRect.y += EditorGUIUtility.singleLineHeight;
  104. }
  105. }
  106. }