ButtonUtil.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. public class ButtonUtil
  5. {
  6. public static void TabSelected(GameObject btn, Transform container)
  7. {
  8. int count = container.childCount;
  9. for(int i=0; i<count; i++)
  10. {
  11. GameObject child = container.GetChild (i).gameObject;
  12. if (child == btn) {
  13. Selected (child);
  14. } else {
  15. Unselect (child);
  16. }
  17. }
  18. }
  19. public static void TabSelected(SelectItemRenderer item, Transform container)
  20. {
  21. int count = container.childCount;
  22. for(int i=0; i<count; i++)
  23. {
  24. SelectItemRenderer renderer = container.GetChild (i).GetComponent<SelectItemRenderer>();
  25. renderer.selected = (renderer == item);
  26. }
  27. }
  28. public static void TabSelected(int index, Transform container)
  29. {
  30. int count = container.childCount;
  31. for(int i=0; i<count; i++)
  32. {
  33. GameObject child = container.GetChild (i).gameObject;
  34. if (i == index) {
  35. Selected (child);
  36. } else {
  37. Unselect (child);
  38. }
  39. }
  40. }
  41. public static void TabUnselect(Transform container)
  42. {
  43. int count = container.childCount;
  44. for(int i=0; i<count; i++)
  45. {
  46. Transform child = container.GetChild (i);
  47. SelectItemRenderer renderer = child.GetComponent<SelectItemRenderer>();
  48. if(renderer != null)
  49. {
  50. renderer.selected = false;
  51. }
  52. else
  53. {
  54. Unselect (child.gameObject);
  55. }
  56. }
  57. }
  58. public static void Selected(GameObject btn)
  59. {
  60. Color color = Color.white;
  61. Image img = btn.GetComponent<Image> ();
  62. if(img != null)
  63. {
  64. color = img.color;
  65. color.a = 1f;
  66. img.color = color;
  67. }
  68. Text[] txtArr = btn.GetComponentsInChildren<Text> ();
  69. for(int i=0; i<txtArr.Length; i++)
  70. {
  71. Text txt = txtArr [i];
  72. color = txt.color;
  73. color.a = 1f;
  74. txt.color = color;
  75. }
  76. }
  77. public static void Unselect(GameObject btn)
  78. {
  79. float alpha = 96f / 255f;
  80. Color color = Color.white;
  81. Image img = btn.GetComponent<Image> ();
  82. if(img != null)
  83. {
  84. color = img.color;
  85. color.a = alpha;
  86. img.color = color;
  87. }
  88. Image[] imgArr = btn.GetComponentsInChildren<Image> ();
  89. for(int i=0; i<imgArr.Length; i++)
  90. {
  91. color = img.color;
  92. color.a = alpha;
  93. img.color = color;
  94. }
  95. Text[] txtArr = btn.GetComponentsInChildren<Text> ();
  96. for(int i=0; i<txtArr.Length; i++)
  97. {
  98. Text txt = txtArr [i];
  99. color = txt.color;
  100. color.a = alpha;
  101. txt.color = color;
  102. }
  103. }
  104. }