UIHelper.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace Fort23.Mono
  6. {
  7. public static class UIHelper
  8. {
  9. public static void IsGray(this Transform transform, bool isGray = true)
  10. {
  11. if (isGray)
  12. {
  13. Gray(transform);
  14. }
  15. else
  16. {
  17. RecoverColor(transform);
  18. }
  19. }
  20. public static void Gray(this Transform transform, bool enableButton = true)
  21. {
  22. if (transform.GetComponent<Button>() != null)
  23. {
  24. transform.GetComponent<Button>().enabled = enableButton;
  25. if (transform.GetComponent<Button>().image?.GetComponent<MyImage>() != null)
  26. transform.GetComponent<Button>().image.GetComponent<MyImage>().IsGray = true;
  27. }
  28. else if (transform.GetComponent<MyImage>() != null)
  29. {
  30. transform.GetComponent<MyImage>().IsGray = true;
  31. }
  32. else if (transform.GetComponent<Text>() != null)
  33. {
  34. transform.GetComponent<Text>().material = UIManager.Instance.uiGray;
  35. }
  36. // else if (transform.GetComponent<MyTextMeshProUGUI>() != null)
  37. // {
  38. // transform.GetComponent<MyTextMeshProUGUI>().Gray();
  39. // }
  40. if (transform.childCount > 0)
  41. {
  42. for (int i = 0; i < transform.childCount; i++)
  43. {
  44. Gray(transform.GetChild(i).transform, enableButton);
  45. }
  46. }
  47. }
  48. public static void RecoverColor(this Transform transform)
  49. {
  50. // transform.Opaque();
  51. if (transform.GetComponent<Button>() != null)
  52. {
  53. transform.GetComponent<Button>().enabled = true;
  54. if (transform.GetComponent<Button>().image?.GetComponent<MyImage>() != null)
  55. transform.GetComponent<Button>().image.GetComponent<MyImage>().IsGray = false;
  56. }
  57. else if (transform.GetComponent<MyImage>() != null)
  58. {
  59. transform.GetComponent<MyImage>().IsGray = false;
  60. }
  61. else if (transform.GetComponent<Text>() != null)
  62. {
  63. transform.GetComponent<Text>().material = null;
  64. }
  65. // else if (transform.GetComponent<MyTextMeshProUGUI>() != null)
  66. // {
  67. // transform.GetComponent<MyTextMeshProUGUI>().Recover();
  68. // }
  69. if (transform.childCount > 0)
  70. {
  71. for (int i = 0; i < transform.childCount; i++)
  72. {
  73. RecoverColor(transform.GetChild(i).transform);
  74. }
  75. }
  76. }
  77. private static Color GrayedColor(Color color)
  78. {
  79. float gray = Vector3.Dot(new Vector3(color.r, color.g, color.b), new Vector3(0.299f, 0.587f, 0.114f));
  80. return new Color(gray, gray, gray, color.a);
  81. }
  82. /// <summary>
  83. /// 半透明
  84. /// </summary>
  85. /// <param name="transform"></param>
  86. public static void Translucency(this Transform transform)
  87. {
  88. RecoverColor(transform);
  89. Color color = new Color(0.34f, 0.34f, 0.34f);
  90. if (transform.GetComponent<Button>() != null)
  91. {
  92. if (transform.GetComponent<Button>().image != null &&
  93. transform.GetComponent<Button>().image.GetComponent<MyImage>().icon_name != string.Empty)
  94. transform.GetComponent<Button>().image.color = color;
  95. }
  96. else if (transform.GetComponent<MyImage>() != null)
  97. {
  98. if (transform.GetComponent<MyImage>().icon_name != String.Empty)
  99. transform.GetComponent<MyImage>().color = color;
  100. }
  101. // else if (transform.GetComponent<Text>() != null)
  102. // {
  103. // // transform.GetComponent<Text>().color = color;
  104. // transform.GetComponent<Text>().Translucency();
  105. // }
  106. if (transform.childCount > 0)
  107. {
  108. for (int i = 0; i < transform.childCount; i++)
  109. {
  110. Translucency(transform.GetChild(i).transform);
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// 从半透明中恢复默认
  116. /// </summary>
  117. /// <param name="transform"></param>
  118. public static void Opaque(this Transform transform)
  119. {
  120. Color color = new Color(1, 1, 1);
  121. if (transform.GetComponent<Button>() != null)
  122. {
  123. if (transform.GetComponent<Button>().image != null)
  124. {
  125. MyImage myImage = transform.GetComponent<Button>().image.GetComponent<MyImage>();
  126. if (myImage != null && myImage.icon_name != string.Empty)
  127. transform.GetComponent<Button>().image.color = color;
  128. }
  129. }
  130. else if (transform.GetComponent<MyImage>() != null)
  131. {
  132. if (transform.GetComponent<MyImage>().icon_name != String.Empty)
  133. {
  134. transform.GetComponent<MyImage>().color = color;
  135. }
  136. }
  137. // else if (transform.GetComponent<Text>() != null)
  138. // {
  139. // // transform.GetComponent<Text>().color = color;
  140. // transform.GetComponent<Text>().Recover();
  141. // }
  142. if (transform.childCount > 0)
  143. {
  144. for (int i = 0; i < transform.childCount; i++)
  145. {
  146. Opaque(transform.GetChild(i).transform);
  147. }
  148. }
  149. }
  150. }
  151. }