避免装备多个物品

时间:2019-02-23 09:26:45

标签: c# unity3d

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

每次我在清单中选择一个项目时,它将添加一个附加的侦听器。这意味着多次单击原木药水,然后单击“饮料”(按钮)将饮用多种原木药水。

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

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被调用。

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);
    }
}

1 个答案:

答案 0 :(得分:1)

考虑仅一次添加侦听器

public class InventoryUIDetails : MonoBehaviour {

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

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

    public void OnItemInteract() {
        if (item == null)
            return;
        ...
    }
}