StyledRemapSliderDrawer.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Cristian Pop - https://boxophobic.com/
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System;
  5. public class StyledRemapSliderDrawer : MaterialPropertyDrawer
  6. {
  7. public string nameMin = "";
  8. public string nameMax = "";
  9. public float min = 0;
  10. public float max = 0;
  11. public float top = 0;
  12. public float down = 0;
  13. public StyledRemapSliderDrawer(string nameMin, string nameMax, float min, float max)
  14. {
  15. this.nameMin = nameMin;
  16. this.nameMax = nameMax;
  17. this.min = min;
  18. this.max = max;
  19. this.top = 0;
  20. this.down = 0;
  21. }
  22. public StyledRemapSliderDrawer(string nameMin, string nameMax, float min, float max, float top, float down)
  23. {
  24. this.nameMin = nameMin;
  25. this.nameMax = nameMax;
  26. this.min = min;
  27. this.max = max;
  28. this.top = top;
  29. this.down = down;
  30. }
  31. public override void OnGUI(Rect position, MaterialProperty prop, String label, MaterialEditor editor)
  32. {
  33. var internalPropMin = MaterialEditor.GetMaterialProperty(editor.targets, nameMin);
  34. var internalPropMax = MaterialEditor.GetMaterialProperty(editor.targets, nameMax);
  35. if (internalPropMin.displayName != null && internalPropMax.displayName != null)
  36. {
  37. var internalValueMin = internalPropMin.floatValue;
  38. var internalValueMax = internalPropMax.floatValue;
  39. Vector4 propVector = prop.vectorValue;
  40. var stylePopup = new GUIStyle(EditorStyles.popup)
  41. {
  42. fontSize = 9,
  43. };
  44. EditorGUI.BeginChangeCheck();
  45. if (internalValueMin < internalValueMax)
  46. {
  47. propVector.w = 0;
  48. }
  49. else if(internalValueMin > internalValueMax)
  50. {
  51. propVector.w = 1;
  52. }
  53. if (propVector.w == 0)
  54. {
  55. propVector.x = internalValueMin;
  56. propVector.y = internalValueMax;
  57. }
  58. else
  59. {
  60. propVector.x = internalValueMax;
  61. propVector.y = internalValueMin;
  62. }
  63. GUILayout.Space(top);
  64. EditorGUI.showMixedValue = prop.hasMixedValue;
  65. GUILayout.BeginHorizontal();
  66. GUILayout.Space(-1);
  67. GUILayout.Label(label, GUILayout.Width(EditorGUIUtility.labelWidth - 1));
  68. EditorGUILayout.MinMaxSlider(ref propVector.x, ref propVector.y, min, max);
  69. GUILayout.Space(2);
  70. propVector.w = (float)EditorGUILayout.Popup((int)propVector.w, new string[] { "Remap", "Invert" }, stylePopup, GUILayout.Width(50));
  71. GUILayout.EndHorizontal();
  72. if (propVector.w == 0f)
  73. {
  74. internalValueMin = propVector.x;
  75. internalValueMax = propVector.y;
  76. }
  77. else if (propVector.w == 1f)
  78. {
  79. internalValueMin = propVector.y;
  80. internalValueMax = propVector.x;
  81. }
  82. EditorGUI.showMixedValue = false;
  83. if (EditorGUI.EndChangeCheck())
  84. {
  85. prop.vectorValue = propVector;
  86. internalPropMin.floatValue = internalValueMin;
  87. internalPropMax.floatValue = internalValueMax;
  88. }
  89. GUILayout.Space(down);
  90. }
  91. }
  92. public override float GetPropertyHeight(MaterialProperty prop, string label, MaterialEditor editor)
  93. {
  94. return -2;
  95. }
  96. }