怎么做外贸个人网站,张掖高端网站建设公司,登录信产部网站,青海网站建设企业Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释#xff0c;可供学习Alex教程的人参考 此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili ItemData_Equipment.cs
using System.Collections;
using System.Collecti…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 ListInventoryItem inventory;//inventoryItems类型的列表public DictionaryItemData, InventoryItem inventoryDictionary;//以ItemData为Key寻找InventoryItem的字典public ListInventoryItem stash;public DictionaryItemData, 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;elseDestroy(gameObject);//防止多次创建Inventory}public void Start(){inventory new ListInventoryItem();inventoryDictionary new DictionaryItemData, InventoryItem();stash new ListInventoryItem();stashDictionary new DictionaryItemData, InventoryItem();inventoryItemSlot inventorySlotParent.GetComponentsInChildrenUI_itemSlot();//拿到的方式有点绕显示拿到Canvas 里的 Inventory 然后通过GetComponentsInChildren拿到其下的使用UISlotstashItemSlot stashSlotParent.GetComponentsInChildrenUI_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);}elsevalue.RemoveStack();}if (stashDictionary.TryGetValue(_item, out InventoryItem stashValue)){if (stashValue.stackSize 1){stash.Remove(value);stashDictionary.Remove(_item);}elsestashValue.RemoveStack();}UpdateSlotUI();}}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Inventory : MonoBehaviour
{public static Inventory instance;public ListInventoryItem inventory;//inventoryItems类型的列表public DictionaryItemData, InventoryItem inventoryDictionary;//以ItemData为Key寻找InventoryItem的字典public ListInventoryItem stash;public DictionaryItemData, 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;elseDestroy(gameObject);//防止多次创建Inventory}public void Start(){inventory new ListInventoryItem();inventoryDictionary new DictionaryItemData, InventoryItem();stash new ListInventoryItem();stashDictionary new DictionaryItemData, InventoryItem();inventoryItemSlot inventorySlotParent.GetComponentsInChildrenUI_itemSlot();//拿到的方式有点绕显示拿到Canvas 里的 Inventory 然后通过GetComponentsInChildren拿到其下的使用UISlotstashItemSlot stashSlotParent.GetComponentsInChildrenUI_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);}elsevalue.RemoveStack();}if (stashDictionary.TryGetValue(_item, out InventoryItem stashValue)){if (stashValue.stackSize 1){stash.Remove(value);stashDictionary.Remove(_item);}elsestashValue.RemoveStack();}UpdateSlotUI();}}