StyledToggleDrawer.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Cristian Pop - https://boxophobic.com/
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System;
  5. namespace Boxophobic.StyledGUI
  6. {
  7. public class StyledToggleDrawer : MaterialPropertyDrawer
  8. {
  9. public float width = 0;
  10. public StyledToggleDrawer()
  11. {
  12. }
  13. public StyledToggleDrawer(float width)
  14. {
  15. this.width = width;
  16. }
  17. public override void OnGUI(Rect position, MaterialProperty prop, String label, MaterialEditor materialEditor)
  18. {
  19. //Material material = materialEditor.target as Material;
  20. EditorGUI.BeginChangeCheck();
  21. EditorGUI.showMixedValue = prop.hasMixedValue;
  22. if (width == 0)
  23. {
  24. bool toggle = false;
  25. if (prop.floatValue > 0.5f)
  26. {
  27. toggle = true;
  28. }
  29. toggle = EditorGUILayout.Toggle(label, toggle);
  30. EditorGUI.showMixedValue = false;
  31. if (EditorGUI.EndChangeCheck())
  32. {
  33. if (toggle)
  34. {
  35. prop.floatValue = 1;
  36. }
  37. else
  38. {
  39. prop.floatValue = 0;
  40. }
  41. }
  42. }
  43. else
  44. {
  45. GUILayout.BeginHorizontal();
  46. GUILayout.Label(label);
  47. bool toggle = false;
  48. if (prop.floatValue > 0.5f)
  49. {
  50. toggle = true;
  51. }
  52. toggle = GUILayout.Toggle(toggle, "", GUILayout.Width(width));
  53. EditorGUI.showMixedValue = false;
  54. if (EditorGUI.EndChangeCheck())
  55. {
  56. if (toggle)
  57. {
  58. prop.floatValue = 1;
  59. }
  60. else
  61. {
  62. prop.floatValue = 0;
  63. }
  64. }
  65. GUILayout.EndHorizontal();
  66. }
  67. }
  68. public override float GetPropertyHeight(MaterialProperty prop, string label, MaterialEditor editor)
  69. {
  70. return -2;
  71. }
  72. }
  73. }