ComboBox2.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using UnityEngine;
  2. public class ComboBox2
  3. {
  4. private static bool forceToUnShow;
  5. private static int useControlID = -1;
  6. public float height;
  7. public bool isClickedComboButton;
  8. public Vector2 scrollPos = new Vector2(0.0f, 0.0f);
  9. public int selectedItemIndex;
  10. private Vector2 scrollPosOld = new Vector2(0.0f, 0.0f);
  11. public int List(Rect rect, string buttonText, GUIContent[] listContent, GUIStyle listStyle)
  12. {
  13. return List(rect, new GUIContent(buttonText), listContent, "button", "box", listStyle);
  14. }
  15. public int List(Rect rect, GUIContent buttonContent, GUIContent[] listContent, GUIStyle listStyle)
  16. {
  17. return List(rect, buttonContent, listContent, "button", "box", listStyle);
  18. }
  19. public int List(Rect rect, string buttonText, GUIContent[] listContent, GUIStyle buttonStyle, GUIStyle boxStyle, GUIStyle listStyle)
  20. {
  21. return List(rect, new GUIContent(buttonText), listContent, buttonStyle, boxStyle, listStyle);
  22. }
  23. public int List(Rect rect,
  24. GUIContent buttonContent,
  25. GUIContent[] listContent,
  26. GUIStyle buttonStyle,
  27. GUIStyle boxStyle,
  28. GUIStyle listStyle)
  29. {
  30. listStyle.onNormal.textColor = listStyle.onHover.textColor;
  31. listStyle.onNormal.background = listStyle.onHover.background;
  32. if (forceToUnShow)
  33. {
  34. forceToUnShow = false;
  35. isClickedComboButton = false;
  36. }
  37. bool flag = false;
  38. int controlId = GUIUtility.GetControlID(FocusType.Passive);
  39. if (Event.current.GetTypeForControl(controlId) == EventType.MouseUp && isClickedComboButton && scrollPosOld == scrollPos)
  40. {
  41. flag = true;
  42. }
  43. if (GUI.Button(rect, buttonContent, buttonStyle))
  44. {
  45. if (useControlID == -1)
  46. {
  47. useControlID = controlId;
  48. isClickedComboButton = false;
  49. }
  50. if (useControlID != controlId)
  51. {
  52. forceToUnShow = true;
  53. useControlID = controlId;
  54. }
  55. isClickedComboButton = true;
  56. }
  57. if (isClickedComboButton)
  58. {
  59. Rect position = new Rect(rect.x, rect.y + GetPix(23), rect.width, listStyle.CalcHeight(listContent[0], 1f) * listContent.Length);
  60. if (position.y + (double)position.height > height)
  61. {
  62. position.height = (float)(height - (double)position.y - 2.0);
  63. position.width += 16f;
  64. }
  65. GUI.Box(position, "", boxStyle);
  66. if (Input.GetMouseButtonDown(0))
  67. {
  68. scrollPosOld = scrollPos;
  69. }
  70. Rect rect1 = new Rect(rect.x,
  71. rect.y + listStyle.CalcHeight(listContent[0], 1f),
  72. rect.width,
  73. listStyle.CalcHeight(listContent[0], 1f) * listContent.Length);
  74. scrollPos = GUI.BeginScrollView(position, scrollPos, rect1);
  75. int num = GUI.SelectionGrid(rect1, selectedItemIndex, listContent, 1, listStyle);
  76. if (num != selectedItemIndex)
  77. {
  78. selectedItemIndex = num;
  79. }
  80. GUI.EndScrollView();
  81. }
  82. if (flag)
  83. {
  84. isClickedComboButton = false;
  85. }
  86. return GetSelectedItemIndex();
  87. }
  88. public int GetSelectedItemIndex()
  89. {
  90. return selectedItemIndex;
  91. }
  92. private int GetPix(int i)
  93. {
  94. return (int)((1.0 + (Screen.width / 1280.0 - 1.0) * 0.600000023841858) * i);
  95. }
  96. }