ToggleDisplay.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #if UNITY_2019_4_OR_NEWER
  2. using System;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityEditor.UIElements;
  6. using UnityEngine.UIElements;
  7. namespace YooAsset.Editor
  8. {
  9. /// <summary>
  10. /// 显示开关(眼睛图标)
  11. /// </summary>
  12. //[UxmlElement]
  13. public partial class ToggleDisplay : Toggle
  14. {
  15. private readonly VisualElement _checkbox;
  16. public ToggleDisplay()
  17. {
  18. _checkbox = this.Q<VisualElement>("unity-checkmark");
  19. RefreshIcon();
  20. }
  21. public override void SetValueWithoutNotify(bool newValue)
  22. {
  23. base.SetValueWithoutNotify(newValue);
  24. RefreshIcon();
  25. }
  26. #if UNITY_2021_3_OR_NEWER
  27. protected override void ToggleValue()
  28. {
  29. base.ToggleValue();
  30. RefreshIcon();
  31. }
  32. #endif
  33. private void RefreshIcon()
  34. {
  35. if (this.value)
  36. {
  37. var icon = EditorGUIUtility.IconContent(UIElementsIcon.VisibilityToggleOff).image as Texture2D;
  38. _checkbox.style.backgroundImage = icon;
  39. }
  40. else
  41. {
  42. var icon = EditorGUIUtility.IconContent(UIElementsIcon.VisibilityToggleOn).image as Texture2D;
  43. _checkbox.style.backgroundImage = icon;
  44. }
  45. }
  46. }
  47. }
  48. #endif