学习游制作记录(背包UI以及各种物品的存储)8.12

1.创建物品槽

ItemObject脚本:

private void OnValidate()//这个函数可以在非运行状态更改对象的属性
{
GetComponent<SpriteRenderer>().sprite=itemData.icon;//直接可以看到物品的图标

gameObject.name ="Item Object -"+itemData.ItemName;//修改名字
}

创建一个画布,在画布下再建一个图像UI,在图像UI下创建一个文本UI,设置文本在图像的右下角,且偏右显示

创建UI_ItemSlot脚本挂载在图像UI上:

**[SerializeField] private Image image;//图像和文本

SerializeField\] private TextMeshProUGUI textMeshPro;** **public InventoryItem Item;//物品数据 void Start() { if(Item != null)//初始化图像和数量 { image.sprite = Item.data.icon; }** **if(Item.stackSize\>1) { textMeshPro.text =Item.stackSize.ToString(); }** **else { textMeshPro.text = " "; } }** ![](https://i-blog.csdnimg.cn/direct/437a889562e641e394e18d262193b892.png) ## 2.实现背包里存放物品并排序显示 ### UI_ItemSlot脚本: **public void UpdataSlot(InventoryItem _newItem)//传入物品类 { Item = _newItem;** **image.color = Color.white;//没有物品时槽是透明的** **if (Item != null) { image.sprite = Item.data.icon; }** **if (Item.stackSize \> 1) { textMeshPro.text = Item.stackSize.ToString(); }** **else { textMeshPro.text = " "; } }** ### Inventory脚本: ![](https://i-blog.csdnimg.cn/direct/09b7e033e24340dfacef8765fe64622e.png) 为所有物品槽创建一个父对象,并给它挂载Gird layout Group组件,它可以自然分配子物体的位置 **\[Header("Inventory UI")

SerializeField\] private Transform InventorySlotParent;//槽位的父对象 private UI_ItemSlot\[\] itemSlot;//物品槽的数组** **private void UpdataSlotUI()//在添加和减少的函数里调用 { for (int i = 0; i \< inventoryItems.Count; i++)//检查列表所有物品类 {** **itemSlot\[i\].UpdataSlot(inventoryItems\[i\]);** **} }** ![](https://i-blog.csdnimg.cn/direct/4be18f72d4ca4519a5a605b9f4de733d.png) ## 3.实现装备类的物品数据 ### ItemData脚本: **public enum ItemType//第一个分类------材料与装备 { Material, Equipment }** ### 创建ItemData_Equipment脚本: **public enum EquiomentType //装备的分类------武器,护甲,饰品和药水 { Weapon, Armor, Amulet, Flask }** **\[CreateAssetMenu(fileName = "New Item Data", menuName = "Data/Equipment")

public class ItemData_Equipment : ItemData
{
public EquiomentType equipmentType;
}**

利用上述脚本创建几个物品

4.实现分类存储

将装备和材料分开存储

Inventory脚本:

[SerializeField] public List<InventoryItem> stash;//仿照之前的方法重写一遍即可
public Dictionary<ItemData,InventoryItem> stashItemsDictionary;

[SerializeField] private Transform stashSlotParent;
private UI_ItemSlot[] stashSlot;

private void UpdataSlotUI()
{
for (int i = 0; i < inventory.Count; i++)
{

itemSlot[i].UpdataSlot(inventory[i]);

}

for(int i = 0;i< stash.Count;i++)//更新材料的存储
{
stashSlot[i].UpdataSlot(stash[i]);
}
}

public void AddItem(ItemData _item)
{
if (_item.itemType == ItemType.Equipment)
{
AddToInventory(_item);

}
else if( _item.itemType == ItemType.Material)
{
AddToStash(_item);//添加方法与之前类似,这里不作赘述
}

UpdataSlotUI();
}

public void RemoveItem(ItemData _item)//移除同理
{
if (_item.itemType == ItemType.Equipment)
{
RemoveInventory(_item);

}

else if( _item.itemType == ItemType.Material)
{
RemoveStash(_item);
}

UpdataSlotUI();
}

只需要再创建一个槽位的父对象即可

相关推荐
一定要AK17 小时前
刷题时的学习笔记
c++·笔记·学习
xxxibolva19 小时前
SQL 学习
数据库·sql·学习
Rabbit_QL19 小时前
【CI/CD】01_为什么手动部署是个危险游戏
游戏·ci/cd
ZC跨境爬虫21 小时前
使用Claude Code开发校园交友平台前端UI全记录(含架构、坑点、登录逻辑及算法)
前端·ui·架构
星辰即远方21 小时前
OC学习Foudation框架
学习·ios·objective-c
weixin_408099671 天前
图片去水印 API 接口实战:网站如何实现自动去水印(Python / PHP / C#)
图像处理·人工智能·python·c#·php·api·图片去水印
yyk的萌1 天前
AI 应用开发工程师基础学习计划
开发语言·python·学习·ai·lua
龘龍龙1 天前
大模型学习(三)-RAG、LangChain
学习·langchain
计算机安禾1 天前
【数据结构与算法】第22篇:线索二叉树(Threaded Binary Tree)
c语言·开发语言·数据结构·学习·算法·链表·visual studio code
:mnong1 天前
Superpowers 项目设计分析
java·c语言·c++·python·c#·php·skills