diff --git a/Assets/PerfectWorld/Scripts/GameData/GShopLoader.cs b/Assets/PerfectWorld/Scripts/GameData/GShopLoader.cs index 00c3795227..90f21734fa 100644 --- a/Assets/PerfectWorld/Scripts/GameData/GShopLoader.cs +++ b/Assets/PerfectWorld/Scripts/GameData/GShopLoader.cs @@ -17,6 +17,12 @@ public class GShopLoader : MonoBehaviour [Header("Loaded Data")] public GShopData primaryShop = new GShopData(); public GShopData secondaryShop = new GShopData(); + + /// True after gshop.txt has been loaded and parsed successfully. + public bool IsPrimaryShopLoaded { get; private set; } + + /// Invoked once when data is ready. + public event Action OnPrimaryShopLoaded; async void Start() { @@ -37,6 +43,8 @@ public class GShopLoader : MonoBehaviour if (await LoadShopData(GSHOP_ADDRESS, primaryShop)) { Debug.Log($"Primary shop loaded: {primaryShop.items.Count} items, {primaryShop.mainTypes.Count} categories"); + IsPrimaryShopLoaded = true; + OnPrimaryShopLoaded?.Invoke(); //LogShopData("Primary Shop", primaryShop); } diff --git a/Assets/PerfectWorld/Scripts/UI/ShopUIManager.cs b/Assets/PerfectWorld/Scripts/UI/ShopUIManager.cs index 76dac792ff..19baddf32f 100644 --- a/Assets/PerfectWorld/Scripts/UI/ShopUIManager.cs +++ b/Assets/PerfectWorld/Scripts/UI/ShopUIManager.cs @@ -52,11 +52,37 @@ public class ShopUIManager : MonoBehaviour //-1 means all sub types public SubTypeShop subTypeShop; private int currentSubType = -1; + private bool _shopLoaderEventsSubscribed; + void Start() { InitializeUI(); SetupEventListeners(); InitializePool(); + SubscribeShopLoaderEvents(); + } + + void SubscribeShopLoaderEvents() + { + if (_shopLoaderEventsSubscribed) + return; + if (shopLoader == null) + shopLoader = FindFirstObjectByType(); + if (shopLoader == null) + return; + + shopLoader.OnPrimaryShopLoaded += OnPrimaryShopDataReady; + _shopLoaderEventsSubscribed = true; + + // Load may have finished before we subscribed (e.g. script order). + if (shopLoader.IsPrimaryShopLoaded) + OnPrimaryShopDataReady(); + } + + void OnPrimaryShopDataReady() + { + if (shopMainPanel != null && shopMainPanel.activeSelf) + RefreshShopDisplay(); } void InitializePool() @@ -106,8 +132,10 @@ public class ShopUIManager : MonoBehaviour { if (shopMainPanel != null) { - OnCategorySelected(0); - RefreshShopDisplay(); + SubscribeShopLoaderEvents(); + shopMainPanel.SetActive(true); + // Always apply category 0 when opening; OnCategorySelected(0) no-ops if currentCategory is already 0. + OnCategorySelected(0, forceRefresh: true); ApplyPendingCash(); UnityGameSession.RequesrQueryPlayerCash(); } @@ -175,9 +203,9 @@ public class ShopUIManager : MonoBehaviour shopDetailPanel.SetActive(false); } - void OnCategorySelected(int categoryIndex) + void OnCategorySelected(int categoryIndex, bool forceRefresh = false) { - if (categoryIndex == currentCategory) + if (!forceRefresh && categoryIndex == currentCategory) return; float startTime = Time.realtimeSinceStartup; @@ -572,6 +600,9 @@ public class ShopUIManager : MonoBehaviour void OnDestroy() { + if (shopLoader != null && _shopLoaderEventsSubscribed) + shopLoader.OnPrimaryShopLoaded -= OnPrimaryShopDataReady; + // Clean up pooled objects if (useObjectPooling && itemPanelPool != null) {