当前位置:   article > 正文

【制作100个unity游戏之23】实现类似七日杀、森林一样的生存游戏17(附项目源码)

【制作100个unity游戏之23】实现类似七日杀、森林一样的生存游戏17(附项目源码)

本节最终效果演示

在这里插入图片描述

系列目录

前言

欢迎来到【制作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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

记得同步修改其他调用该方法的地方,比如
在这里插入图片描述

修改Blueprint蓝图,添加制作数量

public int numOfItrmsToProduce; // 生成的数量

public Blueprint(string name, int producedItems, int reqNUM, string R1, int R1num, string R2, int R2num)
{
    numOfItrmsToProduce = producedItems;
    //。。。
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

修改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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137

配置参数
在这里插入图片描述
效果
在这里插入图片描述

源码

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

完结

赠人玫瑰,手有余香!如果文章内容对你有所帮助,请不要吝啬你的点赞评论和关注,以便我第一时间收到反馈,你的每一次支持都是我不断创作的最大动力。当然如果你发现了文章中存在错误或者有更好的解决方法,也欢迎评论私信告诉我哦!

好了,我是向宇https://xiangyu.blog.csdn.net

一位在小公司默默奋斗的开发者,出于兴趣爱好,最近开始自学unity,闲暇之余,边学习边记录分享,站在巨人的肩膀上,通过学习前辈们的经验总是会给我很多帮助和启发!php是工作,unity是生活!如果你遇到任何问题,也欢迎你评论私信找我, 虽然有些问题我也不一定会,但是我会查阅各方资料,争取给出最好的建议,希望可以帮助更多想学编程的人,共勉~

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/95213
推荐阅读
相关标签
  

闽ICP备14008679号