using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using RR.Game2D.Core.Managers;
namespace RR.Common.UI
{
///
/// Модальное окно для отображения QR-кодов.
/// Создаёт Image-объекты с QR-кодами динамически в указанном контейнере.
///
public class QRCodeModal : Modal
{
/// Текущий экземпляр на сцене. Регистрируется автоматически.
public static QRCodeModal Current { get; private set; }
[Header("Контент")]
[Tooltip("Контейнер для размещения QR-кодов. QR-коды создаются динамически.")]
[SerializeField] private RectTransform qrCodesContainer;
private readonly List _createdObjects = new();
protected override void Awake()
{
Current = this;
base.Awake(); // деактивирует GO, но ссылка Current остаётся валидной
}
private void OnDestroy()
{
if (Current == this) Current = null;
}
///
/// Показывает модальное окно с указанными QR-кодами.
///
/// Массив текстур QR-кодов для отображения.
public void ShowQRCodes(Texture2D[] qrTextures)
{
ClearQRCodes();
if (qrCodesContainer == null)
{
Debug.LogWarning("[QRCodeModal] Контейнер для QR-кодов не назначен.");
return;
}
if (qrTextures == null || qrTextures.Length == 0)
{
Debug.LogWarning("[QRCodeModal] Нет QR-кодов для отображения.");
return;
}
foreach (Texture2D tex in qrTextures)
{
if (tex == null) continue;
GameObject qrObj = new GameObject("QRCode", typeof(RectTransform), typeof(Image));
qrObj.transform.SetParent(qrCodesContainer, false);
Sprite sprite = Sprite.Create(
tex,
new Rect(0, 0, tex.width, tex.height),
new Vector2(0.5f, 0.5f));
Image image = qrObj.GetComponent();
image.sprite = sprite;
image.raycastTarget = false;
_createdObjects.Add(qrObj);
}
Open();
GameManager.Instance?.SetPause(true);
}
///
/// Очищает созданные QR-код объекты.
///
private void ClearQRCodes()
{
foreach (GameObject obj in _createdObjects)
{
if (obj != null) Destroy(obj);
}
_createdObjects.Clear();
}
public override void Close()
{
base.Close();
ClearQRCodes();
GameManager.Instance?.SetPause(false);
}
protected override void OnDisable()
{
base.OnDisable();
ClearQRCodes();
GameManager.Instance?.SetPause(false);
}
}
}