1
0

ComboBox2.cs 3.5 KB

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