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

Godot 3.5如何从编辑器调用脚本方法用于测试?

Godot 3.5 替代Unity ContextMenu的方案

Godot 3.5有几种类似Unity ContextMenu的实现方式,适合快速测试调试功能:

1. 在Inspector面板添加按钮(最接近ContextMenu体验)

通过@tool脚本让代码在编辑器环境运行,自定义Inspector属性列表添加可点击按钮:

@tool
extends Node

# 自定义Inspector面板的属性列表,添加测试按钮
func _get_property_list():
    var props = []
    props.append({
        name = "执行测试逻辑",
        type = TYPE_NIL,
        usage = PROPERTY_USAGE_BUTTON
    })
    return props

# 处理按钮点击事件
func _set(property: String, value: Variant) -> bool:
    if property == "执行测试逻辑":
        _run_debug_function()
        return true
    return false

# 你的调试测试方法
func _run_debug_function():
    print("调试方法执行中...")
    # 这里编写具体的测试逻辑

挂载该脚本的节点会在Inspector面板显示按钮,点击即可直接调用测试方法。

2. 自定义编辑器菜单

如果需要全局调试入口,可通过EditorPlugin添加顶部或右键菜单:

extends EditorPlugin

func _enter_tree():
    # 在编辑器顶部菜单添加"调试"分类及选项
    add_menu_item("调试/运行选中节点测试方法", _execute_selected_test)

func _execute_selected_test():
    # 获取当前选中的节点
    var selected_nodes = get_editor_interface().get_selection().get_selected_nodes()
    if selected_nodes.size() > 0:
        var target_node = selected_nodes[0]
        # 检查节点是否有对应测试方法,有则调用
        if target_node.has_method("_run_debug_function"):
            target_node._run_debug_function()

func _exit_tree():
    # 插件卸载时移除菜单
    remove_menu_item("调试/运行选中节点测试方法")

将此脚本作为编辑器插件启用后,就能通过顶部菜单快速调用选中节点的测试方法。

3. 开关式触发(简易替代)

用@export配合setget实现开关,勾选时触发测试逻辑,适合快速调试:

@tool
extends Node

@export var 触发测试: bool setget _on_trigger_test

func _on_trigger_test(new_value: bool):
    if new_value:
        _run_debug_function()
        触发测试 = false # 自动重置开关,避免重复触发

func _run_debug_function():
    print("简易测试逻辑执行")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 14:45:26