Fix PW-137 mall not opening correct

This commit is contained in:
HungDK
2026-04-13 11:00:15 +07:00
parent ccc13db1e7
commit 2e6989c604
2 changed files with 43 additions and 4 deletions
@@ -17,6 +17,12 @@ public class GShopLoader : MonoBehaviour
[Header("Loaded Data")]
public GShopData primaryShop = new GShopData();
public GShopData secondaryShop = new GShopData();
/// <summary>True after gshop.txt has been loaded and parsed successfully.</summary>
public bool IsPrimaryShopLoaded { get; private set; }
/// <summary>Invoked once when <see cref="primaryShop"/> data is ready.</summary>
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);
}
@@ -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<GShopLoader>();
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)
{