用途
该技能用于自动化与扩展 Unreal Editor,所有内容均为编辑器专用:位于编辑器模块内或受 WITH_EDITOR 保护,打包游戏时会被剥离。两条主要路径为 Python 脚本插件(unreal 模块,适合管道自动化与批量脚本)和 Blutility(编辑器实用工具控件/蓝图,适合面向美术与设计的编辑器内 UI 工具)。
适用场景包括:对资产做批量操作(重命名、设置属性、重新导入、生成 LOD);为美术制作编辑器内自定义 UMG 面板;从内容浏览器或关卡触发右键脚本操作;在 CI 中以无头 commandlet 运行;将 C++ 函数以正确名称暴露给 Python/蓝图;从 C++ 或 Python 获取编辑器子系统引用。
主要能力
Python(unreal 模块)
启用 Python Editor Script Plugin(Plugins > Scripting),插件自带 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/set_editor_property(会触发 pre/post-edit 通知)。批量操作可用 unreal.ScopedEditorTransaction 支持撤销/重做;长任务用 unreal.ScopedSlowTask 提供进度与取消。
启动脚本:可在 Project Settings → Plugins → Python → Startup Scripts 添加路径;任意 Content/Python 文件夹中的 init_unreal.py 也会在启动时自动运行。
无头运行:UPythonScriptCommandlet 可在不打开完整 UI 的情况下执行脚本,命令形如 UnrealEditor-Cmd.exe MyProject.uproject -run=pythonscript -script="my_script.py"。
Editor Utility Widgets 与蓝图(Blutility)
Blutility 类位于 Editor/Blutility/,模块名为 Blutility,需加入编辑器模块的 PrivateDependencyModuleNames。关键类:UEditorUtilityWidget(继承 UUserWidget,可停靠的编辑器内 UMG 面板,Run() 为自动运行时的 BlueprintImplementableEvent,TabDisplayName 设置面板名);UEditorUtilityWidgetBlueprint(生成 UEditorUtilityWidget 的资产);UEditorUtilityObject(无 UI 的编辑器工具,bRunEditorUtilityOnStartup 可设为启动后自动运行);UAssetActionUtility(子类上的 BlueprintCallable 函数会出现在内容浏览器右键菜单,通过 SupportedClasses 过滤资产类型,GetSupportedClass() 自 UE 5.2 弃用);UEditorUtilityTask(由 UEditorUtilitySubsystem 管理的后台任务)。UEditorUtilitySubsystem 负责控件标签页与工具任务,可从 C++ 用 SpawnAndRegisterTab 打开标签页,Python 中用 spawn_and_register_tab。
编辑器子系统
所有子系统继承 UEditorSubsystem,C++ 通过 GEditor->GetEditorSubsystem<T>() 访问,Python 通过 unreal.get_editor_subsystem(T) 访问。
UEditorActorSubsystem:当前关卡编辑器世界中的 Actor 级操作,如 GetAllLevelActors、GetSelectedLevelActors、SpawnActorFromClass(放置到编辑器世界并通知编辑器)、DestroyActor。
UEditorAssetSubsystem:内容浏览器资产操作,是 UEditorAssetLibrary 的首选替代,含 LoadAsset、SaveAsset、DuplicateAsset、RenameAsset、DeleteAsset。
ULevelEditorSubsystem:关卡文件与视口操作,如 NewLevel、LoadLevel、SaveCurrentLevel、EditorRequestBeginPlay。
UAssetEditorSubsystem:以编程方式在专用资产编辑器中打开资产。
UUnrealEditorSubsystem:视口相机查询/设置(GetLevelViewportCameraInfo、SetLevelViewportCameraInfo)与 GetEditorWorld()。
将 C++ 暴露给编辑器脚本
UFUNCTION(BlueprintCallable):自动反射到 Python,类名去前缀、函数名转 snake_case。
meta = (CallInEditor = "true"):在选中实例的 Actor 详情面板中显示为按钮,适用于 AActor 与 UActorComponent 子类。
meta = (ScriptMethod):将首个参数为结构体或对象的静态 BlueprintFunctionLibrary 函数,在 Python 中提升为该类型的实例方法(如 v.scale_vector(2.0))。相关说明符有 ScriptMethodSelfReturn、ScriptMethodMutable。
meta = (ScriptName = "PythonName"):覆盖 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 构建(需重新编译源码)。
常见问题:运行时模块中出现编辑器专用代码会导致打包失败;Python 不随游戏发布,仅为工具/编辑器插件;直接设置属性会绕过 pre/post-edit 回调,EditAnywhere 属性应使用 set_editor_property;长 Python 任务会阻塞编辑器 UI,应配合 ScopedSlowTask 与 enter_progress_frame;忘记保存会丢失资产更改,需调用 SaveAsset 或等价 Python 方法;避免硬编码资产路径,应通过 Asset Registry 动态发现;编辑器放置 Actor 应使用 SpawnActorFromClass 而非 UWorld::SpawnActor。
官方文档链接包括 Epic 的“Scripting and Automating the Unreal Editor”“Scripting the Unreal Editor Using Python”“Scripting the Unreal Editor Using Blueprints”及 Python API Reference。该技能还附带 references/editor-subsystems.md、references/editor-utility-widgets.md、references/python-api.md 等深入参考,相关技能有 ue-subsystems、ue-plugins-and-modules、ue-asset-management。具体收费、价格与更多平台支持信息暂未提供。