using System; using UnityEngine; public class ComboBox { public ComboBox(Rect rect, GUIContent buttonContent, GUIContent[] listContent, GUIStyle listStyle) { this.rect = rect; this.buttonContent = buttonContent; this.listContent = listContent; this.buttonStyle = "button"; this.boxStyle = "box"; this.listStyle = listStyle; } public ComboBox(Rect rect, GUIContent buttonContent, GUIContent[] listContent, string buttonStyle, string boxStyle, GUIStyle listStyle) { this.rect = rect; this.buttonContent = buttonContent; this.listContent = listContent; this.buttonStyle = buttonStyle; this.boxStyle = boxStyle; this.listStyle = listStyle; } public int Show() { if (ComboBox.forceToUnShow) { ComboBox.forceToUnShow = false; this.isClickedComboButton = false; } bool flag = false; int controlID = GUIUtility.GetControlID(FocusType.Passive); EventType typeForControl = Event.current.GetTypeForControl(controlID); if (typeForControl == EventType.MouseUp) { if (this.isClickedComboButton) { flag = true; } } if (GUI.Button(this.rect, this.buttonContent, this.buttonStyle)) { if (ComboBox.useControlID == -1) { ComboBox.useControlID = controlID; this.isClickedComboButton = false; } if (ComboBox.useControlID != controlID) { ComboBox.forceToUnShow = true; ComboBox.useControlID = controlID; } this.isClickedComboButton = true; } if (this.isClickedComboButton) { 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); GUI.Box(position, string.Empty, this.boxStyle); int num = GUI.SelectionGrid(position, this.selectedItemIndex, this.listContent, 1, this.listStyle); if (num != this.selectedItemIndex) { this.selectedItemIndex = num; } } if (flag) { this.isClickedComboButton = false; } return this.selectedItemIndex; } public int SelectedItemIndex { get { return this.selectedItemIndex; } set { this.selectedItemIndex = value; } } private static bool forceToUnShow; private static int useControlID = -1; private bool isClickedComboButton; private int selectedItemIndex; private Rect rect; private GUIContent buttonContent; private GUIContent[] listContent; private string buttonStyle; private string boxStyle; private GUIStyle listStyle; }