Part76:Equip items on character

Part76:Equip items on character(P108)

1:效果和操作

概念拓展:
向上转型:把子类转化为父类(子可以用父),父对象可以是接口,利用继承到方式
向下转型: 把父类转化为子类(父可以用子)
向上转型是自由的向下转型是有约束条件的
//向上转型(自由的)
Human h=new Human();
h.eat();//人类
Animal a=new Animal();
a.eat();//动物
//父类-Human,子类-Animal,a-父类引用,new Human()-子类对象
Animal a=new Human();//向上转型


//向下转型(需要强制类型转换)
Father f=new Son() //先向上转型
Son s=(Son) f;//再进行向下转型,进行强制转换,子类引用s,父类引用f
效果:将装备类型的物品配备到虚拟人物上,且equipment设置四个装备栏,每个网格对应每个类型,
操作:equiped槽的初始化和前几节基本一致,用IPointerDownHandler以及OnPointerDown来检测对应slot的点击检测以及点击事件,当发生点击事件则调用EquipItem,将点击的对应物体进行如equiped槽处理,先对其进行类型检测,如果有同类型的物体则进行替换,如果无则直接根据所对应的类型进入对应槽

2:代码改变

阶段1:点击准备槽显示 “equiped new item”+item.data.name且限定类型为Equipment
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;

public class UI_itemSlot : MonoBehaviour ,IPointerDownHandler//是物品栏的每一个格子
{
    [SerializeField] private Image itemImage;//物品图片
    [SerializeField] private TextMeshProUGUI itemText;//物品数量文字

    public InventoryItem item;//获取物品信息与上述2属性绑定

    public void OnPointerDown(PointerEventData eventData)
    {
        if (item.data.itemDataType == ItemDataType.Equipment)
            Debug.Log("equiped new item" + item.data.name);
    }
}
//利用IPointerDownHandler来检测鼠标点下事件,当点下时调用OnPointerDown
阶段2:实现点击准备上Equip ment类
//新建UI_equipementSlots
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class UI_equipementSlots : UI_itemSlot
{
    public EquipmentType slotType;
 
    private void OnValidate()
    {
        gameObject.name = "Equipment slot -" + slotType.ToString();
    }
}
//修改脚本Inventory
		public List<InventoryItem> equipment;//inventoryItems类型的列表
    public Dictionary<ItemData_equipment, InventoryItem> equipmentDictionary;//以ItemData为Key寻找InventoryItem的字典
		[SerializeField] private Transform equipmentSlotParent;
    private UI_equipementSlots[] equipmentSlot;
		public void Start()
{
  
		   equipment = new List<InventoryItem>();
       equipmentDictionary = new Dictionary<ItemData_equipment, InventoryItem>(); 
        equipmentSlot = equipmentSlotParent.GetComponentsInChildren<UI_equipementSlots>();
    }

    public void EquipItem(ItemData _item)
    {
        //解决在itemdata里拿不到子类equipment里的enum的问题
        ItemData_equipment newEquipment = _item as ItemData_equipment;
        //将父类转换为子类
        InventoryItem newItem = new InventoryItem(newEquipment);
 
        ItemData_equipment oldEquipment = null;
 
        foreach (KeyValuePair<ItemData_equipment, InventoryItem> item in equipmentDictionary)//这种方法可以同时拿到key和value保存到item里面
        {
            if (item.Key.equipmentType == newEquipment.equipmentType)//将拿到的key与转换成itemdata_equipment类型的_item的type对比拿到存在的key
            {
                oldEquipment = item.Key;//此key需保存在外部的data类型里
                //equipment.Remove(item.Value);
                //equipmentDictionary.Remove(item.Key);
            }
        }//好像用foreach里的value和key无法对外部的list和字典进行操作
 
        if (oldEquipment != null)
        {
            Unequipment(oldEquipment);
            AddItem(oldEquipment);
        }
        equipment.Add(newItem);
        equipmentDictionary.Add(newEquipment, newItem);
        RemoveItem(_item);
    }
 
    private void Unequipment(ItemData_equipment itemToRemove)//装备其他同类型的装备时。去除已装备的装备
    {
        if (equipmentDictionary.TryGetValue(itemToRemove, out InventoryItem value))
        {
            equipment.Remove(value);
            equipmentDictionary.Remove(itemToRemove);
        }
    }
 private void UpdateSlotUI()
    {
        for (int i = 0; i < equipmentSlot.Length; i++)
        {
            //此步骤用于将对应类型的武器插入对应的槽内
            foreach (KeyValuePair<itemData_Equipment, InventoryItem> item in equipmentDictionary)//这种方法可以同时拿到key和value保存到item里面
            {
                if (item.Key.equipmentType == equipmentSlot[i].slotType)
                {
                    equipmentSlot[i].UpdateSlots(item.Value);
                }
            }
 
        }
        //解决出现UI没有跟着Inventory变化的bug
        for (int i = 0; i < inventoryItemSlot.Length;i++)
        {
            inventoryItemSlot[i].CleanUpSlot();
        }
        for (int i = 0; i < stashItemSlot.Length; i++)
        {
            stashItemSlot[i].CleanUpSlot();
        }
 
        for (int i = 0; i < inventory.Count; i++)
        {
            inventoryItemSlot[i].UpdateSlots(inventory[i]);
        }
 
        for (int i = 0; i < stash.Count; i++)
        {
            stashItemSlot[i].UpdateSlots(stash[i]);
        }
    }
//修改脚本UI_itemSlot
public void CleanUpSlot()//解决出现UI没有跟着Inventory变化的bug
    {
        item = null;
 
        itemImage.sprite = null;
        itemImage.color = Color.clear;
        itemText.text = "";
    }
 
    public void OnPointerDown(PointerEventData eventData)
    {
        if (item.data.itemType == ItemType.Equipment)
            Inventory.instance.EquipItem(item.data);
    }

3:效果图

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值