【用途】本技能面向 Unreal Editor 的编辑器脚本与自动化,所有内容均为编辑器专用:代码位于编辑器模块或 WITH_EDITOR 之后,打包游戏时不包含。适用于批量资产操作(重命名、设置属性、重新导入、生成 LOD)、为美术制作编辑器内 UMG 面板、从内容浏览器或关卡触发右键脚本动作、在 CI 中无界面运行 commandlet,以及把 C++ 函数暴露到 Python/蓝图、从 C++ 或 Python 获取编辑器子系统引用。
【两条主路径】1)Python Script Plugin(unreal 模块):启用 Plugins > Scripting 中的 Python Editor Script Plugin,插件内置 Python 3.11.8,无需单独安装;unreal 模块在启动时根据反射到蓝图的内容自动生成,任何启用插件暴露的 BlueprintCallable 函数或类都可用。命名规则:C++ 类 UEditorAssetSubsystem 对应 Python 类 unreal.EditorAssetSubsystem,函数 GetAllLevelActors() 变成 get_all_level_actors(),枚举值变为 UPPER_SNAKE_CASE,U/A/F/T 前缀被去掉。BlueprintReadWrite 属性可直接访问;EditAnywhere 属性应使用 get_editor_property/set_editor_property 以触发编辑前后回调。可用 unreal.ScopedEditorTransaction 支持撤销/重做,用 unreal.ScopedSlowTask 为长任务提供进度与取消。启动脚本可在 Project Settings → Plugins → Python → Startup Scripts 配置,任意 Content/Python 下的 init_unreal.py 也会自动运行;无界面运行命令为 UnrealEditor-Cmd.exe MyProject.uproject -run=pythonscript -script="my_script.py"。
2)Blutility(Editor Utility Widgets/Blueprints):模块为 Blutility,需加入编辑器模块的 PrivateDependencyModuleNames。核心类包括 UEditorUtilityWidget(继承 UUserWidget,可停靠的编辑器 UMG 面板,Run() 在自动运行时调用,TabDisplayName 设置面板名)、UEditorUtilityObject(无 UI 的纯逻辑工具,bRunEditorUtilityOnStartup 可自动运行)、UAssetActionUtility(子类上的 BlueprintCallable 函数出现在内容浏览器右键菜单,通过 SupportedClasses 过滤资产类型)和 UEditorUtilityTask(由 UEditorUtilitySubsystem 管理的后台任务)。UEditorUtilitySubsystem 管理面板标签页与任务,C++ 用 SpawnAndRegisterTab,Python 用 unreal.get_editor_subsystem(unreal.EditorUtilitySubsystem).spawn_and_register_tab。
【编辑器子系统】所有子系统继承 UEditorSubsystem,C++ 通过 GEditor->GetEditorSubsystem<T>() 访问,Python 通过 unreal.get_editor_subsystem(T) 访问。UEditorActorSubsystem 提供关卡 Actor 操作(GetAllLevelActors、GetSelectedLevelActors、SpawnActorFromClass、DestroyActor);UEditorAssetSubsystem 提供资产操作(LoadAsset、SaveAsset、DuplicateAsset、RenameAsset、DeleteAsset),是 UEditorAssetLibrary 的首选替代;ULevelEditorSubsystem 提供关卡文件与视口操作(NewLevel、LoadLevel、SaveCurrentLevel、EditorRequestBeginPlay);UAssetEditorSubsystem 可用程序方式在专用资产编辑器中打开资产;UUnrealEditorSubsystem 提供视口相机查询/设置与 GetEditorWorld()。
【暴露 C++ 能力】BlueprintCallable 函数自动反射到 Python,类名去前缀、函数名转 snake_case。CallInEditor 元数据让函数在选中实例时显示为 Details 面板按钮,适用于 AActor 与 UActorComponent 子类。ScriptMethod 可把首个参数为结构体或对象的静态 BlueprintFunctionLibrary 函数在 Python 中提升为该类型的实例方法;ScriptName 可覆盖 Python 中的函数名。
【版本注意】UEditorAssetLibrary/UEditorLevelLibrary(EditorScriptingUtilities 插件)在 UE 5.0 已弃用,新代码应使用 UEditorAssetSubsystem 与 UEditorActorSubsystem;UAssetActionUtility::GetSupportedClass() 在 UE 5.2 弃用,改用类默认值中的 SupportedClasses 数组;UE 5.8 内置 Python 3.11.8,可通过 UE_PYTHON_DIR 嵌入其他 CPython 构建(需重新编译源码)。
【限制与注意事项】运行时模块中放编辑器代码会导致打包失败,应放在 WITH_EDITOR 后或 type 为 Editor 的模块;Python 不随游戏发布,仅限工具/编辑器插件;直接设置属性会绕过编辑前后回调,EditAnywhere 属性应使用 set_editor_property;长 Python 任务会阻塞编辑器 UI,应配合 ScopedSlowTask 与 enter_progress_frame;记得调用 SaveAsset 保存,否则编辑器关闭后未保存的改动会丢失;避免硬编码资产路径,可用 Asset Registry 动态发现;编辑器内放置 Actor 应使用 SpawnActorFromClass 而非 UWorld::SpawnActor。
【相关资料】技能内参考文档包括 references/editor-subsystems.md(子系统方法与 Python 对应)、references/editor-utility-widgets.md(Blutility 类层次、标签页生命周期、UEditorUtilityTask、脚本动作)和 references/python-api.md(C++↔Python 命名规则、类型映射、get_editor_subsystem、事务、日志、commandlet 调用);相关技能为 subsystems、plugins-and-modules、asset-management。官方文档见 Epic 的 Scripting and Automating the Unreal Editor、Scripting the Unreal Editor Using Python/Blueprints 与 Python API Reference。