本节最终效果演示

 
文章目录
系列目录
前言
欢迎来到【制作100个Unity游戏】系列!本系列将引导您一步步学习如何使用Unity开发各种类型的游戏。在这第23篇中,我们将探索如何制作一个类似于七日杀和森林的生存游戏。
本篇内容会比较多,我会分几篇来实现,感兴趣的可以关注一下,以免错过内容更新。
本节主要实现新的制作功能。
制作木板
UI直接复制和工具一样的即可

检查背包是否有指定数量的空插槽
修改InventorySystem的CheckIfFull方法,改名为CheckSlotsAvailable,因为我们需要判断背包是否有指定数量的空位
//判断背包是否有指定数量的空位
public bool CheckSlotsAvailable(int emptyMeeded)
{
    int emptySlot = 0;
    // 遍历slotList列表中的每个槽位对象
    foreach (GameObject slot in slotList)
    {
        // 如果槽位对象没有子对象,及是空位
        if (slot.transform.childCount <= 0)
        {
            emptySlot += 1;
        }
    }
    // 如果空位大于等于emptyMeeded,返回true,否则返回false
    if (emptySlot >= emptyMeeded)
    {
        return true;
    }
    else
    {
        return false;
    }
}
记得同步修改其他调用该方法的地方,比如
 
修改Blueprint蓝图,添加制作数量
public int numOfItrmsToProduce; // 生成的数量
public Blueprint(string name, int producedItems, int reqNUM, string R1, int R1num, string R2, int R2num)
{
    numOfItrmsToProduce = producedItems;
    //。。。
}
修改CraftingSystem
[Header("材料")]
public GameObject materialsScreenUl;
Button materialsBTN;
Button craftPlankBTN;// 制作木板按钮
TextMeshProUGUI LogReq1;// 需求材料文本
public Blueprint PlankBLP = new Blueprint("木板", 2, 1, "圆木", 1, "", 0); //木板制作蓝图
void Start()
{
    //。。。
    /// <summary>
    /// 材料
    /// </summary>
    materialsBTN = craftingScreenUl.transform.Find("背景").Find("材料").GetComponent<Button>();
    materialsBTN.onClick.AddListener(delegate { OpenMaterialsCategory(); });
    // 木板
    LogReq1 = materialsScreenUl.transform.Find("背景").Find("内容").Find("木板").Find("材料1").GetComponent<TextMeshProUGUI>();
    craftPlankBTN = materialsScreenUl.transform.Find("背景").Find("内容").Find("木板").Find("制作按钮").GetComponent<Button>();
    craftPlankBTN.onClick.AddListener(delegate { CraftAnyltem(PlankBLP); });
    RefreshNeededItems();
}
//制作事件
void CraftAnyltem(Blueprint blueprintToCraft)
{
    //判断背包是否够位置
    if(!InventorySystem.Instance.CheckSlotsAvailable(blueprintToCraft.numOfItrmsToProduce)){
        //TODO报错提示,错误提示音
        Debug.Log("库存不足");
        return;
    }
    AudioManager.Instance.PlaySFX("制作");
    for (int i = 0; i < blueprintToCraft.numOfItrmsToProduce; i++)
    {
        InventorySystem.Instance.AddToInventory(blueprintToCraft.itemName);
    }
    //按需求的总数,删除对应的物品
    if (blueprintToCraft.numOfRequirements == 1)
    {
        InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
    }
    else if (blueprintToCraft.numOfRequirements == 2)
    {
        InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
        InventorySystem.Instance.RemoveItem(blueprintToCraft.Req2, blueprintToCraft.Req2amount);
    }
}
//刷新需要的物品
public void RefreshNeededItems()
{
    int stone_count = 0; // 石头数量
    int stick_count = 0; // 树枝数量
    int log_count = 0; // 圆木数量
    inventoryltemList = InventorySystem.Instance.itemList; // 获取物品清单
    foreach (string itemName in inventoryltemList) // 遍历物品清单
    {
        switch (itemName)
        {
            case "小石头":
                stone_count += 1;
                break;
            case "树枝":
                stick_count += 1;
                break;
            case "圆木":
                log_count += 1;
                break;
        }
    }
    // 更新需求文本
    AxeReq1.text = "2 石头 [" + stone_count + "]"; // 显示需要的石头数量
    AxeReq2.text = "2 树枝 [" + stick_count + "]"; // 显示需要的树枝数量
    LogReq1.text = "1 圆木 [" + log_count + "]";
    //斧头制作按钮
    if (stone_count >= 2 && stick_count >= 2)
    {
        craftAxeBTN.gameObject.SetActive(true);
    }
    else
    {
        craftAxeBTN.gameObject.SetActive(false);
    }
    //木板制作按钮
    if (log_count >= 1)
    {
        craftPlankBTN.gameObject.SetActive(true);
    }
    else
    {
        craftPlankBTN.gameObject.SetActive(false);
    }
}
void OpenToolsCategory()
{
    craftingScreenUl.SetActive(false); // 关闭制作界面
    toolsScreenUl.SetActive(true); // 打开工具界面   
    materialsScreenUl.SetActive(false); // 关闭材料界面   
}
void OpenMaterialsCategory()
{
    craftingScreenUl.SetActive(false);
    toolsScreenUl.SetActive(false);
    materialsScreenUl.SetActive(true);
}
void Update()
{
    if (Input.GetKeyDown(KeyCode.I))
    {
        // 打开关闭制作界面
        craftingScreenUl.SetActive(!isOpen);
        if (isOpen)
        {
            toolsScreenUl.SetActive(false); // 关闭工具界面
            materialsScreenUl.SetActive(false); // 关闭材料界面
        }
        isOpen = !isOpen;
        // 设置鼠标锁定模式为无锁定,允许鼠标在界面上移动
        Cursor.lockState = (isOpen || InventorySystem.Instance.isOpen) ? CursorLockMode.None : CursorLockMode.Locked;
    }
}
配置参数
 
 效果
 
源码
源码不出意外的话我会放在最后一节
完结
赠人玫瑰,手有余香!如果文章内容对你有所帮助,请不要吝啬你的点赞评论和关注,以便我第一时间收到反馈,你的每一次支持都是我不断创作的最大动力。当然如果你发现了文章中存在错误或者有更好的解决方法,也欢迎评论私信告诉我哦!
好了,我是向宇,https://xiangyu.blog.csdn.net
一位在小公司默默奋斗的开发者,出于兴趣爱好,最近开始自学unity,闲暇之余,边学习边记录分享,站在巨人的肩膀上,通过学习前辈们的经验总是会给我很多帮助和启发!php是工作,unity是生活!如果你遇到任何问题,也欢迎你评论私信找我, 虽然有些问题我也不一定会,但是我会查阅各方资料,争取给出最好的建议,希望可以帮助更多想学编程的人,共勉~











