TabButtonGroup.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class TabButtonGroup
  5. {
  6. public delegate void TabSelectDelegate(int index);
  7. public TabButtonGroup.TabSelectDelegate OnSelect;
  8. private List<TabButton> m_TabList;
  9. public List<TabButton> TabList
  10. {
  11. get
  12. {
  13. return this.m_TabList;
  14. }
  15. set
  16. {
  17. this.m_TabList = value;
  18. }
  19. }
  20. public bool Enable
  21. {
  22. get;
  23. set;
  24. }
  25. public TabButtonGroup()
  26. {
  27. this.m_TabList = new List<TabButton>();
  28. this.Enable = true;
  29. }
  30. public void Add(TabButton tab)
  31. {
  32. this.m_TabList.Add(tab);
  33. }
  34. public void Remove(TabButton tab)
  35. {
  36. this.m_TabList.Remove(tab);
  37. }
  38. public void Select(int index)
  39. {
  40. if (!this.Enable)
  41. {
  42. return;
  43. }
  44. for (int i = 0; i < this.m_TabList.Count; i++)
  45. {
  46. TabButton tabButton = this.m_TabList[i];
  47. if (i == index)
  48. {
  49. tabButton.Selected = true;
  50. if (OnSelect != null)
  51. {
  52. OnSelect(i);
  53. }
  54. }
  55. else
  56. {
  57. tabButton.Selected = false;
  58. }
  59. }
  60. }
  61. public void Select(TabButton tab)
  62. {
  63. if (!this.Enable)
  64. {
  65. return;
  66. }
  67. for (int i = 0; i < this.m_TabList.Count; i++)
  68. {
  69. TabButton tabButton = this.m_TabList[i];
  70. if (tabButton == tab)
  71. {
  72. tabButton.Selected = true;
  73. if (this.OnSelect != null)
  74. {
  75. this.OnSelect(i);
  76. }
  77. }
  78. else
  79. {
  80. tabButton.Selected = false;
  81. }
  82. }
  83. }
  84. public void OnDestroy()
  85. {
  86. this.m_TabList.Clear();
  87. this.OnSelect = null;
  88. }
  89. public int GetTabCount()
  90. {
  91. return this.m_TabList.Count;
  92. }
  93. public TabButton GetTab(int index)
  94. {
  95. index = Mathf.Clamp(index, 0, this.m_TabList.Count - 1);
  96. return this.m_TabList[index];
  97. }
  98. }