コード例 #1
0
        //public void SetOwner(InteractableObject targetObj)
        //{
        //    if (targetObj.TryGetComponent(out owner))
        //    {
        //        owner.RefreshInventory();
        //        PopulateLootInventory();
        //    }
        //    else
        //    {
        //        Debug.LogWarning("inventory not found for " + targetObj.name);
        //    }
        //}
        //public void SetOwner(ActorInventory _owner)
        //{
        //    owner = _owner;
        //    owner.RefreshInventory();
        //    PopulateInventory();

        //}
        public void PopulateInventory()
        {
            if (owner.ItemsInBag.Count <= 0)
            {
                Debug.Log("No Items in Bag");
                return;
            }
            if (content.childCount > 0 && owner.ItemsInBag.Count > 0)
            {
                for (int i = 0; i < content.childCount; i++)
                {
                    if (owner.HasItem(owner.ItemsInBag[i].ItemName))
                    {
                        GameObject go = content.GetChild(i).gameObject;
                        go.name = owner.ItemsInBag[i].ItemName;
                        go.SetActive(true);
                        go.GetComponentInChildren <TextMeshProUGUI>().text = owner.ItemsInBag[i].ItemName + "\n  x" + owner.ItemsInBag[i].quantity;
                        go.GetComponent <Image>().sprite = owner.FindItemQuantity(owner.ItemsInBag[i].ItemName, out int qty).GetSprite();
                        go.GetComponent <Button>().onClick.RemoveAllListeners();
                        go.GetComponent <Button>().onClick.AddListener(() => UseItem(go.name));
                        go.GetComponent <Button>().onClick.AddListener(() => UpdateInventory(go.name));
                    }
                }
                int lastIndex = content.childCount;

                for (int k = lastIndex; k < owner.ItemsInBag.Count; k++)
                {
                    GameObject go = Instantiate(ItembuttonPrefab, content);
                    go.name = owner.ItemsInBag[k].ItemName;
                    go.GetComponentInChildren <TextMeshProUGUI>().text = owner.ItemsInBag[k].ItemName + "\n  x" + owner.ItemsInBag[k].quantity;
                    go.GetComponent <Image>().sprite = owner.FindItemQuantity(owner.ItemsInBag[k].ItemName, out int qty).GetSprite();
                    go.GetComponent <Button>().onClick.RemoveAllListeners();
                    go.GetComponent <Button>().onClick.AddListener(() => UseItem(go.name));
                    go.GetComponent <Button>().onClick.AddListener(() => UpdateInventory(go.name));
                }

                return;
            }



            foreach (ItemFactoryData item in owner.ItemsInBag)
            {
                GameObject go = Instantiate(ItembuttonPrefab, content);
                go.name = item.ItemName;
                go.GetComponentInChildren <TextMeshProUGUI>().text = item.ItemName + "\n  x" + item.quantity;
                go.GetComponent <Image>().sprite = owner.FindItemQuantity(item.ItemName, out int qty).GetSprite();
                go.GetComponent <Button>().onClick.AddListener(() => UseItem(go.name));
                go.GetComponent <Button>().onClick.AddListener(() => UpdateInventory(go.name));
            }
        }
コード例 #2
0
    private void UpdateInventory(string itemname)
    {
        if (content.childCount == 0)
        {
            return;
        }

        for (int i = 0; i < content.childCount; i++)
        {
            if (content.GetChild(i).name == itemname)
            {
                owner.HasItem(itemname, out int qty);
                Debug.Log(itemname + " : " + qty);
                if (qty <= 0)
                {
                    content.GetChild(i).gameObject.SetActive(false);
                    return;
                }
                content.GetChild(i).transform.GetComponentInChildren <TextMeshProUGUI>().text = itemname + "\n  x" + qty;
            }
        }
    }