ComboBox.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using UnityEngine;
  3. public class ComboBox
  4. {
  5. public ComboBox(Rect rect, GUIContent buttonContent, GUIContent[] listContent, GUIStyle listStyle)
  6. {
  7. this.rect = rect;
  8. this.buttonContent = buttonContent;
  9. this.listContent = listContent;
  10. this.buttonStyle = "button";
  11. this.boxStyle = "box";
  12. this.listStyle = listStyle;
  13. }
  14. public ComboBox(Rect rect, GUIContent buttonContent, GUIContent[] listContent, string buttonStyle, string boxStyle, GUIStyle listStyle)
  15. {
  16. this.rect = rect;
  17. this.buttonContent = buttonContent;
  18. this.listContent = listContent;
  19. this.buttonStyle = buttonStyle;
  20. this.boxStyle = boxStyle;
  21. this.listStyle = listStyle;
  22. }
  23. public int Show()
  24. {
  25. if (ComboBox.forceToUnShow)
  26. {
  27. ComboBox.forceToUnShow = false;
  28. this.isClickedComboButton = false;
  29. }
  30. bool flag = false;
  31. int controlID = GUIUtility.GetControlID(FocusType.Passive);
  32. EventType typeForControl = Event.current.GetTypeForControl(controlID);
  33. if (typeForControl == EventType.MouseUp)
  34. {
  35. if (this.isClickedComboButton)
  36. {
  37. flag = true;
  38. }
  39. }
  40. if (GUI.Button(this.rect, this.buttonContent, this.buttonStyle))
  41. {
  42. if (ComboBox.useControlID == -1)
  43. {
  44. ComboBox.useControlID = controlID;
  45. this.isClickedComboButton = false;
  46. }
  47. if (ComboBox.useControlID != controlID)
  48. {
  49. ComboBox.forceToUnShow = true;
  50. ComboBox.useControlID = controlID;
  51. }
  52. this.isClickedComboButton = true;
  53. }
  54. if (this.isClickedComboButton)
  55. {
  56. Rect position = new Rect(this.rect.x, this.rect.y + this.listStyle.CalcHeight(this.listContent[0], 1f), this.rect.width, this.listStyle.CalcHeight(this.listContent[0], 1f) * (float)this.listContent.Length);
  57. GUI.Box(position, string.Empty, this.boxStyle);
  58. int num = GUI.SelectionGrid(position, this.selectedItemIndex, this.listContent, 1, this.listStyle);
  59. if (num != this.selectedItemIndex)
  60. {
  61. this.selectedItemIndex = num;
  62. }
  63. }
  64. if (flag)
  65. {
  66. this.isClickedComboButton = false;
  67. }
  68. return this.selectedItemIndex;
  69. }
  70. public int SelectedItemIndex
  71. {
  72. get
  73. {
  74. return this.selectedItemIndex;
  75. }
  76. set
  77. {
  78. this.selectedItemIndex = value;
  79. }
  80. }
  81. private static bool forceToUnShow;
  82. private static int useControlID = -1;
  83. private bool isClickedComboButton;
  84. private int selectedItemIndex;
  85. private Rect rect;
  86. private GUIContent buttonContent;
  87. private GUIContent[] listContent;
  88. private string buttonStyle;
  89. private string boxStyle;
  90. private GUIStyle listStyle;
  91. }