初面网初面网

对话系统

(待补充图片,完善逻辑)

步骤一 创建资源脚本

extends Resource
class_name Dialogue

# 设置信息的属性
@export_category("Information")
@export var texture: Texture2D
@export var name: String
@export var dialogue: String

# 链接到对话文件
@export_category("Linking Nodes")

# 分支路径源
@export var path_option: String

# 分支选项
@export var options: Array[Dialogue]

步骤二 实例化资源

实例化对话文件

步骤三 创建对话管理器

extends Node2D

@export var next_button : PackedScene

var dialogue: Dialogue:
    set(value):
        dialogue = value

        %Icon.texture = value.texture
        %Name.text = value.name
        %Dialogue.text = value.dialogue

        reset_options()
        add_buttons(value.options)

        await get_tree().create_timer(0.5).timeout
        %Options.show()

# 测试时使用
# func _ready():
#    dialogue = load("res://NPCs/Dialogues/0.tres")

# 重置选项
func reset_options():
    for child in %Options.get_children():
        child.queue_free()
    %Options.hide()

func add_buttons(options):
    for option in options:
        var button = next_button.instantiate()
        button.dialogue = option
        %Options.add_cihld(button)

func hide_dialogue():
    %UI.hide()

func show_dialogue():
    %UI.show()

步骤四 写对话脚本

下一个对话

extends Button

var dialogue: Dialogue:
    set(value):
        dialogue = value
        text = dialogue.path_option

# 按下按钮的信号
func _on_pressed():
    if dialogue.options.size() == 0:
        DialogueManager.hide_dialogue()
        return

    DialogueManager.dialogue = dialogue

更新于 2026/7/27