| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- using System.Collections.Generic;
- using System.Globalization;
- using TMPro;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.UI;
- public static class UIViewUtility
- {
- static readonly Dictionary<int, Sprite> AvatarSprites = new Dictionary<int, Sprite>();
- public static Transform Find(Transform root, string path)
- {
- if (root == null || string.IsNullOrEmpty(path))
- return null;
- Transform direct = root.Find(path);
- if (direct != null)
- return direct;
- string[] segments = path.Split('/');
- string leaf = segments[segments.Length - 1];
- return FindByName(root, leaf);
- }
- public static Transform FindByName(Transform root, string name)
- {
- if (root == null)
- return null;
- if (root.name == name)
- return root;
- for (int i = 0; i < root.childCount; i++)
- {
- Transform match = FindByName(root.GetChild(i), name);
- if (match != null)
- return match;
- }
- return null;
- }
- public static T Get<T>(Transform root, string path) where T : Component
- {
- Transform target = Find(root, path);
- return target != null ? target.GetComponent<T>() : null;
- }
- public static TMP_Text Text(Transform root, string path)
- {
- return Get<TMP_Text>(root, path);
- }
- public static Image Image(Transform root, string path)
- {
- return Get<Image>(root, path);
- }
- public static Button Button(Transform root, string path, bool ensure = true)
- {
- Transform target = Find(root, path);
- if (target == null)
- return null;
- return EnsureButton(target);
- }
- public static Button EnsureButton(Transform target)
- {
- if (target == null)
- return null;
- Button button = target.GetComponent<Button>();
- if (button == null)
- {
- button = target.gameObject.AddComponent<Button>();
- Graphic graphic = target.GetComponent<Graphic>();
- ConfigureButton(button, graphic);
- }
- return button;
- }
- public static void ConfigureButton(Button button, Graphic targetGraphic)
- {
- if (button == null)
- return;
- if (targetGraphic != null)
- button.targetGraphic = targetGraphic;
- button.transition = Selectable.Transition.ColorTint;
- button.colors = new ColorBlock
- {
- normalColor = Color.white,
- highlightedColor = new Color(0.96f, 0.96f, 0.96f, 1f),
- pressedColor = new Color(0.86f, 0.86f, 0.86f, 1f),
- selectedColor = Color.white,
- disabledColor = new Color(0.72f, 0.72f, 0.72f, 0.72f),
- colorMultiplier = 1f,
- fadeDuration = 0.08f
- };
- button.navigation = new Navigation { mode = Navigation.Mode.None };
- }
- public static void Bind(Button button, UnityAction action)
- {
- if (button == null || action == null)
- return;
- button.onClick.RemoveListener(action);
- button.onClick.RemoveListener(GameAudio.PlayTap);
- button.onClick.AddListener(GameAudio.PlayTap);
- button.onClick.AddListener(action);
- }
- public static void EnsureGraphicRaycast(Transform target)
- {
- Graphic graphic = target != null ? target.GetComponent<Graphic>() : null;
- if (graphic != null)
- graphic.raycastTarget = true;
- }
- public static void SetProgress(Transform fillContainer, float normalized)
- {
- if (fillContainer == null || fillContainer.childCount == 0)
- return;
- RectTransform container = fillContainer as RectTransform;
- RectTransform fillImage = fillContainer.GetChild(0) as RectTransform;
- if (container == null || fillImage == null)
- return;
- float maxWidth = Mathf.Max(fillImage.rect.width, fillImage.sizeDelta.x);
- if (maxWidth <= 0f)
- maxWidth = container.rect.width;
- if (maxWidth <= 0f)
- return;
- Vector2 anchor = new Vector2(0f, fillImage.anchorMin.y);
- fillImage.anchorMin = anchor;
- fillImage.anchorMax = new Vector2(0f, fillImage.anchorMax.y);
- fillImage.pivot = new Vector2(0f, fillImage.pivot.y);
- fillImage.anchoredPosition = new Vector2(0f, fillImage.anchoredPosition.y);
- fillImage.sizeDelta = new Vector2(maxWidth, fillImage.sizeDelta.y);
- container.sizeDelta = new Vector2(maxWidth * Mathf.Clamp01(normalized), container.sizeDelta.y);
- }
- public static void SetGraphicVisible(Transform root, string path, bool visible)
- {
- Transform target = Find(root, path);
- if (target != null)
- target.gameObject.SetActive(visible);
- }
- public static void EnableAutoSize(TMP_Text text, float maxSize, float minSize)
- {
- if (text == null)
- return;
- text.enableAutoSizing = true;
- text.fontSizeMax = maxSize;
- text.fontSizeMin = minSize;
- }
- public static string FormatNumber(int value)
- {
- return value.ToString("N0", CultureInfo.InvariantCulture);
- }
- public static Sprite LoadAvatarSprite(int itemIndex)
- {
- if (AvatarSprites.TryGetValue(itemIndex, out Sprite cached))
- return cached;
- string path = "UI/items/spr_item_" + itemIndex;
- Sprite sprite = Resources.Load<Sprite>(path);
- AvatarSprites[itemIndex] = sprite;
- return sprite;
- }
- public static Sprite LoadSprite(string resourcePath)
- {
- if (string.IsNullOrEmpty(resourcePath))
- return null;
- return Resources.Load<Sprite>(resourcePath);
- }
- }
|