Guest User

Untitled

a guest
May 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. [CustomPropertyDrawer(typeof(IntRangeAttribute))]
  7. public class IntRangeDrawer : PropertyDrawer
  8. {
  9. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  10. {
  11. return base.GetPropertyHeight(property, label) + 16;
  12. }
  13.  
  14. // Draw the property inside the given rect
  15. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  16. {
  17. // Now draw the property as a Slider or an IntSlider based on whether it’s a float or integer.
  18. if (property.type != typeof(IntRange).ToString())
  19. Debug.LogWarning("Use only with IntRange type");
  20. else
  21. {
  22. IntRangeAttribute range = attribute as IntRangeAttribute;
  23. SerializedProperty minValue = property.FindPropertyRelative("RangeStart");
  24. SerializedProperty maxValue = property.FindPropertyRelative("RangeEnd");
  25. float newMin = minValue.intValue;
  26. float newMax = maxValue.intValue;
  27.  
  28. float xDivision = position.width * 0.4f;
  29. float xLabelDiv = xDivision * 0.125f;
  30.  
  31. float yDivision = position.height * 0.5f;
  32. EditorGUI.LabelField(new Rect(position.x, position.y, xDivision, yDivision)
  33. , label);
  34.  
  35.  
  36. Rect mmRect = new Rect(position.x + xDivision + xLabelDiv, position.y, position.width - (xDivision + xLabelDiv*2), yDivision);
  37.  
  38. EditorGUI.MinMaxSlider(mmRect, ref newMin, ref newMax, range.MinLimit, range.MaxLimit);
  39.  
  40.  
  41. //to deal with rounding on negative values:
  42. int newMinI = (int)(newMin - (float)range.MinLimit) + range.MinLimit;
  43. int newMaxI = (int)(newMax - (float)range.MinLimit) + range.MinLimit;
  44.  
  45. //left label
  46. Rect minRangeRect = new Rect(position.x + xDivision, position.y, xLabelDiv, yDivision);
  47. minRangeRect.x += xLabelDiv * 0.5f - 12;
  48. minRangeRect.width = 24;
  49. EditorGUI.LabelField( minRangeRect, range.MinLimit.ToString());
  50.  
  51. //right label
  52. Rect maxRangeRect = new Rect(minRangeRect);
  53. maxRangeRect.x = mmRect.xMax;
  54. maxRangeRect.x = mmRect.xMax + xLabelDiv * 0.5f - 12;
  55. maxRangeRect.width = 24;
  56. EditorGUI.LabelField(maxRangeRect, range.MaxLimit.ToString());
  57.  
  58. int totalRange = Mathf.Max(range.MaxLimit - range.MinLimit, 1 );
  59. Rect minLabelRect = new Rect(mmRect);
  60. minLabelRect.x = minLabelRect.x + minLabelRect.width * ((newMin - range.MinLimit) / totalRange);
  61. minLabelRect.x -= 12;
  62. minLabelRect.y += yDivision;
  63. minLabelRect.width = 24;
  64. newMinI = Mathf.Clamp(EditorGUI.IntField(minLabelRect, newMinI), range.MinLimit, newMaxI);
  65. //EditorGUI.LabelField(minLabelRect, newMin.ToString());//old style non moving label
  66.  
  67. Rect maxLabelRect = new Rect(mmRect);
  68. maxLabelRect.x = maxLabelRect.x + maxLabelRect.width * ((newMax - range.MinLimit)/ totalRange);
  69. maxLabelRect.x -= 12;
  70. maxLabelRect.x = Mathf.Max(maxLabelRect.x, minLabelRect.xMax);
  71. maxLabelRect.y += yDivision;
  72. maxLabelRect.width = 24;
  73. newMaxI = Mathf.Clamp(EditorGUI.IntField(maxLabelRect, newMaxI), newMinI, range.MaxLimit);
  74. //EditorGUI.LabelField(maxLabelRect, newMax.ToString());//old style non moving label
  75.  
  76.  
  77. minValue.intValue = (int)newMinI;
  78. maxValue.intValue = (int)newMaxI;
  79. }
  80. }
  81. }
Add Comment
Please, Sign In to add comment