ButtonUtil.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 Selected(GameObject btn)
  42. {
  43. Color color = Color.white;
  44. Image img = btn.GetComponent<Image> ();
  45. if(img != null)
  46. {
  47. color = img.color;
  48. color.a = 1f;
  49. img.color = color;
  50. }
  51. Text[] txtArr = btn.GetComponentsInChildren<Text> ();
  52. for(int i=0; i<txtArr.Length; i++)
  53. {
  54. Text txt = txtArr [i];
  55. color = txt.color;
  56. color.a = 1f;
  57. txt.color = color;
  58. }
  59. }
  60. public static void Unselect(GameObject btn)
  61. {
  62. float alpha = 96f / 255f;
  63. Color color = Color.white;
  64. Image img = btn.GetComponent<Image> ();
  65. if(img != null)
  66. {
  67. color = img.color;
  68. color.a = alpha;
  69. img.color = color;
  70. }
  71. Image[] imgArr = btn.GetComponentsInChildren<Image> ();
  72. for(int i=0; i<imgArr.Length; i++)
  73. {
  74. color = img.color;
  75. color.a = alpha;
  76. img.color = color;
  77. }
  78. Text[] txtArr = btn.GetComponentsInChildren<Text> ();
  79. for(int i=0; i<txtArr.Length; i++)
  80. {
  81. Text txt = txtArr [i];
  82. color = txt.color;
  83. color.a = alpha;
  84. txt.color = color;
  85. }
  86. }
  87. }