前言
欢迎来到【制作100个Unity游戏】系列!本系列将引导您一步步学习如何使用Unity开发各种类型的游戏。在这第25篇中,我们将探索如何用unity制作一个3D背包、库存、制作、快捷栏、存储系统、砍伐树木获取资源、随机战利品宝箱等功能,我会附带项目源码,以便你更好理解它。
丢弃物品
修改Inventory,丢弃的位置
[Header("丢弃")] public Transform dropLocation;//丢弃物品的位置 public void Update() { //。。。 if (Input.GetKeyDown(KeyCode.G)) { dropItem();//丢弃物品 } } // 丢弃物品 private void dropItem() { // 循环遍历背包中的所有槽位 for (int i = 0; i < allInventorySlots.Count; i++) { // 获取当前遍历到的槽位 Slot curSlot = allInventorySlots[i]; // 如果当前槽位被鼠标悬停并且槽位中有物品,则执行丢弃操作 if (curSlot.hovered && curSlot.hasItem()) { // 设置物品的游戏对象为激活状态,使其在游戏中可见 curSlot.getItem().gameObject.SetActive(true); // 将物品移动到指定的放下位置 curSlot.getItem().transform.position = dropLocation.position; // 将槽位中的物品设置为null,表示该槽位现在为空 curSlot.setItem(null); // 物品已经放下,无需继续检查其他槽位,因此跳出循环 break; } } }
配置丢弃点
配置参数
效果
源码
源码不出意外的话我会放在最后一节