Godot 3.5:如何通过工具模式在导出前修改场景导出变量?
问题:Godot 3.5导出前自动设置Main.tscn中show_title变量为true
主场景包含一个名为show_title的布尔变量,需要通过自定义导出插件,在每次导出游戏前将该变量设置为true。
解决方案
在自定义导出插件的_export_begin方法中,添加加载主场景、修改变量并保存的逻辑,完整代码如下:
tool extends EditorExportPlugin class_name CustomExportPlugin func _export_begin(features, is_debug, path, flags): # 主场景路径,根据项目实际路径调整 var main_scene_path = "res://Main.tscn" # 加载场景资源 var main_scene = load(main_scene_path) if main_scene == null: print_error("无法加载主场景: %s" % main_scene_path) return # 实例化场景根节点以访问变量 var root_node = main_scene.instance() # 检查变量是否存在并设置为true if root_node.has("show_title"): root_node.show_title = true else: print_error("主场景根节点未找到show_title变量") root_node.free() return # 将修改后的节点打包为场景并保存 var updated_scene = PackedScene.new() updated_scene.pack(root_node) var save_result = ResourceSaver.save(main_scene_path, updated_scene) if save_result != OK: print_error("保存主场景失败,错误码: %s" % save_result) # 释放临时实例的节点 root_node.free()
注意事项
- 确认
main_scene_path与项目中Main.tscn的实际路径完全匹配 - 操作会直接修改场景文件,导出后
show_title会保持为true,若需要导出后恢复原值,可在导出完成后添加反向修改逻辑 - 若执行过程中出现异常,编辑器控制台会输出对应的错误信息,方便排查问题
内容的提问来源于stack exchange,提问作者Manas R. Makde
相关产品推荐
相关产品推荐

