Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
GameData.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class GameData
{
public int currency;
public SerializableDictionary<string, bool> skillTree;
public SerializableDictionary<string, int> inventory;
public List<string> equipmentId;
public GameData()
{
this.currency = 0;
skillTree = new SerializableDictionary<string, bool>();
inventory = new SerializableDictionary<string, int>();
equipmentId = new List<string>();
}
}
Inventory.cs
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class Inventory : MonoBehaviour, ISaveManager
{
public static Inventory instance;
public List<ItemData> startingItem;
public List<InventoryItem> equipment;//inventoryItems类型的列表
public Dictionary<ItemData_Equipment, InventoryItem> equipmentDictionary;//以ItemData为Key寻找InventoryItem的字典
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;
[SerializeField] private Transform equipmentSlotParent;
[SerializeField] private Transform statSlotParent;
private UI_itemSlot[] inventoryItemSlot;//UI Slot的数组
private UI_itemSlot[] stashItemSlot;
private UI_equipementSlots[] equipmentSlot;
private UI_statslot[] statSlot;
[Header("Items cooldown")]
private float lastTimeUsedFlask;
private float lastTimeUsedArmor;
public float flaskCooldown { get; private set; }
private float armorCooldown;
[Header("Data base")]
//public string[] assetsNames;//NameId
private List<ItemData> itemDataBase;//所有的equipmentData
public List<InventoryItem> loadedItems;
public List<ItemData_Equipment> loadedEquipment;
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>();
equipment = new List<InventoryItem>();
equipmentDictionary = new Dictionary<ItemData_Equipment, InventoryItem>();
inventoryItemSlot = inventorySlotParent.GetComponentsInChildren<UI_itemSlot>();//拿到的方式有点绕,显示拿到Canvas 里的 Inventory 然后通过GetComponentsInChildren拿到其下的使用UISlot
stashItemSlot = stashSlotParent.GetComponentsInChildren<UI_itemSlot>();
equipmentSlot = equipmentSlotParent.GetComponentsInChildren<UI_equipementSlots>();
statSlot = statSlotParent.GetComponentsInChildren<UI_statslot>();
AddStartingItems();
}
private void AddStartingItems()
{
foreach(ItemData_Equipment item in loadedEquipment)
{
EquipItem(item);
}
if(loadedItems.Count > 0)//载入文件存在才调用
{
foreach(InventoryItem item in loadedItems)
{
for(int i = 0;i<item.stackSize;i++)
{
AddItem(item.data);
}
}
return;
}
for (int i = 0; i < startingItem.Count; i++)//此语句只在游戏刚开的时候才执行
{
if (startingItem[i] != null)
AddItem(startingItem[i]);
}
}//设置初始物品
public void EquipItem(ItemData _item)
{
//解决在itemdata里拿不到子类equipment里的enum的问题
ItemData_Equipment newEquipment = _item as ItemData_Equipment;//https://www.bilibili.com/read/cv15551811/
//将父类转换为子类
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)
{
AddItem(oldEquipment);
Unequipment(oldEquipment);
}
equipment.Add(newItem);
equipmentDictionary.Add(newEquipment, newItem);
RemoveItem(_item);
newEquipment.AddModifiers();
UpdateSlotUI();
}//装备装备的函数
public void Unequipment(ItemData_Equipment itemToRemove)//装备其他同类型的装备时。去除已装备的装备
{
if (equipmentDictionary.TryGetValue(itemToRemove, out InventoryItem value))
{
equipment.Remove(value);
equipmentDictionary.Remove(itemToRemove);
itemToRemove.RemoveModifiers();
UpdateSlotUI();
}
}
private void UpdateSlotUI()//更新槽UI的函数
{
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]);
}
UpdateStatsUI();
}
public void UpdateStatsUI()//更新状态UI函数
{
for (int i = 0; i < statSlot.Length; i++)
{
statSlot[i].UpdateStatValueUI();
}
}
public void AddItem(ItemData _item)//添加物体的函数
{
if (_item.itemType == ItemType.Equipment && CanAddItem())//修复Inventory数量大于Slot能存放的数量时报错的Bug
{
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);//同上
}
UpdateSlotUI();
}
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);//同上
}
}//将物体存入Inventory的函数
public void RemoveItem(ItemData _item)//修复Inventory数量大于Slot能存放的数量时报错的Bug
{
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(stashValue);
stashDictionary.Remove(_item);
}
else
stashValue.RemoveStack();
}
UpdateSlotUI();
}
public bool CanAddItem()//通过Inventory数量和Slot能存放的数量进行对比,确定是否可以添加新的装备到装备槽
{
if (inventory.Count >= inventoryItemSlot.Length)
{
return false;
}
return true;
}
public List<InventoryItem> GetEquipmentList() => equipment;
public List<InventoryItem> GetStashList() => stash;
public ItemData_Equipment GetEquipment(EquipmentType _Type)//通过Type找到对应的已装备装备的函数
{
ItemData_Equipment equipedItem = null;
foreach (KeyValuePair<ItemData_Equipment, InventoryItem> item in equipmentDictionary)
if (item.Key.equipmentType == _Type)
{
equipedItem = item.Key;
}
return equipedItem;
}
public void UseFlask()//使用药瓶设置冷却时间
{
ItemData_Equipment currentFlask = GetEquipment(EquipmentType.Flask);
if (currentFlask == null)
return;
//使用药瓶设置冷却时间
bool canUseFlask = Time.time > lastTimeUsedFlask + flaskCooldown;
if (canUseFlask)
{
flaskCooldown = currentFlask.itemCooldown;
currentFlask.Effect(null);
lastTimeUsedFlask = Time.time;
}
else
{
Debug.Log("Flask is Cooldown");
}
}//使用药瓶函数
public bool CanUseArmor()
{
ItemData_Equipment currentArmor = GetEquipment(EquipmentType.Armor);
if (Time.time > lastTimeUsedArmor + armorCooldown)
{
lastTimeUsedArmor = Time.time;
armorCooldown = currentArmor.itemCooldown;
return true;
}
Debug.Log("Armor on cooldown");
return false;
}
public bool CanCraft(ItemData_Equipment _itemToCraft, List<InventoryItem> _requiredMaterials)
{
List<InventoryItem> materialsToRemove = new List<InventoryItem>();
for (int i = 0; i < _requiredMaterials.Count; i++)
{
if (stashDictionary.TryGetValue(_requiredMaterials[i].data, out InventoryItem stashValue))//判断数量是否足够
{
if (stashValue.stackSize < _requiredMaterials[i].stackSize)
{
Debug.Log("not enough materials");
return false;
}
else
{
for(int j = 0; j < _requiredMaterials[i].stackSize;j++)//修复创建物品耗费的物品数量不对
materialsToRemove.Add(stashValue);
}
}
else
{
Debug.Log("not enough materials");
return false;
}
}
for (int i = 0; i < materialsToRemove.Count; i++)
{
RemoveItem(materialsToRemove[i].data);
}
AddItem(_itemToCraft);
Debug.Log("Here is your item " + _itemToCraft.name);
return true;
}//检测材料足够制造对应装备的函数
public void LoadData(GameData _data)
{
foreach (KeyValuePair<string,int> pair in _data.inventory)//比较文件里保存data和所有data的,相同就保存在loadedItems里
{
foreach(var item in GetItemDataBase())
{
if(item != null && item.itemId == pair.Key)
{
InventoryItem itemToLoad = new InventoryItem(item);
itemToLoad.stackSize = pair.Value;//因为要保存的是两个变量故采用此方法,这也是InventoryItem存在的原因,需要保存数量
loadedItems.Add(itemToLoad);
}
}
}
foreach(string loadedItemId in _data.equipmentId)
{
foreach(var item in GetItemDataBase())
{
if(item != null && item.itemId == loadedItemId)
{
loadedEquipment.Add(item as ItemData_Equipment);
}
}
}
}
public void SaveData(ref GameData _data)
{
_data.inventory.Clear();
_data.equipmentId.Clear();
foreach (KeyValuePair<ItemData, InventoryItem> pair in inventoryDictionary)//遍历存储
{
_data.inventory.Add(pair.Key.itemId, pair.Value.stackSize);
}
foreach(KeyValuePair<ItemData,InventoryItem> pair in stashDictionary)
{
_data.inventory.Add(pair.Key.itemId, pair.Value.stackSize);
}
foreach(KeyValuePair<ItemData_Equipment,InventoryItem> pair in equipmentDictionary)
{
_data.equipmentId.Add(pair.Key.itemId);
}
}
private List<ItemData> GetItemDataBase()//获得所有的equipmentData的IdName和data的函数
{
itemDataBase = new List<ItemData>();
string[] assetsNames = AssetDatabase.FindAssets("", new[] { "Assets/Data/Items" });//这是根据unity的文件夹走的,也就是拿到了所有的Equipment的文件名IdName
foreach (string SOName in assetsNames)
{
var SOpath = AssetDatabase.GUIDToAssetPath(SOName);//这是通过找到的文件名拿到对应的位置
var itemData = AssetDatabase.LoadAssetAtPath<ItemData>(SOpath);//这是实打实的通过位置转换拿到相应的数据
itemDataBase.Add(itemData);//将数据填到itemDataBase里
}
return itemDataBase;
}
}