国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁 > news >正文

新吳區(qū)推薦做網(wǎng)站電話2021國內(nèi)最好用免費(fèi)建站系統(tǒng)

新吳區(qū)推薦做網(wǎng)站電話,2021國內(nèi)最好用免費(fèi)建站系統(tǒng),廣西柳州網(wǎng)站建設(shè),百度快照提交Alex教程每一P的教程原代碼加上我自己的理解初步理解寫的注釋,可供學(xué)習(xí)Alex教程的人參考 此代碼僅為較上一P有所改變的代碼 【Unity教程】從0編程制作類銀河惡魔城游戲_嗶哩嗶哩_bilibili Inventory.cs using Newtonsoft.Json.Linq; using System.Collections; us…

Alex教程每一P的教程原代碼加上我自己的理解初步理解寫的注釋,可供學(xué)習(xí)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 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的數(shù)組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);//防止多次創(chuàng)建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>();//拿到的方式有點(diǎn)繞,顯示拿到Canvas 里的 Inventory 然后通過GetComponentsInChildren拿到其下的使用UISlotstashItemSlot = stashSlotParent.GetComponentsInChildren<UI_itemSlot>();equipmentSlot = equipmentSlotParent.GetComponentsInChildren<UI_equipementSlots>();statSlot = statSlotParent.GetComponentsInChildren<UI_Statslot>();AddStartingItems();}private void AddStartingItems(){for (int i = 0; i < startingItem.Count; i++){AddItem(startingItem[i]);}}//設(shè)置初始物品public void EquipItem(ItemData _item){//解決在itemdata里拿不到子類equipment里的enum的問題ItemData_Equipment newEquipment = _item as ItemData_Equipment;//https://www.bilibili.com/read/cv15551811///將父類轉(zhuǎn)換為子類InventoryItem newItem = new InventoryItem(newEquipment);ItemData_Equipment oldEquipment = null;foreach (KeyValuePair<ItemData_Equipment, InventoryItem> item in equipmentDictionary)//這種方法可以同時(shí)拿到key和value保存到item里面{if (item.Key.equipmentType == newEquipment.equipmentType)//將拿到的key與轉(zhuǎn)換成itemdata_equipment類型的_item的type對(duì)比拿到存在的key{oldEquipment = item.Key;//此key需保存在外部的data類型里//equipment.Remove(item.Value);//equipmentDictionary.Remove(item.Key);}}//好像用foreach里的value和key無法對(duì)外部的list和字典進(jìn)行操作if (oldEquipment != null){AddItem(oldEquipment);Unequipment(oldEquipment);}equipment.Add(newItem);equipmentDictionary.Add(newEquipment, newItem);RemoveItem(_item);newEquipment.AddModifiers();UpdateSlotUI();}//裝備裝備的函數(shù)public void Unequipment(ItemData_Equipment itemToRemove)//裝備其他同類型的裝備時(shí)。去除已裝備的裝備{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++){//此步驟用于將對(duì)應(yīng)類型的武器插入對(duì)應(yīng)的槽內(nèi)foreach (KeyValuePair<ItemData_Equipment, InventoryItem> item in equipmentDictionary)//這種方法可以同時(shí)拿到key和value保存到item里面{if (item.Key.equipmentType == equipmentSlot[i].slotType){equipmentSlot[i].UpdateSlots(item.Value);}}}//解決出現(xiàn)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函數(shù)public void AddItem(ItemData _item){if (_item.itemType == ItemType.Equipment && CanAddItem())//修復(fù)Inventory數(shù)量大于Slot能存放的數(shù)量時(shí)報(bào)錯(cuò)的Bug{AddToInventory(_item);}else if (_item.itemType == ItemType.Material){AddToStash(_item);}UpdateSlotUI();}//添加物體的函數(shù)private void AddToStash(ItemData _item)//向stash加物體的函數(shù){if (stashDictionary.TryGetValue(_item, out InventoryItem value))//只有這種方法才能在查找到是否存在key對(duì)應(yīng)value是否存在的同時(shí),能夠同時(shí)拿到value,其他方法的拿不到value{value.AddStack();}//字典的使用,通過ItemData類型的數(shù)據(jù)找到InventoryItem里的與之對(duì)應(yīng)的同樣類型的數(shù)據(jù)else//初始時(shí)由于沒有相同類型的物體,故調(diào)用else是為了初始化庫存,使其中含有一個(gè)基本的值{InventoryItem newItem = new InventoryItem(_item);stash.Add(newItem);//填進(jìn)列表里只有一次stashDictionary.Add(_item, newItem);//同上}}private void AddToInventory(ItemData _item){if (inventoryDictionary.TryGetValue(_item, out InventoryItem value))//只有這種方法才能在查找到是否存在key對(duì)應(yīng)value是否存在的同時(shí),能夠同時(shí)拿到value,其他方法的拿不到value{value.AddStack();}//字典的使用,通過ItemData類型的數(shù)據(jù)找到InventoryItem里的與之對(duì)應(yīng)的同樣類型的數(shù)據(jù)else//初始時(shí)由于沒有相同類型的物體,故調(diào)用else是為了初始化庫存,使其中含有一個(gè)基本的值{InventoryItem newItem = new InventoryItem(_item);inventory.Add(newItem);//填進(jìn)列表里只有一次inventoryDictionary.Add(_item, newItem);//同上}}//將物體存入Inventory的函數(shù)public void RemoveItem(ItemData _item)//修復(fù)Inventory數(shù)量大于Slot能存放的數(shù)量時(shí)報(bào)錯(cuò)的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數(shù)量和Slot能存放的數(shù)量進(jìn)行對(duì)比,確定是否可以添加新的裝備到裝備槽{if(inventory.Count >= inventoryItemSlot.Length){Debug.Log("No more space");return false; }return true;}public List<InventoryItem> GetEquipmentList() => equipment;public List<InventoryItem> GetStashList() => stash;public ItemData_Equipment GetEquipment(EquipmentType _Type)//通過Type找到對(duì)應(yīng)的已裝備裝備的函數(shù){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()//使用藥瓶設(shè)置冷卻時(shí)間{ItemData_Equipment currentFlask = GetEquipment(EquipmentType.Flask);if (currentFlask == null)return;//使用藥瓶設(shè)置冷卻時(shí)間bool canUseFlask = Time.time > lastTimeUsedFlask + flaskCooldown;if(canUseFlask){flaskCooldown = currentFlask.itemCooldown;currentFlask.Effect(null);lastTimeUsedFlask = Time.time;}else{Debug.Log("Flask is Cooldown");}}//使用藥瓶函數(shù)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;//設(shè)置速度[SerializeField] private ItemData ItemData;[SerializeField] private Vector2 velocity;//設(shè)置速度private void SetupVisuals(){if (ItemData == null)return;GetComponent<SpriteRenderer>().sprite = ItemData.icon;gameObject.name = ItemData.name;}public void SetupItem(ItemData _itemData,Vector2 _velocity)設(shè)置實(shí)例函數(shù){ItemData = _itemData;rb.velocity = _velocity;//設(shè)置速度SetupVisuals();}public void PickupItem()//拾取函數(shù)打包{if(!Inventory.instance.CanAddItem()&&ItemData.itemType == ItemType.Equipment)//修復(fù)在Inventory滿時(shí)撿錢裝備并銷毀它的bug{rb.velocity = new Vector2(0, 7);return;}Inventory.instance.AddItem(ItemData);Destroy(gameObject);}
}

http://aloenet.com.cn/news/35920.html

相關(guān)文章:

  • 專為網(wǎng)站做點(diǎn)擊量網(wǎng)絡(luò)營銷師工作內(nèi)容
  • 簡述網(wǎng)站設(shè)計(jì)的原則在線外鏈工具
  • 設(shè)計(jì)參考圖網(wǎng)站龍華百度快速排名
  • wordpress固定鏈接設(shè)置后404seo推廣是什么意思呢
  • 目前網(wǎng)站開發(fā)語言深圳全網(wǎng)推廣
  • 個(gè)人網(wǎng)站不備案做經(jīng)營性質(zhì)網(wǎng)站百度廣告投放技巧
  • 網(wǎng)站域名被劫持怎么辦百度識(shí)圖搜索網(wǎng)頁版
  • 手機(jī)軟件下載大全seo優(yōu)化設(shè)計(jì)
  • 集團(tuán)網(wǎng)站開發(fā)公司百度app下載官方
  • 網(wǎng)站開發(fā)行業(yè)免費(fèi)的拓客平臺(tái)有哪些
  • 如何查一個(gè)網(wǎng)站的備案號(hào)網(wǎng)站權(quán)重
  • 桂林微信網(wǎng)站優(yōu)化師是做什么的
  • 廣州網(wǎng)站建設(shè)設(shè)計(jì)平臺(tái)制作網(wǎng)站要花多少錢
  • 旅游公司網(wǎng)站建設(shè)ppt深圳專業(yè)建站公司
  • 做ps的網(wǎng)站有哪些功能嗎蘭州seo公司
  • 網(wǎng)站開發(fā)所需經(jīng)費(fèi)上海疫情最新數(shù)據(jù)
  • 做動(dòng)態(tài)網(wǎng)站需要學(xué)什么最新的軍事新聞
  • 建設(shè)網(wǎng)站需要多少錢百度競價(jià)排名醫(yī)院事件
  • 成都網(wǎng)站建設(shè) 四川冠辰科技公司建站平臺(tái)如何隱藏技術(shù)支持
  • 統(tǒng)一企業(yè)信息管理系統(tǒng)網(wǎng)站seo博客網(wǎng)站
  • 煙臺(tái)網(wǎng)站建設(shè)優(yōu)化百度查詢關(guān)鍵詞排名工具
  • 廈門比較好的網(wǎng)站設(shè)計(jì)公司刷網(wǎng)站seo排名軟件
  • java ee只是做網(wǎng)站免費(fèi)網(wǎng)站注冊(cè)免費(fèi)創(chuàng)建網(wǎng)站
  • 沙井網(wǎng)站建設(shè)哈爾濱seo優(yōu)化
  • 融資網(wǎng)站建設(shè)方案競價(jià)網(wǎng)絡(luò)推廣托管
  • 可以做h5的網(wǎng)站百度指數(shù)特點(diǎn)
  • 男女做那個(gè)能看的視頻網(wǎng)站新產(chǎn)品宣傳推廣策劃方案
  • 網(wǎng)站建設(shè)賬戶搭建頁面關(guān)鍵詞優(yōu)化
  • 做網(wǎng)站開發(fā)錢百度指數(shù)有哪些功能
  • 泰安網(wǎng)站優(yōu)化濟(jì)南百度開戶電話