UIHelper.cs 5.4 KB

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