123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- using UnityEngine;
- public class ComboBox2
- {
- private static bool forceToUnShow;
- private static int useControlID = -1;
- public float height;
- public bool isClickedComboButton;
- public Vector2 scrollPos = new Vector2(0.0f, 0.0f);
- public int selectedItemIndex;
- private Vector2 scrollPosOld = new Vector2(0.0f, 0.0f);
- public int List(Rect rect, string buttonText, GUIContent[] listContent, GUIStyle listStyle)
- {
- return List(rect, new GUIContent(buttonText), listContent, "button", "box", listStyle);
- }
- public int List(Rect rect, GUIContent buttonContent, GUIContent[] listContent, GUIStyle listStyle)
- {
- return List(rect, buttonContent, listContent, "button", "box", listStyle);
- }
- public int List(Rect rect, string buttonText, GUIContent[] listContent, GUIStyle buttonStyle, GUIStyle boxStyle, GUIStyle listStyle)
- {
- return List(rect, new GUIContent(buttonText), listContent, buttonStyle, boxStyle, listStyle);
- }
- public int List(Rect rect,
- GUIContent buttonContent,
- GUIContent[] listContent,
- GUIStyle buttonStyle,
- GUIStyle boxStyle,
- GUIStyle listStyle)
- {
- listStyle.onNormal.textColor = listStyle.onHover.textColor;
- listStyle.onNormal.background = listStyle.onHover.background;
- if (forceToUnShow)
- {
- forceToUnShow = false;
- isClickedComboButton = false;
- }
- bool flag = false;
- int controlId = GUIUtility.GetControlID(FocusType.Passive);
- if (Event.current.GetTypeForControl(controlId) == EventType.MouseUp && isClickedComboButton && scrollPosOld == scrollPos)
- {
- flag = true;
- }
- if (GUI.Button(rect, buttonContent, buttonStyle))
- {
- if (useControlID == -1)
- {
- useControlID = controlId;
- isClickedComboButton = false;
- }
- if (useControlID != controlId)
- {
- forceToUnShow = true;
- useControlID = controlId;
- }
- isClickedComboButton = true;
- }
- if (isClickedComboButton)
- {
- Rect position = new Rect(rect.x, rect.y + GetPix(23), rect.width, listStyle.CalcHeight(listContent[0], 1f) * listContent.Length);
- if (position.y + (double)position.height > height)
- {
- position.height = (float)(height - (double)position.y - 2.0);
- position.width += 16f;
- }
- GUI.Box(position, "", boxStyle);
- if (Input.GetMouseButtonDown(0))
- {
- scrollPosOld = scrollPos;
- }
- Rect rect1 = new Rect(rect.x,
- rect.y + listStyle.CalcHeight(listContent[0], 1f),
- rect.width,
- listStyle.CalcHeight(listContent[0], 1f) * listContent.Length);
- scrollPos = GUI.BeginScrollView(position, scrollPos, rect1);
- int num = GUI.SelectionGrid(rect1, selectedItemIndex, listContent, 1, listStyle);
- if (num != selectedItemIndex)
- {
- selectedItemIndex = num;
- }
- GUI.EndScrollView();
- }
- if (flag)
- {
- isClickedComboButton = false;
- }
- return GetSelectedItemIndex();
- }
- public int GetSelectedItemIndex()
- {
- return selectedItemIndex;
- }
- private int GetPix(int i)
- {
- return (int)((1.0 + (Screen.width / 1280.0 - 1.0) * 0.600000023841858) * i);
- }
- }
|