【制作100个unity游戏之25】3D背包、库存、制作、快捷栏、存储系统、砍伐树木获取资源、随机战利品宝箱5(附带项目源码)

简介: 【制作100个unity游戏之25】3D背包、库存、制作、快捷栏、存储系统、砍伐树木获取资源、随机战利品宝箱5(附带项目源码)

前言

欢迎来到【制作100个Unity游戏】系列!本系列将引导您一步步学习如何使用Unity开发各种类型的游戏。在这第25篇中,我们将探索如何用unity制作一个3D背包、库存、制作、快捷栏、存储系统、砍伐树木获取资源、随机战利品宝箱等功能,我会附带项目源码,以便你更好理解它。

制作系统

定义制作配方

// 制作配方
[CreateAssetMenu(fileName = "NewRecipe", menuName = "Inventory/Recipe")]
public class Recipe : ScriptableObject
{
    // 生成的物品预制体
    public GameObject createdItemPrefab;

    // 生产数量
    public int quantityProduced = 1;

    // 所需的原料列表
    public List<RequiredIngredient> requiredIngredients = new List<RequiredIngredient>();

    // 所需的原料
    [System.Serializable]
    public class RequiredIngredient
    {
        // 原料的名称
        public string itemName;

        // 所需的数量
        public int requiredQuantity;
    }
}

简单配置一个制作配方

新增Crafting,实现制作功能,注意修改Inventory为单例

using System.Collections.Generic;
using UnityEngine;

public class Crafting : MonoBehaviour
{
    public List<Slot> allInventorySlots;
    [Header("制作")]
    public List<Recipe> itemRecipes = new List<Recipe>();//制作配方列表

    private void Start() {
        allInventorySlots = Inventory.Instance.allInventorySlots;
    }

    //合成制作物品
    public void craftItem(string itemName)
    {
        // 遍历所有配方
        foreach (Recipe recipe in itemRecipes)
        {
            // 找到与指定物品名称匹配的配方
            if (recipe.createdItemPrefab.GetComponent<Item>().name == itemName)
            {
                bool haveAllIngredients = true;//是否有足够的原料

                for (int i = 0; i < recipe.requiredIngredients.Count; i++)
                {
                    if (!haveIngredient(recipe.requiredIngredients[i].itemName, recipe.requiredIngredients[i].requiredQuantity))
                    {
                        Debug.Log(recipe.requiredIngredients[i].itemName + "不够,制作失败");
                        haveAllIngredients = false;
                        break;
                    }
                }

                if (haveAllIngredients)
                {
                    // 移除所需的原料
                    for (int i = 0; i < recipe.requiredIngredients.Count; i++)
                    {
                        removeIngredient(recipe.requiredIngredients[i].itemName, recipe.requiredIngredients[i].requiredQuantity);
                    }

                    // 创建物品
                    GameObject craftedItem = Instantiate(recipe.createdItemPrefab, Inventory.Instance.dropLocation.position, Quaternion.identity);
                    craftedItem.GetComponent<Item>().currentQuantity = recipe.quantityProduced;

                    // 将物品添加到库存中
                    Inventory.Instance.addItemToInventory(craftedItem.GetComponent<Item>());
                    break;
                }
            }
        }
    }

    //从库存中移除指定数量的原料
    private void removeIngredient(string itemName, int quantity)
    {
        // 如果没有足够的原料,则直接返回
        if (!haveIngredient(itemName, quantity))
        {
            Debug.Log("没有足够的原料");
            return;
        }

        int remainingQuantity = quantity;

        // 遍历所有库存槽位
        foreach (Slot curSlot in allInventorySlots)
        {
            Item item = curSlot.getItem();

            // 找到匹配的原料
            if (item != null && item.name == itemName)
            {
                // 如果库存中的数量大于所需数量,则减去所需数量
                if (item.currentQuantity >= remainingQuantity)
                {
                    item.currentQuantity -= remainingQuantity;
                    // 如果库存中的数量等于所需数量,则将槽位置空
                    if (item.currentQuantity == 0)
                    {
                        curSlot.setItem(null);
                    }
                    return;
                }
                else
                {
                    Debug.Log("没有足够的原料");
                    // 更新剩余数量
                    // remainingQuantity -= item.currentQuantity;
                    // curSlot.setItem(null);
                }
            }
        }
    }

    //检查库存中是否有足够的指定原料
    private bool haveIngredient(string itemName, int quantity)
    {
        int foundQuantity = 0;

        // 遍历所有库存槽位
        foreach (Slot curSlot in allInventorySlots)
        {
            // 如果槽位存在物品且名称匹配
            if (curSlot.hasItem() && curSlot.getItem().name == itemName)
            {
                foundQuantity += curSlot.getItem().currentQuantity;
                // 如果找到的数量大于所需数量,则返回true,否则返回false
                if (foundQuantity >= quantity)
                {
                    return true;
                }
            }
        }
        return false;
    }
}

绑定配方参数

绘制简单的制作界面,并并绑定点击制作事件

效果,制作出一个石斧

源码

源码不出意外的话我会放在最后一节

目录
相关文章
|
4月前
|
人工智能 IDE API
Qoder CN支持哪些大模型?Qwen、GLM、DeepSeek、Kimi都能切换吗?
Qoder CN(原灵码)是阿里云推出的AI智能体产品,支持Qwen、GLM、DeepSeek、Kimi、MiniMax等主流国产大模型,可在IDE内一键切换。提供免费社区版及多档付费版本,含Credits配额与BYOK自定义接入能力。阿里云Qoder CN官网:https://t.aliyun.com/U/fEiOLV
|
9月前
|
人工智能 IDE 开发工具
Visual Studio 2026 Enterprise 18.4.0 - 适用于 Windows 上 .NET 和 C++ 开发人员的最全面 IDE
Visual Studio 2026 Enterprise 18.4.0 - 适用于 Windows 上 .NET 和 C++ 开发人员的最全面 IDE
303 1
Visual Studio 2026 Enterprise 18.4.0 - 适用于 Windows 上 .NET 和 C++ 开发人员的最全面 IDE
|
JSON 供应链 数据挖掘
1688买家/卖家店铺订单API说明
1688订单API是阿里巴巴B2B平台的核心接口,支持订单全生命周期管理。采用RESTful架构,返回JSON数据,可查询订单状态、商品及物流等50+字段,适用于电商整合与数据分析。支持分页、多条件筛选与状态更新,助力自动化运营。
1436 1
|
Rust 图形学
【unity实战】使用unity制作一个类似Rust的3D生存建造建筑系统,具有很好的吸附性(附项目源码)
【unity实战】使用unity制作一个类似Rust的3D生存建造建筑系统,具有很好的吸附性(附项目源码)
1216 1
|
图形学
【制作100个unity游戏之28】花半天时间用unity复刻童年4399经典小游戏《黄金矿工》(附带项目源码)
【制作100个unity游戏之28】花半天时间用unity复刻童年4399经典小游戏《黄金矿工》(附带项目源码)
1547 0
|
图形学
【unity小技巧】实现FPS武器的瞄准放大效果(UGUI实现反向遮罩,全屏遮挡,局部镂空效果)
【unity小技巧】实现FPS武器的瞄准放大效果(UGUI实现反向遮罩,全屏遮挡,局部镂空效果)
983 1
|
图形学 容器
【unity实战】Unity中基于瓦片的网格库存系统——类似《逃离塔科夫》的库存系统(上)
【unity实战】Unity中基于瓦片的网格库存系统——类似《逃离塔科夫》的库存系统
1288 1
|
图形学
【unity小技巧】实现投掷物品,比如长矛,刀具,手榴弹和其他物体
【unity小技巧】实现投掷物品,比如长矛,刀具,手榴弹和其他物体
744 1
|
C++
3D模型在线转换工具
NSDT 3Dconvert是一个可以进行3D模型格式转换的在线工具,并支持GLTF、GLB、OBJ、STL、STP、IGS、FBX、IFC、DXF等3D模型在线预览和转换成果下载。
5236 1
3D模型在线转换工具
|
图形学
【制作100个unity游戏之23】实现类似七日杀、森林一样的生存游戏16(附项目源码)
【制作100个unity游戏之23】实现类似七日杀、森林一样的生存游戏16(附项目源码)
370 0