示例#1
0
    /// <summary>
    /// This function is used to equip items onto this bodypart.
    /// Items will be placed onto the position of this bodypart.
    /// If this bodypart was already holding an item, the previous item will be unequipped.
    /// This function also works with prefabs. If the itemToEquip is a prefab, it'll automatically instantiate the item instead of teleporting it.
    /// </summary>
    /// <param name="itemToEquip">The item to equip.</param>
    /// <returns>Return the equipped item.</returns>
    public virtual Item EquipItem(Item itemToEquip)
    {
        BodyPart previousBodyPart = playerBody.GetBodyPartFromEquippedItem(itemToEquip, new[] { GetType() });

        if (previousBodyPart != null && previousBodyPart != this)
        {
            previousBodyPart.EquipItem(null);
        }

        if (equippedItem)
        {
            equippedItem.OnUnEquip();
        }

        equippedItem = itemToEquip;

        // Can be null in case we equip an empty item (could be used for unequipping for example)
        if (equippedItem == null)
        {
            return(null);
        }

        if (equippedItem.gameObject?.scene.IsValid() == false)
        {
            equippedItem = Instantiate(itemToEquip, transform);
        }
        else
        {
            equippedItem.gameObject?.SetActive(true);
            equippedItem.transform.SetParent(transform, false);
            equippedItem.transform.position = transform.position;
        }

        equippedItem.OnEquip(playerBody.Player);
        return(equippedItem);
    }