StyledOptionsSliderDrawer.cs 4.7 KB

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