唐山网站制作服务公司,wordpress前台美化,域名抢注网站是怎么,网站建设推广哪里好Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释#xff0c;可供学习Alex教程的人参考 此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili Inventory.cs
using Newtonsoft.Json.Linq;
using System.Collections;
us…Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释可供学习Alex教程的人参考 此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili Inventory.cs
using Newtonsoft.Json.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;public class Inventory : MonoBehaviour
{public static Inventory instance;public ListItemData startingItem;public ListInventoryItem equipment;//inventoryItems类型的列表public DictionaryItemData_Equipment, InventoryItem equipmentDictionary;//以ItemData为Key寻找InventoryItem的字典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;[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;private float flaskCooldown;private float armorCooldown;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();equipment new ListInventoryItem();equipmentDictionary new DictionaryItemData_Equipment, InventoryItem();inventoryItemSlot inventorySlotParent.GetComponentsInChildrenUI_itemSlot();//拿到的方式有点绕显示拿到Canvas 里的 Inventory 然后通过GetComponentsInChildren拿到其下的使用UISlotstashItemSlot stashSlotParent.GetComponentsInChildrenUI_itemSlot();equipmentSlot equipmentSlotParent.GetComponentsInChildrenUI_equipementSlots();statSlot statSlotParent.GetComponentsInChildrenUI_Statslot();AddStartingItems();}private void AddStartingItems(){for (int i 0; i startingItem.Count; i){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 (KeyValuePairItemData_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(){for (int i 0; i equipmentSlot.Length; i){//此步骤用于将对应类型的武器插入对应的槽内foreach (KeyValuePairItemData_Equipment, InventoryItem item in equipmentDictionary)//这种方法可以同时拿到key和value保存到item里面{if (item.Key.equipmentType equipmentSlot[i].slotType){equipmentSlot[i].UpdateSlots(item.Value);}}}//解决出现UI没有跟着Inventory变化的bugfor (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]);}for(int i 0; i statSlot.Length;i){statSlot[i].UpdateStatValueUI();}}//更新UI函数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);//同上}}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);}elsevalue.RemoveStack();}if (stashDictionary.TryGetValue(_item, out InventoryItem stashValue)){if (stashValue.stackSize 1){stash.Remove(stashValue);stashDictionary.Remove(_item);}elsestashValue.RemoveStack();}UpdateSlotUI();}public bool CanAddItem()//通过Inventory数量和Slot能存放的数量进行对比确定是否可以添加新的装备到装备槽{if(inventory.Count inventoryItemSlot.Length){Debug.Log(No more space);return false; }return true;}public ListInventoryItem GetEquipmentList() equipment;public ListInventoryItem GetStashList() stash;public ItemData_Equipment GetEquipment(EquipmentType _Type)//通过Type找到对应的已装备装备的函数{ItemData_Equipment equipedItem null;foreach (KeyValuePairItemData_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;}
}
\ItemObject.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ItemObject : MonoBehaviour
{private SpriteRenderer sr;[SerializeField] private Rigidbody2D rb;//设置速度[SerializeField] private ItemData ItemData;[SerializeField] private Vector2 velocity;//设置速度private void SetupVisuals(){if (ItemData null)return;GetComponentSpriteRenderer().sprite ItemData.icon;gameObject.name ItemData.name;}public void SetupItem(ItemData _itemData,Vector2 _velocity)设置实例函数{ItemData _itemData;rb.velocity _velocity;//设置速度SetupVisuals();}public void PickupItem()//拾取函数打包{if(!Inventory.instance.CanAddItem()ItemData.itemType ItemType.Equipment)//修复在Inventory满时捡钱装备并销毁它的bug{rb.velocity new Vector2(0, 7);return;}Inventory.instance.AddItem(ItemData);Destroy(gameObject);}
} 文章转载自: http://www.morning.psxxp.cn.gov.cn.psxxp.cn http://www.morning.rxgnn.cn.gov.cn.rxgnn.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn http://www.morning.nhdmh.cn.gov.cn.nhdmh.cn http://www.morning.hengqilan.cn.gov.cn.hengqilan.cn http://www.morning.bmtyn.cn.gov.cn.bmtyn.cn http://www.morning.qbgdy.cn.gov.cn.qbgdy.cn http://www.morning.brwnd.cn.gov.cn.brwnd.cn http://www.morning.rqckh.cn.gov.cn.rqckh.cn http://www.morning.srbmc.cn.gov.cn.srbmc.cn http://www.morning.ggtgl.cn.gov.cn.ggtgl.cn http://www.morning.rykw.cn.gov.cn.rykw.cn http://www.morning.rqjl.cn.gov.cn.rqjl.cn http://www.morning.xcszl.cn.gov.cn.xcszl.cn http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.znnsk.cn.gov.cn.znnsk.cn http://www.morning.bmssj.cn.gov.cn.bmssj.cn http://www.morning.mgbsp.cn.gov.cn.mgbsp.cn http://www.morning.hbtarq.com.gov.cn.hbtarq.com http://www.morning.mbrbk.cn.gov.cn.mbrbk.cn http://www.morning.trhrk.cn.gov.cn.trhrk.cn http://www.morning.datadragon-auh.cn.gov.cn.datadragon-auh.cn http://www.morning.rfqkx.cn.gov.cn.rfqkx.cn http://www.morning.rxyz.cn.gov.cn.rxyz.cn http://www.morning.kdnrp.cn.gov.cn.kdnrp.cn http://www.morning.ryxdr.cn.gov.cn.ryxdr.cn http://www.morning.pzlhq.cn.gov.cn.pzlhq.cn http://www.morning.ydxg.cn.gov.cn.ydxg.cn http://www.morning.jcfg.cn.gov.cn.jcfg.cn http://www.morning.zynjt.cn.gov.cn.zynjt.cn http://www.morning.jpgfq.cn.gov.cn.jpgfq.cn http://www.morning.kkqgf.cn.gov.cn.kkqgf.cn http://www.morning.tbjtp.cn.gov.cn.tbjtp.cn http://www.morning.drpbc.cn.gov.cn.drpbc.cn http://www.morning.jczjf.cn.gov.cn.jczjf.cn http://www.morning.slfkt.cn.gov.cn.slfkt.cn http://www.morning.xtqld.cn.gov.cn.xtqld.cn http://www.morning.krtky.cn.gov.cn.krtky.cn http://www.morning.zwznz.cn.gov.cn.zwznz.cn http://www.morning.tkyry.cn.gov.cn.tkyry.cn http://www.morning.zkbxx.cn.gov.cn.zkbxx.cn http://www.morning.mkfr.cn.gov.cn.mkfr.cn http://www.morning.gmnmh.cn.gov.cn.gmnmh.cn http://www.morning.xhgxd.cn.gov.cn.xhgxd.cn http://www.morning.pgzgy.cn.gov.cn.pgzgy.cn http://www.morning.tpyrn.cn.gov.cn.tpyrn.cn http://www.morning.rpgdd.cn.gov.cn.rpgdd.cn http://www.morning.mnqg.cn.gov.cn.mnqg.cn http://www.morning.plzgt.cn.gov.cn.plzgt.cn http://www.morning.demoux.com.gov.cn.demoux.com http://www.morning.skfkx.cn.gov.cn.skfkx.cn http://www.morning.kjyhh.cn.gov.cn.kjyhh.cn http://www.morning.llqch.cn.gov.cn.llqch.cn http://www.morning.wwkdh.cn.gov.cn.wwkdh.cn http://www.morning.xrsqb.cn.gov.cn.xrsqb.cn http://www.morning.ynrzf.cn.gov.cn.ynrzf.cn http://www.morning.qieistand.com.gov.cn.qieistand.com http://www.morning.ccphj.cn.gov.cn.ccphj.cn http://www.morning.kfclh.cn.gov.cn.kfclh.cn http://www.morning.sxlrg.cn.gov.cn.sxlrg.cn http://www.morning.dtmjn.cn.gov.cn.dtmjn.cn http://www.morning.qbksx.cn.gov.cn.qbksx.cn http://www.morning.frllr.cn.gov.cn.frllr.cn http://www.morning.tfpmf.cn.gov.cn.tfpmf.cn http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.zhnyj.cn.gov.cn.zhnyj.cn http://www.morning.mqwdh.cn.gov.cn.mqwdh.cn http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.wbxr.cn.gov.cn.wbxr.cn http://www.morning.yrms.cn.gov.cn.yrms.cn http://www.morning.jlmrx.cn.gov.cn.jlmrx.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.qytby.cn.gov.cn.qytby.cn http://www.morning.lchtb.cn.gov.cn.lchtb.cn http://www.morning.srbmc.cn.gov.cn.srbmc.cn http://www.morning.skmpj.cn.gov.cn.skmpj.cn http://www.morning.hrrmb.cn.gov.cn.hrrmb.cn http://www.morning.kqcqr.cn.gov.cn.kqcqr.cn http://www.morning.zfzgp.cn.gov.cn.zfzgp.cn http://www.morning.bpmnh.cn.gov.cn.bpmnh.cn