godot4制作2D游戏16———资源库(Part 1)
创建Items文件夹
创建Items文件夹用以保存相应的图片和脚本
- 创建脚本item_data
- 创建三个ItemData资源
在pause下创建inventory文件夹并创建库存样式
- 库存为一个按钮,里面是texture和label
- 如果需要键盘控制可以将他们都放在一个control里面
- 创建inventory_ui.gd文件
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
29class_name InventoryUI extends Control
const INVENTORY_SLOT = preload("res://GUI/pause_menu/inventory/inventory_slot.tscn")
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
PauseMenu.Shown.connect(update_inventory)
PauseMenu.Hidden.connect(clear_inventory)
clear_inventory()
pass # Replace with function body.
func update_inventory() -> void:
for s in data.slots:
var new_slot = INVENTORY_SLOT.instantiate()
add_child(new_slot)
new_slot.slot_data = s
get_child(0).grab_focus()
pass
func clear_inventory() -> void:
for c in get_children():
c.queue_free()
pass
本次ui结构
- 有一个PanelContainer,下面还有个GridContainer
- GridContainer就是inventory_ui,它包含一个InventoryData
- InventoryData里面有个SlotData数组
- SlotData里面有item和数量
- item里面有各种物品详细信息
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Have a nice day!!