当前位置: 首页 > news >正文

学习游制作记录(背包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 = " ";
}
}

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脚本:

为所有物品槽创建一个父对象,并给它挂载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]);

        }
}

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();
}

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

http://www.lryc.cn/news/618825.html

相关文章:

  • Xshell远程连接Ubuntu 24.04.2 LTS虚拟机
  • 浪潮推出首个“人工智能工厂”,工业化模式加速技术落地
  • Java连接MySQL数据库
  • CANopen Magic调试软件使用
  • 文件io ,缓冲区
  • 堆排序以及实现
  • 基于多模态大模型的个性化学习路径生成系统研究
  • 【motion】身体动作与面部表情捕捉2:Motion-X++ 论文分析
  • 过程设计工具深度解析-软件工程之详细设计(补充篇)
  • MyBatis 缓存与 Spring 事务相关笔记
  • redis的过期策略和定时器
  • Cloud Computing(云计算)和Sky Computing(天空计算)
  • 地图可视化实践录:显示地理区域图
  • 深层神经网络
  • 测试匠谈 | AI语音合成之大模型性能优化实践
  • 【C#】用队列构建一个对象池管理对象的创建和释放
  • PySpark性能优化与多语言选型讨论
  • 各种 dp 刷题下
  • 人机交互:连接人类与数字世界的桥梁
  • apache+虚拟主机
  • 五、Elasticsearch在Linux的安装部署
  • Rust 项目编译故障排查:从 ‘onnxruntime‘ 链接失败到 ‘#![feature]‘ 工具链不兼容错误
  • 使用reqwest+select实现简单网页爬虫
  • Rust 性能提升“最后一公里”:详解 Profiling 瓶颈定位与优化|得物技术
  • open-webui源码分析1—文件上传
  • Vue接口平台十三——测试记录
  • springboot整合sharding-jdbc 5.5.2 做单库分表
  • 燕山大学计算机网络实验(2025最新)
  • Java调用Vue前端页面生成PDF文件
  • 深入剖析 React 合成事件:透过 onClick 看本质