using TMPro; using UnityEngine; namespace RR.Common.UI { /// /// Отображение кнопки ячейки витрины. /// Поддерживает отдельные текстовые поля для номера ячейки и цены в очках. /// Если префаб ещё не обновлён, недостающие поля будут созданы автоматически. /// [DisallowMultipleComponent] public sealed class PrizeStandButtonView : MonoBehaviour { [Header("Labels")] [SerializeField] private TMP_Text boxNumberText; [SerializeField] private TMP_Text boxPriceText; [Header("Formatting")] [SerializeField] private string boxPriceFormat = "{0}"; public void Bind(int boxNumber, int requiredScore) { EnsureTextFields(); if (boxNumberText != null) { boxNumberText.text = boxNumber.ToString(); } if (boxPriceText != null) { boxPriceText.text = FormatPrice(requiredScore); } } private void EnsureTextFields() { if (boxNumberText != null && boxPriceText != null) { return; } bool createdMissingFields = false; TMP_Text[] texts = GetComponentsInChildren(true); if (boxNumberText == null && texts.Length > 0) { boxNumberText = texts[0]; } if (boxPriceText == null) { for (int i = 0; i < texts.Length; i++) { if (texts[i] != null && texts[i] != boxNumberText) { boxPriceText = texts[i]; break; } } } if (boxNumberText == null) { boxNumberText = CreateTextField("BoxNumberText"); createdMissingFields = true; } if (boxPriceText == null) { boxPriceText = CreateTextField("BoxPriceText", boxNumberText); createdMissingFields = true; } if (createdMissingFields) { ApplyDefaultLayout(); } } private TMP_Text CreateTextField(string objectName, TMP_Text template = null) { GameObject textObject = new(objectName, typeof(RectTransform), typeof(CanvasRenderer), typeof(TextMeshProUGUI)); textObject.transform.SetParent(transform, false); textObject.layer = gameObject.layer; TMP_Text created = textObject.GetComponent(); if (template != null) { created.font = template.font; created.fontSharedMaterial = template.fontSharedMaterial; created.fontSize = template.fontSize; created.fontSizeMin = template.fontSizeMin; created.fontSizeMax = template.fontSizeMax; created.enableAutoSizing = template.enableAutoSizing; created.color = template.color; created.alignment = template.alignment; created.raycastTarget = template.raycastTarget; } else { created.enableAutoSizing = true; created.fontSizeMin = 10; created.fontSizeMax = 36; created.color = Color.black; created.alignment = TextAlignmentOptions.Center; } return created; } private void ApplyDefaultLayout() { if (boxNumberText != null) { ConfigureTextRect(boxNumberText.rectTransform, new Vector2(0f, 0.48f), new Vector2(1f, 1f)); boxNumberText.alignment = TextAlignmentOptions.Center; } if (boxPriceText != null) { ConfigureTextRect(boxPriceText.rectTransform, new Vector2(0f, 0f), new Vector2(1f, 0.48f)); boxPriceText.alignment = TextAlignmentOptions.Center; } } private static void ConfigureTextRect(RectTransform rectTransform, Vector2 anchorMin, Vector2 anchorMax) { if (rectTransform == null) { return; } rectTransform.anchorMin = anchorMin; rectTransform.anchorMax = anchorMax; rectTransform.offsetMin = Vector2.zero; rectTransform.offsetMax = Vector2.zero; rectTransform.anchoredPosition = Vector2.zero; rectTransform.localScale = Vector3.one; } private string FormatPrice(int requiredScore) { if (string.IsNullOrWhiteSpace(boxPriceFormat)) { return requiredScore.ToString(); } try { return string.Format(boxPriceFormat, requiredScore); } catch { return requiredScore.ToString(); } } } }