Toggle.cs 960 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using UnityEngine;
  3. namespace MeidoPhotoStudio.Plugin;
  4. public class Toggle : BaseControl
  5. {
  6. private bool value;
  7. public Toggle(string label, bool state = false)
  8. {
  9. Label = label;
  10. value = state;
  11. }
  12. public string Label { get; set; }
  13. public bool Value
  14. {
  15. get => value;
  16. set
  17. {
  18. this.value = value;
  19. OnControlEvent(EventArgs.Empty);
  20. }
  21. }
  22. public override void Draw(params GUILayoutOption[] layoutOptions) =>
  23. Draw(new(GUI.skin.toggle), layoutOptions);
  24. public void Draw(GUIStyle toggleStyle, params GUILayoutOption[] layoutOptions)
  25. {
  26. var value = GUILayout.Toggle(Value, Label, toggleStyle, layoutOptions);
  27. if (value != Value)
  28. Value = value;
  29. }
  30. public void Draw(Rect rect)
  31. {
  32. var value = GUI.Toggle(rect, Value, Label);
  33. if (value != Value)
  34. Value = value;
  35. }
  36. }