首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >避免多装备项目

避免多装备项目
EN

Stack Overflow用户
提问于 2019-02-23 09:26:44
回答 1查看 93关注 0票数 0

在InventoryUIDetails类中,在SetItem函数中,我们向itemInteractButton的onClick事件添加一个侦听器。

每次我在库存中选择一个项目时,它都会添加一个额外的侦听器。这意味着多次单击日志药水,然后单击“饮料”(按钮)将饮用多个日志药水。

我应该在哪里调用RemoveAllListeners(),以确保它是在单击interact按钮时调用的唯一函数。

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class InventoryUIDetails : MonoBehaviour {

    Item item;
    Button selectedItemButton, itemInteractButton;
    Text itemNameText, itemDescriptionText, itemInteractButtonText; 

    public Text statText;

    void Start() {
        itemNameText = transform.Find ("Item_Name").GetComponent<Text> ();
        itemDescriptionText = transform.Find ("Item_Description").GetComponent<Text> ();
        itemInteractButton = transform.GetComponentInChildren<Button> ();
        itemInteractButtonText = itemInteractButton.GetComponentInChildren<Text>();
        gameObject.SetActive (false);
    }

    public void SetItem(Item item, Button selectedButton) {
        gameObject.SetActive (true);

        statText.text = "";
        if (item.Stats != null) {
            foreach(BaseStat stat in item.Stats) {
                statText.text += stat.StatName + ": " + stat.BaseValue + "\n";
            }
        }

        itemInteractButton.onClick.RemoveAllListeners ();

        this.item = item;
        selectedItemButton = selectedButton;
        itemNameText.text = item.ItemName;
        itemDescriptionText.text = item.Description;
        itemInteractButtonText.text = item.ActionName;

        itemInteractButton.onClick.AddListener (OnItemInteract);
    }

    public void OnItemInteract() {
        if (item.ItemType == Item.ItemTypes.Consumable) {
            InventoryController.Instance.ConsumeItem (item);
            Destroy (selectedItemButton.gameObject);
        } else if (item.ItemType == Item.ItemTypes.Weapon) {
            InventoryController.Instance.EquipItem (item);
            Destroy (selectedItemButton.gameObject);
        }

        item = null;
        gameObject.SetActive (false);
    }
}

在这个类中,SetItem调用.

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class InventoryController : MonoBehaviour {

    public static InventoryController Instance { get; set; }
    public PlayerWeaponController playerWeaponController;
    public ConsumableController consumableController;
    public InventoryUIDetails inventoryDetailsPanel;
    public List<Item> playerItems = new List<Item> ();

    void Start() {

        if (Instance != null && Instance != this)
            Destroy (gameObject);
        else
            Instance = this;

        playerWeaponController = GetComponent<PlayerWeaponController> ();
        consumableController = GetComponent<ConsumableController> ();

        GiveItem ("Kampilan");
        GiveItem ("potion_log");
    }

    public void GiveItem(string itemSlug) {
        Item item = ItemDatabase.Instance.GetItem (itemSlug);
        playerItems.Add(item);
        Debug.Log (playerItems.Count + " items in inventory. Added: " + itemSlug);
        UIEventHandler.ItemAddedToInventory (item);
    }

    public void SetItemDetails(Item item, Button selectedButton) {
        inventoryDetailsPanel.SetItem (item, selectedButton);
    }

    public void EquipItem(Item itemToEquip) {
        playerWeaponController.EquipWeapon (itemToEquip);
    }

    public void ConsumeItem(Item itemToConsume) {
        consumableController.ConsumeItem (itemToConsume);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-23 10:04:08

考虑只添加侦听器一次

代码语言:javascript
复制
public class InventoryUIDetails : MonoBehaviour {

    void Start() {
        ...
        itemInteractButton.onClick.AddListener (OnItemInteract);
    }

    void OnDestroy() {
        itemInteractButton.onClick.RemoveListener (OnItemInteract);
    }

    public void OnItemInteract() {
        if (item == null)
            return;
        ...
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54840143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档