예제 #1
0
    void Start()
    {
        // TODO: instantiate this based on player's save game data.
        var saveData = GameManager.Instance.saveData;

        // Remove example items from hierarchy.
        gridLayoutGroup.gameObject.DestroyAllChildren();

        // Assign dynamic component references.
        playerStatsManager = FindObjectOfType <PlayerStatsManager>();
        UpdatePlayerCurrency(saveData.currency);

        itemsOwned = playerStatsManager.GetItemsOwned();
        foreach (var item in itemsOwned)
        {
            Debug.Log($"owned: {item.name}");
        }
        // itemsOwned = new List<ScriptableObject>();
        // itemsOwned =

        // Register purchase button event.
        purchaseButton.onClick.AddListener(() => PurchaseItem());

        exitButton.onClick.AddListener(() =>
        {
            SceneManager.UnloadSceneAsync("Scenes/Store");
        });

        if (playerStatsManager == null)
        {
            Debug.LogError("No player stats manager found, StoreUI cannot function correctly");
        }

        // Create UI entries for each unpurchased item.
        for (var i = 0; i < storeInventory.Length; i++)
        {
            var item = storeInventory[i];
            if (!itemsOwned.Contains(item))
            {
                var storeEntry = Instantiate(storeItemPrefab, gridLayoutGroup);
                var storeItem  = storeEntry.GetComponent <StoreItem>();
                storeItem.SetItem(item);
                storeEntry.GetComponent <Button>().onClick.AddListener(() => SetSelection(storeEntry));
                storeEntry.name = storeItem.GetItem().name;
            }
        }

        // Set initially selected element for event system.
        var initialSelection = gridLayoutGroup.childCount > 0
            ? gridLayoutGroup.GetChild(0).gameObject
            : exitButton.gameObject;

        EventSystem.current.SetSelectedGameObject(initialSelection);
    }