Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
ItemData_Equipment.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum EquipmentType
{
sword,
armor
}
[CreateAssetMenu(fileName = "New Item Data", menuName = "Data/Equipment")]
public class ItemData_Equipment : ItemData
{
public EquipmentType equipmentType;
}
Inventory.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public static Inventory instance;
public List<InventoryItem> inventory;//inventoryItems类型的列表
public Dictionary<ItemData, InventoryItem> inventoryDictionary;//以ItemData为Key寻找InventoryItem的字典
public List<InventoryItem> stash;
public Dictionary<ItemData, InventoryItem> stashDictionary;
[Header("Inventory UI")]
[SerializeField] private Transform inventorySlotParent;
[SerializeField] private Transform stashSlotParent;
private UI_itemSlot[] inventoryItemSlot;//UI Slot的数组
private UI_itemSlot[] stashItemSlot;
private void Awake()
{
if (instance == null)
instance = this;
else
Destroy(gameObject);
//防止多次创建Inventory
}
public void Start()
{
inventory = new List<InventoryItem>();
inventoryDictionary = new Dictionary<ItemData, InventoryItem>();
stash = new List<InventoryItem>();
stashDictionary = new Dictionary<ItemData, InventoryItem>();
inventoryItemSlot = inventorySlotParent.GetComponentsInChildren<UI_itemSlot>();//拿到的方式有点绕,显示拿到Canvas 里的 Inventory 然后通过GetComponentsInChildren拿到其下的使用UISlot
stashItemSlot = stashSlotParent.GetComponentsInChildren<UI_itemSlot>();
}
private void UpdateSlotUI()
{
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]);
}
}
public void AddItem(ItemData _item)//将物体存入Inventory的函数
{
if(_item.itemType == ItemType.Equipment)
{
AddToInventory(_item);
}
else if(_item.itemType == ItemType.Material)
{
AddToStash(_item);
}
UpdateSlotUI();
}
private void AddToStash(ItemData _item)//向stash加物体的函数
{
if (stashDictionary.TryGetValue(_item, out InventoryItem value))//只有这种方法才能在查找到是否存在key对应value是否存在的同时,能够同时拿到value,其他方法的拿不到value
{
value.AddStack();
}//字典的使用,通过ItemData类型的数据找到InventoryItem里的与之对应的同样类型的数据
else//初始时由于没有相同类型的物体,故调用else是为了初始化库存,使其中含有一个基本的值
{
InventoryItem newItem = new InventoryItem(_item);
stash.Add(newItem);//填进列表里只有一次
stashDictionary.Add(_item, newItem);//同上
}
}
private void AddToInventory(ItemData _item)
{
if (inventoryDictionary.TryGetValue(_item, out InventoryItem value))//只有这种方法才能在查找到是否存在key对应value是否存在的同时,能够同时拿到value,其他方法的拿不到value
{
value.AddStack();
}//字典的使用,通过ItemData类型的数据找到InventoryItem里的与之对应的同样类型的数据
else//初始时由于没有相同类型的物体,故调用else是为了初始化库存,使其中含有一个基本的值
{
InventoryItem newItem = new InventoryItem(_item);
inventory.Add(newItem);//填进列表里只有一次
inventoryDictionary.Add(_item, newItem);//同上
}
}
public void RemoveItem(ItemData _item)//将物体剔除Inventory的函数
{
if(inventoryDictionary.TryGetValue(_item,out InventoryItem value))
{
if (value.stackSize <= 1)
{
inventory.Remove(value);
inventoryDictionary.Remove(_item);
}
else
value.RemoveStack();
}
if (stashDictionary.TryGetValue(_item, out InventoryItem stashValue))
{
if (stashValue.stackSize <= 1)
{
stash.Remove(value);
stashDictionary.Remove(_item);
}
else
stashValue.RemoveStack();
}
UpdateSlotUI();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public static Inventory instance;
public List<InventoryItem> inventory;//inventoryItems类型的列表
public Dictionary<ItemData, InventoryItem> inventoryDictionary;//以ItemData为Key寻找InventoryItem的字典
public List<InventoryItem> stash;
public Dictionary<ItemData, InventoryItem> stashDictionary;
[Header("Inventory UI")]
[SerializeField] private Transform inventorySlotParent;
[SerializeField] private Transform stashSlotParent;
private UI_itemSlot[] inventoryItemSlot;//UI Slot的数组
private UI_itemSlot[] stashItemSlot;
private void Awake()
{
if (instance == null)
instance = this;
else
Destroy(gameObject);
//防止多次创建Inventory
}
public void Start()
{
inventory = new List<InventoryItem>();
inventoryDictionary = new Dictionary<ItemData, InventoryItem>();
stash = new List<InventoryItem>();
stashDictionary = new Dictionary<ItemData, InventoryItem>();
inventoryItemSlot = inventorySlotParent.GetComponentsInChildren<UI_itemSlot>();//拿到的方式有点绕,显示拿到Canvas 里的 Inventory 然后通过GetComponentsInChildren拿到其下的使用UISlot
stashItemSlot = stashSlotParent.GetComponentsInChildren<UI_itemSlot>();
}
private void UpdateSlotUI()
{
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]);
}
}
public void AddItem(ItemData _item)//将物体存入Inventory的函数
{
if(_item.itemType == ItemType.Equipment)
{
AddToInventory(_item);
}
else if(_item.itemType == ItemType.Material)
{
AddToStash(_item);
}
UpdateSlotUI();
}
private void AddToStash(ItemData _item)//向stash加物体的函数
{
if (stashDictionary.TryGetValue(_item, out InventoryItem value))//只有这种方法才能在查找到是否存在key对应value是否存在的同时,能够同时拿到value,其他方法的拿不到value
{
value.AddStack();
}//字典的使用,通过ItemData类型的数据找到InventoryItem里的与之对应的同样类型的数据
else//初始时由于没有相同类型的物体,故调用else是为了初始化库存,使其中含有一个基本的值
{
InventoryItem newItem = new InventoryItem(_item);
stash.Add(newItem);//填进列表里只有一次
stashDictionary.Add(_item, newItem);//同上
}
}
private void AddToInventory(ItemData _item)
{
if (inventoryDictionary.TryGetValue(_item, out InventoryItem value))//只有这种方法才能在查找到是否存在key对应value是否存在的同时,能够同时拿到value,其他方法的拿不到value
{
value.AddStack();
}//字典的使用,通过ItemData类型的数据找到InventoryItem里的与之对应的同样类型的数据
else//初始时由于没有相同类型的物体,故调用else是为了初始化库存,使其中含有一个基本的值
{
InventoryItem newItem = new InventoryItem(_item);
inventory.Add(newItem);//填进列表里只有一次
inventoryDictionary.Add(_item, newItem);//同上
}
}
public void RemoveItem(ItemData _item)//将物体剔除Inventory的函数
{
if(inventoryDictionary.TryGetValue(_item,out InventoryItem value))
{
if (value.stackSize <= 1)
{
inventory.Remove(value);
inventoryDictionary.Remove(_item);
}
else
value.RemoveStack();
}
if (stashDictionary.TryGetValue(_item, out InventoryItem stashValue))
{
if (stashValue.stackSize <= 1)
{
stash.Remove(value);
stashDictionary.Remove(_item);
}
else
stashValue.RemoveStack();
}
UpdateSlotUI();
}
}