Пример #1
0
    protected void EquipItem()
    {
        ColorBlock colors;
        string     itemName;
        string     itemEquipped;

        //this loop checks if an item is already equipped by checking the Text of the items corresponding button
        foreach (GameObject item in items)
        {
            Button anItemButton     = item.transform.Find("PriceButton").GetComponent <Button>();
            Text   anItemButtonText = item.transform.Find("PriceButton").GetComponentInChildren <Text>();
            itemScript = item.GetComponent <ItemScript>();

            if (itemScript.IsEquipped())
            {
                itemName = itemScript.playerPrefabName;
                Debug.Log("Previously equipped: " + itemScript.playerPrefabName);
                itemEquipped = itemName + "Equipped";

                PlayerPrefs.SetInt(itemEquipped, 0); //unequip the item 0-false, 1-true
                itemScript.SetEquipped(false);
                anItemButton.interactable = true;

                anItemButtonText.text  = "Equip";
                anItemButtonText.color = new Color32(255, 255, 255, 255);

                colors              = anItemButton.colors;
                colors.normalColor  = new Color32(75, 75, 75, 255);
                anItemButton.colors = colors;
            }
        }

        itemButton     = EventSystem.current.currentSelectedGameObject.GetComponent <Button>();         //get the Button component of the clicked button
        itemButtonText = EventSystem.current.currentSelectedGameObject.GetComponentInChildren <Text>(); //get the Text component of the clicked button
        itemScript     = itemButton.GetComponentInParent <ItemScript>();

        playerPrefabLocation = itemButton.GetComponentInParent <ItemScript>().GetPrefabLocation();

        itemName     = itemScript.playerPrefabName;
        itemEquipped = itemName + "Equipped";

        Debug.Log("Now equipping: " + itemName);
        PlayerPrefs.SetInt(itemEquipped, 1); //equips the item 0-false, 1-true
        itemScript.SetEquipped(true);
        itemButton.interactable = false;

        colors               = itemButton.colors;
        colors.normalColor   = new Color32(185, 185, 185, 255);
        itemButtonText.color = new Color32(75, 75, 75, 255);
        itemButtonText.text  = "Equipped";
    }