ComboBox2.cs 3.4 KB

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