You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Godot 4.2中如何为ItemList项设置普通与按下状态纹理?

在Godot 4.2中为ItemList项实现带状态的纹理背景

针对你的需求,有两种可行方案,根据你的场景选择即可:

方案一:自定义ItemList的项绘制(保留ItemList组件)

ItemList本身没有直接设置项背景纹理的API,需要通过自定义绘制实现普通/按下状态的纹理切换,步骤如下:

  1. 准备资源:提前加载普通状态和按下状态的纹理
  2. 跟踪按下的项索引:通过input_event信号记录当前被按下的项
  3. 自定义绘制项:通过item_custom_draw信号绘制纹理和文本

示例代码

@onready var item_list: ItemList = $ItemList
# 替换为你的纹理路径
var normal_texture: Texture2D = load("res://Resource/Button_normal.png")
var pressed_texture: Texture2D = load("res://Resource/Button_pressed.png")
var pressed_item_idx: int = -1

func _ready():
    # 添加测试项
    item_list.add_item("选项1")
    item_list.add_item("选项2")
    item_list.add_item("选项3")
    
    # 连接必要信号
    item_list.connect("item_custom_draw", self, "_on_item_custom_draw")
    item_list.connect("input_event", self, "_on_input_event")
    
    # 设置项高度匹配纹理尺寸(按需调整)
    item_list.fixed_item_height = normal_texture.get_height()

# 跟踪鼠标按下的项
func _on_input_event(index: int, event: InputEvent, shape_idx: int) -> void:
    if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
        pressed_item_idx = index if event.pressed else -1
        item_list.queue_redraw() # 触发重绘更新状态

# 自定义绘制每个项
func _on_item_custom_draw(index: int, rect: Rect2, hover: bool, selected: bool) -> void:
    # 选择当前状态的纹理
    var draw_texture = pressed_texture if index == pressed_item_idx else normal_texture
    # 绘制纹理填充整个项区域
    draw_texture_rect(draw_texture, rect, false)
    # 手动绘制项文本(自定义绘制会覆盖默认文本)
    var item_text = item_list.get_item_text(index)
    var text_font = get_font("font")
    var text_size = text_font.get_string_size(item_text)
    # 文本居中显示
    var text_pos = rect.position + (rect.size - text_size) / 2
    draw_string(text_font, text_pos, item_text)

方案二:用VBoxContainer + TextureButton替代ItemList(更适合问答游戏)

如果你的问答游戏选项数量不多,直接用VBoxContainer搭配TextureButton会更简单——TextureButton原生支持普通/按下状态的纹理,无需自定义绘制:

示例代码

@onready var option_container: VBoxContainer = $VBoxContainer
# 替换为你的纹理路径
var normal_texture: Texture2D = load("res://Resource/Button_normal.png")
var pressed_texture: Texture2D = load("res://Resource/Button_pressed.png")

func _ready():
    # 添加问答选项
    add_option("选项A:这是正确答案")
    add_option("选项B:错误选项")
    add_option("选项C:错误选项")

# 创建带纹理的选项按钮
func add_option(option_text: String):
    var option_btn = TextureButton.new()
    option_btn.text = option_text
    option_btn.texture_normal = normal_texture
    option_btn.texture_pressed = pressed_texture
    # 设置纹理拉伸模式(按需选择)
    option_btn.stretch_mode = TextureButton.STRETCH_SCALE
    # 绑定点击事件
    option_btn.connect("pressed", self, "_on_option_selected", [option_text])
    option_container.add_child(option_btn)

# 处理选项选中逻辑
func _on_option_selected(selected_text: String):
    print("选中的选项:", selected_text)
    # 这里添加你的问答逻辑,比如判断答案是否正确

方案对比

  • 方案一适合需要ItemList特性(如滚动、批量数据管理)的场景,需自定义绘制逻辑
  • 方案二更直观,TextureButton自带状态管理,适合问答游戏这种选项交互为主的场景

内容的提问来源于stack exchange,提问作者user24460252

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 07:45:02