UIViewUtility.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System.Collections.Generic;
  2. using System.Globalization;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. using UnityEngine.UI;
  7. public static class UIViewUtility
  8. {
  9. static readonly Dictionary<int, Sprite> AvatarSprites = new Dictionary<int, Sprite>();
  10. public static Transform Find(Transform root, string path)
  11. {
  12. if (root == null || string.IsNullOrEmpty(path))
  13. return null;
  14. Transform direct = root.Find(path);
  15. if (direct != null)
  16. return direct;
  17. string[] segments = path.Split('/');
  18. string leaf = segments[segments.Length - 1];
  19. return FindByName(root, leaf);
  20. }
  21. public static Transform FindByName(Transform root, string name)
  22. {
  23. if (root == null)
  24. return null;
  25. if (root.name == name)
  26. return root;
  27. for (int i = 0; i < root.childCount; i++)
  28. {
  29. Transform match = FindByName(root.GetChild(i), name);
  30. if (match != null)
  31. return match;
  32. }
  33. return null;
  34. }
  35. public static T Get<T>(Transform root, string path) where T : Component
  36. {
  37. Transform target = Find(root, path);
  38. return target != null ? target.GetComponent<T>() : null;
  39. }
  40. public static TMP_Text Text(Transform root, string path)
  41. {
  42. return Get<TMP_Text>(root, path);
  43. }
  44. public static Image Image(Transform root, string path)
  45. {
  46. return Get<Image>(root, path);
  47. }
  48. public static Button Button(Transform root, string path, bool ensure = true)
  49. {
  50. Transform target = Find(root, path);
  51. if (target == null)
  52. return null;
  53. return EnsureButton(target);
  54. }
  55. public static Button EnsureButton(Transform target)
  56. {
  57. if (target == null)
  58. return null;
  59. Button button = target.GetComponent<Button>();
  60. if (button == null)
  61. {
  62. button = target.gameObject.AddComponent<Button>();
  63. Graphic graphic = target.GetComponent<Graphic>();
  64. ConfigureButton(button, graphic);
  65. }
  66. return button;
  67. }
  68. public static void ConfigureButton(Button button, Graphic targetGraphic)
  69. {
  70. if (button == null)
  71. return;
  72. if (targetGraphic != null)
  73. button.targetGraphic = targetGraphic;
  74. button.transition = Selectable.Transition.ColorTint;
  75. button.colors = new ColorBlock
  76. {
  77. normalColor = Color.white,
  78. highlightedColor = new Color(0.96f, 0.96f, 0.96f, 1f),
  79. pressedColor = new Color(0.86f, 0.86f, 0.86f, 1f),
  80. selectedColor = Color.white,
  81. disabledColor = new Color(0.72f, 0.72f, 0.72f, 0.72f),
  82. colorMultiplier = 1f,
  83. fadeDuration = 0.08f
  84. };
  85. button.navigation = new Navigation { mode = Navigation.Mode.None };
  86. }
  87. public static void Bind(Button button, UnityAction action)
  88. {
  89. if (button == null || action == null)
  90. return;
  91. button.onClick.RemoveListener(action);
  92. button.onClick.RemoveListener(GameAudio.PlayTap);
  93. button.onClick.AddListener(GameAudio.PlayTap);
  94. button.onClick.AddListener(action);
  95. }
  96. public static void EnsureGraphicRaycast(Transform target)
  97. {
  98. Graphic graphic = target != null ? target.GetComponent<Graphic>() : null;
  99. if (graphic != null)
  100. graphic.raycastTarget = true;
  101. }
  102. public static void SetProgress(Transform fillContainer, float normalized)
  103. {
  104. if (fillContainer == null || fillContainer.childCount == 0)
  105. return;
  106. RectTransform container = fillContainer as RectTransform;
  107. RectTransform fillImage = fillContainer.GetChild(0) as RectTransform;
  108. if (container == null || fillImage == null)
  109. return;
  110. float maxWidth = Mathf.Max(fillImage.rect.width, fillImage.sizeDelta.x);
  111. if (maxWidth <= 0f)
  112. maxWidth = container.rect.width;
  113. if (maxWidth <= 0f)
  114. return;
  115. Vector2 anchor = new Vector2(0f, fillImage.anchorMin.y);
  116. fillImage.anchorMin = anchor;
  117. fillImage.anchorMax = new Vector2(0f, fillImage.anchorMax.y);
  118. fillImage.pivot = new Vector2(0f, fillImage.pivot.y);
  119. fillImage.anchoredPosition = new Vector2(0f, fillImage.anchoredPosition.y);
  120. fillImage.sizeDelta = new Vector2(maxWidth, fillImage.sizeDelta.y);
  121. container.sizeDelta = new Vector2(maxWidth * Mathf.Clamp01(normalized), container.sizeDelta.y);
  122. }
  123. public static void SetGraphicVisible(Transform root, string path, bool visible)
  124. {
  125. Transform target = Find(root, path);
  126. if (target != null)
  127. target.gameObject.SetActive(visible);
  128. }
  129. public static void EnableAutoSize(TMP_Text text, float maxSize, float minSize)
  130. {
  131. if (text == null)
  132. return;
  133. text.enableAutoSizing = true;
  134. text.fontSizeMax = maxSize;
  135. text.fontSizeMin = minSize;
  136. }
  137. public static string FormatNumber(int value)
  138. {
  139. return value.ToString("N0", CultureInfo.InvariantCulture);
  140. }
  141. public static Sprite LoadAvatarSprite(int itemIndex)
  142. {
  143. if (AvatarSprites.TryGetValue(itemIndex, out Sprite cached))
  144. return cached;
  145. string path = "UI/items/spr_item_" + itemIndex;
  146. Sprite sprite = Resources.Load<Sprite>(path);
  147. AvatarSprites[itemIndex] = sprite;
  148. return sprite;
  149. }
  150. public static Sprite LoadSprite(string resourcePath)
  151. {
  152. if (string.IsNullOrEmpty(resourcePath))
  153. return null;
  154. return Resources.Load<Sprite>(resourcePath);
  155. }
  156. }