MinMaxDrawer.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace HQFPSWeapons
  4. {
  5. [CustomPropertyDrawer(typeof(MinMaxAttribute))]
  6. public class MinMaxDrawer : PropertyDrawer
  7. {
  8. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  9. {
  10. var minMaxAttribute = (MinMaxAttribute)attribute;
  11. var propertyType = property.propertyType;
  12. label.tooltip = minMaxAttribute.min.ToString("F2") + " to " + minMaxAttribute.max.ToString("F2");
  13. Rect ctrlRect = EditorGUI.PrefixLabel(position, label);
  14. Rect[] splittedRect = SplitRect(ctrlRect, 3);
  15. if (propertyType == SerializedPropertyType.Vector2)
  16. {
  17. EditorGUI.BeginChangeCheck();
  18. Vector2 vector = property.vector2Value;
  19. float minVal = vector.x;
  20. float maxVal = vector.y;
  21. minVal = EditorGUI.FloatField(splittedRect[0], float.Parse(minVal.ToString("F2")));
  22. maxVal = EditorGUI.FloatField(splittedRect[2], float.Parse(maxVal.ToString("F2")));
  23. EditorGUI.MinMaxSlider(splittedRect[1], ref minVal, ref maxVal,
  24. minMaxAttribute.min, minMaxAttribute.max);
  25. if (minVal < minMaxAttribute.min)
  26. {
  27. minVal = minMaxAttribute.min;
  28. }
  29. if (maxVal > minMaxAttribute.max)
  30. {
  31. maxVal = minMaxAttribute.max;
  32. }
  33. vector = new Vector2(minVal > maxVal ? maxVal : minVal, maxVal);
  34. if (EditorGUI.EndChangeCheck())
  35. {
  36. property.vector2Value = vector;
  37. }
  38. }
  39. else if (propertyType == SerializedPropertyType.Vector2Int)
  40. {
  41. EditorGUI.BeginChangeCheck();
  42. Vector2Int vector = property.vector2IntValue;
  43. float minVal = vector.x;
  44. float maxVal = vector.y;
  45. minVal = EditorGUI.FloatField(splittedRect[0], minVal);
  46. maxVal = EditorGUI.FloatField(splittedRect[2], maxVal);
  47. EditorGUI.MinMaxSlider(splittedRect[1], ref minVal, ref maxVal,
  48. minMaxAttribute.min, minMaxAttribute.max);
  49. if (minVal < minMaxAttribute.min)
  50. {
  51. maxVal = minMaxAttribute.min;
  52. }
  53. if (minVal > minMaxAttribute.max)
  54. {
  55. maxVal = minMaxAttribute.max;
  56. }
  57. vector = new Vector2Int(Mathf.FloorToInt(minVal > maxVal ? maxVal : minVal), Mathf.FloorToInt(maxVal));
  58. if (EditorGUI.EndChangeCheck())
  59. {
  60. property.vector2IntValue = vector;
  61. }
  62. }
  63. }
  64. Rect[] SplitRect(Rect rectToSplit, int n)
  65. {
  66. Rect[] rects = new Rect[n];
  67. float indentLevel = EditorGUI.indentLevel;
  68. if (indentLevel > 1)
  69. indentLevel += 15f;
  70. else
  71. indentLevel = 1;
  72. for (int i = 0; i < n; i++)
  73. {
  74. rects[i] = new Rect(rectToSplit.position.x + (i * rectToSplit.width / n) - 40f, rectToSplit.position.y, rectToSplit.width / n + indentLevel, rectToSplit.height);
  75. }
  76. int padding = (int)rects[0].width - 35;
  77. if (indentLevel > 1)
  78. padding -= 20;
  79. rects[0].x += 15f;
  80. rects[0].width -= padding - 10f;
  81. rects[2].width -= padding - 10f;
  82. rects[1].x -= padding - 20f;
  83. rects[1].width += padding * 2f;
  84. rects[2].x += padding + 20f;
  85. return rects;
  86. }
  87. }
  88. }