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

本文主要是介绍【制作100个unity游戏之25】3D背包、库存、制作、快捷栏、存储系统、砍伐树木获取资源、随机战利品宝箱1(附带项目源码),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

效果演示

在这里插入图片描述

文章目录

  • 效果演示
  • 系列目录
  • 前言
  • 人物和视角基本控制
  • 简单的背包系统和物品交互
    • 绘制背包UI
    • 脚本控制
  • 源码
  • 完结

系列目录

前言

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

人物和视角基本控制

具体可以看我这篇文章:
【unity小技巧】unity最完美的CharacterController 3d角色控制器,实现移动、跳跃、下蹲、奔跑、上下坡、物理碰撞效果,复制粘贴即用

这里我就直接贴出代码了

人物移动控制

[RequireComponent(typeof(CharacterController))]
public class MovementScript : MonoBehaviour
{[Tooltip("角色控制器")] public CharacterController characterController;[Tooltip("重力加速度")] private float Gravity = -19.8f;private float horizontal;private float vertical;[Header("移动")][Tooltip("角色行走的速度")] public float walkSpeed = 6f;[Tooltip("角色奔跑的速度")] public float runSpeed = 9f;[Tooltip("角色移动的方向")] private Vector3 moveDirection;[Tooltip("当前速度")] private float speed;[Tooltip("是否奔跑")] private bool isRun;[Header("地面检测")][Tooltip("是否在地面")] private bool isGround;[Header("跳跃")][Tooltip("角色跳跃的高度")] public float jumpHeight = 8f;private float _verticalVelocity;void Start(){speed = walkSpeed;}void Update(){horizontal = Input.GetAxis("Horizontal");vertical = Input.GetAxis("Vertical");//地面检测isGround = characterController.isGrounded;SetSpeed();SetRun();SetMove();SetJump();}//速度设置void SetSpeed(){if (isRun){speed = runSpeed;}else{speed = walkSpeed;}}//控制奔跑void SetRun(){if (Input.GetKey(KeyCode.LeftShift)){isRun = true;}else{isRun = false;}}//控制移动void SetMove(){moveDirection = transform.right * horizontal + transform.forward * vertical; // 计算移动方向moveDirection = moveDirection.normalized; // 归一化移动方向,避免斜向移动速度过快  }//控制跳跃void SetJump(){bool jump = Input.GetButtonDown("Jump");if (isGround){// 在着地时阻止垂直速度无限下降if (_verticalVelocity < 0.0f){_verticalVelocity = -2f;}if (jump){_verticalVelocity = jumpHeight;}}else{//随时间施加重力_verticalVelocity += Gravity * Time.deltaTime;}characterController.Move(moveDirection * speed * Time.deltaTime + new Vector3(0.0f, _verticalVelocity, 0.0f) * Time.deltaTime);}
}

视角控制

public class MouseLook : MonoBehaviour
{// 鼠标灵敏度public float mouseSensitivity = 500f;// 玩家的身体Transform组件,用于旋转public Transform playerBody;// x轴的旋转角度float xRotation = 0f;void Start(){// 锁定光标到屏幕中心,并隐藏光标Cursor.lockState = CursorLockMode.Locked;}// Update在每一帧调用void Update(){// 执行自由视角查看功能FreeLook();}// 自由视角查看功能的实现void FreeLook(){// 获取鼠标X轴和Y轴的移动量,乘以灵敏度和时间,得到平滑的移动速率float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;//限制旋转角度在-90到90度之间,防止过度翻转xRotation = Mathf.Clamp(xRotation, -90f, 90f);// 累计x轴上的旋转量xRotation -= mouseY;// 应用摄像头的x轴旋转transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);// 应用玩家身体的y轴旋转playerBody.Rotate(Vector3.up * mouseX);}
}

效果
在这里插入图片描述

简单的背包系统和物品交互

对UI知识还不太懂的小伙伴可以看这篇基础篇文件:【Unity游戏开发教程】零基础带你从小白到超神30——UI组件和布局的使用

绘制背包UI

物品插槽背景框
在这里插入图片描述
物品插槽,可以把物品插槽做出预制体,后面好修改
在这里插入图片描述

准星图像和文本
在这里插入图片描述

脚本控制

物品信息脚本

public class Item : MonoBehaviour
{public new string name = "New Item";//物品名称[TextArea]public string description = "New Description";//物品描述public Sprite icon;//物品图标public int currentQuantity = 1;//物品当前数量public int maxQuantity = 16;//物品最大堆叠数量
}

背包插槽脚本

public class Slot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{public bool hovered; // 鼠标是否悬停在该槽位上的标志private Item heldItem; // 当前槽位持有的物品private Color opaque = new Color(1, 1, 1, 1); // 不透明颜色private Color transparent = new Color(1, 1, 1, 0); // 透明颜色private Image thisSlotImage; // 该槽位的图像组件public TMP_Text thisSlotQuantityText; // 用于显示物品数量的文本组件// 初始化槽位public void initialiseSlot(){thisSlotImage = gameObject.GetComponent<Image>();thisSlotQuantityText = transform.GetChild(0).GetComponent<TMP_Text>();thisSlotImage.sprite = null;thisSlotImage.color = transparent;setItem(null);}// 设置槽位中的物品public void setItem(Item item){heldItem = item;if (item != null){thisSlotImage.sprite = heldItem.icon;thisSlotImage.color = opaque;updateData();}else {thisSlotImage.sprite = null;thisSlotImage.color = transparent;updateData();}}// 获取当前槽位持有的物品public Item getItem(){return heldItem;}// 当前槽位是否持有的物品public bool hasItem(){return heldItem ? true : false;}// 更新槽位显示的数据public void updateData(){if (heldItem != null) // 如果持有物品thisSlotQuantityText.text = heldItem.currentQuantity.ToString(); // 显示物品的数量else // 如果不持有物品thisSlotQuantityText.text = "";}// 当鼠标指针进入槽位区域时调用public void OnPointerEnter(PointerEventData pointerEventData){hovered = true;}// 当鼠标指针离开槽位区域时调用public void OnPointerExit(PointerEventData pointerEventData){hovered = false;}
}

库存系统脚本

public class Inventory : MonoBehaviour
{[Header("UI")]public GameObject inventory; // 游戏中的背包界面public List<Slot> allInventorySlots = new List<Slot>(); // 所有的槽位列表public List<Slot> inventorySloats = new List<Slot>();//背包的的槽位列表public Image crosshair; // 准星图像public TMP_Text itemHoverText; // 当中心悬停在物品上时显示物品名称的文本[Header("射线检测")]public float raycastDistance = 5f; // 射线检测的距离public LayerMask itemLayer; // 射线检测的目标层,用于识别物品public void Start(){toggleInventory(false); // 初始时关闭背包界面//合并槽位allInventorySlots.AddRange(inventorySloats);foreach (Slot uiSlot in allInventorySlots) // 初始化所有槽位{uiSlot.initialiseSlot();}}public void Update(){itemRaycast(Input.GetKeyDown(KeyCode.E)); // 显示物品名称和按E拾取物品if (Input.GetKeyDown(KeyCode.Tab)) // 按下tab键切换背包界面的显示状态toggleInventory(!inventory.activeInHierarchy);}private void itemRaycast(bool hasClicked = false){itemHoverText.text = ""; // 默认不显示任何物品名称Ray ray = Camera.main.ScreenPointToRay(crosshair.transform.position); // 从准星位置发出射线RaycastHit hit;if (Physics.Raycast(ray, out hit, raycastDistance, itemLayer)) // 如果射线检测到物品层的对象{if (hit.collider != null){if (hasClicked) // 如果是按了操作,尝试捡起物品{Item newItem = hit.collider.GetComponent<Item>();if (newItem){addItemToInventory(newItem); // 将物品添加到背包中}}else // 否则,仅获取物品名称以显示{Item newItem = hit.collider.GetComponent<Item>();if (newItem){itemHoverText.text = newItem.name; // 显示物品名称}}}}}//将物品添加到背包中private void addItemToInventory(Item itemToAdd){int leftoverQuantity = itemToAdd.currentQuantity; // 剩余需要添加到背包的物品数量Slot openSlot = null; // 记录一个空的槽位for (int i = 0; i < allInventorySlots.Count; i++) // 遍历所有槽位{Item heldItem = allInventorySlots[i].getItem();if (heldItem != null && itemToAdd.name == heldItem.name) // 如果槽位中有相同名称的物品{int freeSpaceInSlot = heldItem.maxQuantity - heldItem.currentQuantity; // 计算槽位中的剩余空间if (freeSpaceInSlot >= leftoverQuantity) // 如果剩余空间足够{heldItem.currentQuantity += leftoverQuantity; // 添加物品到该槽位Destroy(itemToAdd.gameObject); // 销毁场景中的物品对象allInventorySlots[i].updateData(); // 更新槽位显示的数据return;}else // 如果剩余空间不足{heldItem.currentQuantity = heldItem.maxQuantity; // 填满当前槽位leftoverQuantity -= freeSpaceInSlot; // 更新剩余需要添加的物品数量}}else if (heldItem == null) // 如果槽位为空{if (!openSlot)openSlot = allInventorySlots[i]; // 记录第一个空槽位}allInventorySlots[i].updateData(); // 更新槽位显示的数据}if (leftoverQuantity > 0 && openSlot) // 如果还有剩余物品且找到了空槽位{openSlot.setItem(itemToAdd); // 将物品添加到空槽位itemToAdd.currentQuantity = leftoverQuantity; // 更新物品的数量itemToAdd.gameObject.SetActive(false); // 隐藏场景中的物品对象}else{itemToAdd.currentQuantity = leftoverQuantity; // 更新物品的数量}}private void toggleInventory(bool enable){//关闭背包时,关闭所有鼠标悬停在该槽位上的标志if (!enable){foreach (Slot curSlot in allInventorySlots){curSlot.hovered = false;}}inventory.SetActive(enable); // 根据参数显示或隐藏背包界面Cursor.lockState = enable ? CursorLockMode.None : CursorLockMode.Locked; // 根据背包界面的状态锁定或解锁鼠标指针Cursor.visible = enable; // 设置鼠标指针的可见性// 禁用或启用相机的旋转控制Camera.main.GetComponent<MouseLook>().enabled = !enable;}
}

物品挂载Item脚本,配置参数,记得添加碰撞体并修改图层为Item
在这里插入图片描述
背包插槽挂载Slot脚本
在这里插入图片描述
角色上挂载Inventory脚本
在这里插入图片描述

拾取
在这里插入图片描述

源码

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

完结

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

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

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

在这里插入图片描述

这篇关于【制作100个unity游戏之25】3D背包、库存、制作、快捷栏、存储系统、砍伐树木获取资源、随机战利品宝箱1(附带项目源码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/702800

相关文章

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

C#和Unity中的中介者模式使用方式

《C#和Unity中的中介者模式使用方式》中介者模式通过中介者封装对象交互,降低耦合度,集中控制逻辑,适用于复杂系统组件交互场景,C#中可用事件、委托或MediatR实现,提升可维护性与灵活性... 目录C#中的中介者模式详解一、中介者模式的基本概念1. 定义2. 组成要素3. 模式结构二、中介者模式的特点

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结