using System.Collections; using TMPro; using UnityEngine; namespace BulletHellTemplate { /// /// Manages the In-App Purchase (IAP) shop, displaying purchasable items and handling purchase confirmations. /// public class UIShopIAP : MonoBehaviour { [Header("UI Elements")] [Tooltip("Popup used to confirm the purchase of an IAP item.")] public ShopIAPPopup buyPopup; // Popup for confirming the purchase [Tooltip("Prefab for each IAP item entry in the shop.")] public ShopIAPEntry shopEntryPrefab; // Prefab for each IAP item [Tooltip("Container to hold the IAP item entries.")] public Transform containerItems; // Container for IAP item entries public static UIShopIAP Singleton; // Singleton instance for easy access [Tooltip("Text used to display error messages.")] public TextMeshProUGUI errorMessage; // Error message text element private string currentLang; private void Awake() { // Ensure Singleton pattern if (Singleton == null) { Singleton = this; } } private void OnEnable() { currentLang = LanguageManager.LanguageManager.Instance.GetCurrentLanguage(); } private void Start() { // Load and display available IAP items from IAPManager when the shop starts LoadIAPItems(); } /// /// Loads the IAP items from IAPManager and instantiates their entries in the UI. /// private void LoadIAPItems() { // Clear existing entries before loading new ones ClearItems(); // Access the purchasable items directly from IAPManager foreach (IAPItem iapItem in IAPManager.Singleton.purchasableItems) { ShopIAPEntry shopEntry = Instantiate(shopEntryPrefab, containerItems); string itemNameTranslated = GetTranslatedString(iapItem.itemNameTranslated, iapItem.itemName,currentLang); string itemDescriptionTranslated = GetTranslatedString(iapItem.itemDescriptionTranslated, iapItem.itemDescription,currentLang); shopEntry.SetItemInfo(iapItem,itemNameTranslated,itemDescriptionTranslated); // Set IAP item info } } /// /// Clears all IAP item entries from the UI containerPassItems. /// private void ClearItems() { foreach (Transform child in containerItems) { Destroy(child.gameObject); } } /// /// Opens the purchase confirmation popup with the selected IAP item's details. /// /// The IAP item selected for purchase. public void OpenBuyPopup(IAPItem iapItem) { buyPopup.SetPopupInfo(iapItem); buyPopup.gameObject.SetActive(true); } public void ShowErrorMessage() { } /// /// Coroutine to clear the error message after a specified delay. /// /// The delay in seconds before clearing the message. /// An IEnumerator for coroutine execution. public IEnumerator ClearErrorMessageAfterDelay(float delay) { yield return new WaitForSeconds(delay); errorMessage.text = ""; } private string GetTranslatedString(NameTranslatedByLanguage[] translations, string fallback, string currentLang) { if (translations != null) { foreach (var trans in translations) { if (!string.IsNullOrEmpty(trans.LanguageId) && trans.LanguageId.Equals(currentLang) && !string.IsNullOrEmpty(trans.Translate)) { return trans.Translate; } } } return fallback; } private string GetTranslatedString(DescriptionTranslatedByLanguage[] translations, string fallback, string currentLang) { if (translations != null) { foreach (var trans in translations) { if (!string.IsNullOrEmpty(trans.LanguageId) && trans.LanguageId.Equals(currentLang) && !string.IsNullOrEmpty(trans.Translate)) { return trans.Translate; } } } return fallback; } } }