ComboBox.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using UnityEngine;
  2. namespace COM3D2.MeidoPhotoStudio.Plugin
  3. {
  4. public class ComboBox : BaseControl
  5. {
  6. private readonly TextField textField = new TextField();
  7. public Dropdown BaseDropDown { get; }
  8. public string Value
  9. {
  10. get => textField.Value;
  11. set => textField.Value = value;
  12. }
  13. public ComboBox(string[] itemList)
  14. {
  15. BaseDropDown = new Dropdown("▾", itemList);
  16. BaseDropDown.SelectionChange += (s, a) => textField.Value = BaseDropDown.SelectedItem;
  17. Value = itemList[0];
  18. }
  19. public void SetDropdownItems(string[] itemList)
  20. {
  21. string oldValue = Value;
  22. BaseDropDown.SetDropdownItems(itemList);
  23. Value = oldValue;
  24. }
  25. public void SetDropdownItem(int index, string newItem) => BaseDropDown.SetDropdownItem(index, newItem);
  26. public void SetDropdownItem(string newItem) => BaseDropDown.SetDropdownItem(newItem);
  27. public override void Draw(params GUILayoutOption[] layoutOptions)
  28. {
  29. GUIStyle buttonStyle = new GUIStyle(GUI.skin.button) { alignment = TextAnchor.MiddleCenter };
  30. Draw(buttonStyle, layoutOptions);
  31. }
  32. public void Draw(GUIStyle style, params GUILayoutOption[] layoutOptions)
  33. {
  34. GUILayout.BeginHorizontal();
  35. textField.Draw(new GUIStyle(GUI.skin.textField), layoutOptions);
  36. BaseDropDown.Draw(style, GUILayout.ExpandWidth(false));
  37. GUILayout.EndHorizontal();
  38. }
  39. }
  40. }