StyledRangeOptionsDrawer.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Cristian Pop - https://boxophobic.com/
  2. using UnityEngine;
  3. using UnityEditor;
  4. namespace Boxophobic.StyledGUI
  5. {
  6. [CustomPropertyDrawer(typeof(StyledRangeOptions))]
  7. public class StyledRangeOptionsAttributeDrawer : PropertyDrawer
  8. {
  9. StyledRangeOptions a;
  10. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  11. {
  12. a = (StyledRangeOptions)attribute;
  13. GUIStyle styleMid = new GUIStyle();
  14. styleMid.alignment = TextAnchor.MiddleCenter;
  15. styleMid.normal.textColor = Color.gray;
  16. styleMid.fontSize = 7;
  17. if (a.displayLabel.Length > 0)
  18. {
  19. EditorGUI.PropertyField(position, property, label, true);
  20. GUILayout.Space(5);
  21. }
  22. GUILayout.BeginHorizontal();
  23. GUILayout.Space(8);
  24. property.floatValue = GUILayout.HorizontalSlider(property.floatValue, a.min, a.max);
  25. property.floatValue = Mathf.Clamp(property.floatValue, a.min, a.max);
  26. property.floatValue = Mathf.Round(property.floatValue * 1000f) / 1000f;
  27. GUILayout.Space(8);
  28. GUILayout.EndHorizontal();
  29. #if UNITY_2019_3_OR_NEWER
  30. GUILayout.Space(15);
  31. #endif
  32. GUILayout.BeginHorizontal();
  33. int maxWidth = 20;
  34. #if UNITY_2019_3_OR_NEWER
  35. maxWidth = 28;
  36. #endif
  37. for (int i = 0; i < a.options.Length - 1; i++)
  38. {
  39. GUILayout.Label(a.options[i], styleMid, GUILayout.Width(maxWidth));
  40. GUILayout.Label("", styleMid);
  41. }
  42. GUILayout.Label(a.options[a.options.Length - 1], styleMid, GUILayout.Width(maxWidth));
  43. GUILayout.EndHorizontal();
  44. }
  45. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  46. {
  47. a = (StyledRangeOptions)attribute;
  48. if (a.displayLabel.Length > 0)
  49. {
  50. return 18;
  51. }
  52. else
  53. {
  54. return -2;
  55. }
  56. }
  57. }
  58. }