ContentSizeLimitEditor.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * -------------------------------------------------------------------------------
  3. * FileName : ContentSizeLimitEditor.cs
  4. * Created : 2019-02-23 16:19
  5. * Author : wboy
  6. * Mail : mrtop@126.com
  7. * Compiler : Visual Studio Code And Unity3d
  8. * Description :
  9. *
  10. * -------------------------------------------------------------------------------
  11. */
  12. using System;
  13. using System.Collections;
  14. using System.Collections.Generic;
  15. using UnityEditor;
  16. using UnityEngine;
  17. using UnityEngine.UI;
  18. [CustomEditor(typeof(ContentSizeLimit), true)]
  19. [CanEditMultipleObjects]
  20. public class ContentSizeLimitEditor : Editor {
  21. private SerializedProperty m_MaxWidth;
  22. private SerializedProperty m_MaxHeight;
  23. protected virtual void OnEnable()
  24. {
  25. m_MaxWidth = base.serializedObject.FindProperty("m_MaxWidth");
  26. m_MaxHeight = base.serializedObject.FindProperty("m_MaxHeight");
  27. }
  28. /// <summary>
  29. /// <para>See: Editor.OnInspectorGUI.</para>
  30. /// </summary>
  31. public override void OnInspectorGUI()
  32. {
  33. base.serializedObject.Update();
  34. LayoutElementField(m_MaxWidth, delegate(RectTransform t)
  35. {
  36. Rect rect2 = t.rect;
  37. return rect2.width;
  38. });
  39. LayoutElementField(m_MaxHeight, delegate(RectTransform t)
  40. {
  41. Rect rect2 = t.rect;
  42. return rect2.height;
  43. });
  44. base.serializedObject.ApplyModifiedProperties();
  45. }
  46. private void LayoutElementField(SerializedProperty property, float defaultValue)
  47. {
  48. LayoutElementField(property, (RectTransform _) => defaultValue);
  49. }
  50. private void LayoutElementField(SerializedProperty property, Func<RectTransform, float> defaultValue)
  51. {
  52. Rect controlRect = EditorGUILayout.GetControlRect();
  53. GUIContent label = EditorGUI.BeginProperty(controlRect, null, property);
  54. Rect val = EditorGUI.PrefixLabel(controlRect, label);
  55. Rect position = val;
  56. position.width = 16f;
  57. Rect position2 = val;
  58. position2.xMin = (position2.xMin + 16f);
  59. EditorGUI.BeginChangeCheck();
  60. bool flag = EditorGUI.ToggleLeft(position, GUIContent.none, property.floatValue >= 0f);
  61. if (EditorGUI.EndChangeCheck())
  62. {
  63. property.floatValue = ((!flag) ? -1f : defaultValue((base.target as ContentSizeLimit).transform as RectTransform));
  64. }
  65. if (!property.hasMultipleDifferentValues && property.floatValue >= 0f)
  66. {
  67. EditorGUIUtility.labelWidth = 4f;
  68. EditorGUI.BeginChangeCheck();
  69. float num = EditorGUI.FloatField(position2, new GUIContent(" "), property.floatValue);
  70. if (EditorGUI.EndChangeCheck())
  71. {
  72. property.floatValue = Mathf.Max(0f, num);
  73. }
  74. EditorGUIUtility.labelWidth = 0f;
  75. }
  76. EditorGUI.EndProperty();
  77. }
  78. }