ComboBox2.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 = listStyle.onHover;
  31. if (forceToUnShow)
  32. {
  33. forceToUnShow = false;
  34. isClickedComboButton = false;
  35. }
  36. bool flag = false;
  37. int controlId = GUIUtility.GetControlID(FocusType.Passive);
  38. if (Event.current.GetTypeForControl(controlId) == EventType.MouseUp && isClickedComboButton && scrollPosOld == scrollPos)
  39. {
  40. flag = true;
  41. }
  42. if (GUI.Button(rect, buttonContent, buttonStyle))
  43. {
  44. if (useControlID == -1)
  45. {
  46. useControlID = controlId;
  47. isClickedComboButton = false;
  48. }
  49. if (useControlID != controlId)
  50. {
  51. forceToUnShow = true;
  52. useControlID = controlId;
  53. }
  54. isClickedComboButton = true;
  55. }
  56. if (isClickedComboButton)
  57. {
  58. Rect position = new Rect(rect.x, rect.y + GetPix(23), rect.width, listStyle.CalcHeight(listContent[0], 1f) * listContent.Length);
  59. if (position.y + (double)position.height > height)
  60. {
  61. position.height = (float)(height - (double)position.y - 2.0);
  62. position.width += 16f;
  63. }
  64. GUI.Box(position, "", boxStyle);
  65. if (Input.GetMouseButtonDown(0))
  66. {
  67. scrollPosOld = scrollPos;
  68. }
  69. Rect rect1 = new Rect(rect.x,
  70. rect.y + listStyle.CalcHeight(listContent[0], 1f),
  71. rect.width,
  72. listStyle.CalcHeight(listContent[0], 1f) * listContent.Length);
  73. scrollPos = GUI.BeginScrollView(position, scrollPos, rect1);
  74. int num = GUI.SelectionGrid(rect1, selectedItemIndex, listContent, 1, listStyle);
  75. if (num != selectedItemIndex)
  76. {
  77. selectedItemIndex = num;
  78. }
  79. GUI.EndScrollView();
  80. }
  81. if (flag)
  82. {
  83. isClickedComboButton = false;
  84. }
  85. return GetSelectedItemIndex();
  86. }
  87. public int GetSelectedItemIndex()
  88. {
  89. return selectedItemIndex;
  90. }
  91. private int GetPix(int i)
  92. {
  93. return (int)((1.0 + (Screen.width / 1280.0 - 1.0) * 0.600000023841858) * i);
  94. }
  95. }