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

如何在Unreal Engine插件C++代码中启动当前关卡运行?

在UE5插件中实现关卡运行/停止功能的方案

核心API说明

UE5编辑器中控制关卡运行的核心逻辑封装在FPlayWorldCommands命令集和UEditorPlayWorld类中,这两个接口完全对齐原生工具栏"Play"按钮的行为。

实现步骤

  1. 添加模块依赖
    打开插件的.uplugin文件,确保依赖UnrealEd和EditorFramework模块(这两个模块包含了关卡运行相关的API):

    "Modules": [
        {
            "Name": "YourPluginName",
            "Type": "Editor",
            "LoadingPhase": "PostEngineInit",
            "Dependencies": [
                "Core",
                "CoreUObject",
                "Engine",
                "UnrealEd",
                "EditorFramework"
            ]
        }
    ]
    
  2. 引入必要头文件
    在按钮点击事件的实现文件(如YourPluginModule.cpp)中添加以下头文件:

    #include "Editor/EditorPlayWorld.h"
    #include "PlayWorldCommands.h"
    
  3. 实现按钮点击逻辑
    在按钮的回调函数中,通过命令系统实现启动/停止关卡的逻辑,和原生Play按钮行为完全一致:

    void FYourPluginModule::PluginButtonClicked()
    {
        // 判断当前是否处于PIE运行状态
        if (GEditor->IsPlaySessionInProgress())
        {
            // 停止关卡运行
            FPlayWorldCommands::Get().StopSession.Execute();
        }
        else
        {
            // 启动关卡运行(默认使用当前关卡,Play In Viewport模式)
            FPlayWorldCommands::Get().PlaySession.Execute();
        }
    }
    

进阶扩展

如果需要自定义运行模式(如Play In New Window、VR模式等),可以直接调用UEditorPlayWorld的静态方法:

// 示例:以"独立窗口"模式启动关卡
UEditorPlayWorld::PlayWorld(this, EPlayMode::PlayInNewWindow);

状态监听(可选)

如果需要监听关卡运行的开始/结束事件,可以绑定编辑器委托:

// 在模块初始化时绑定委托
FEditorDelegates::BeginPIE.AddRaw(this, &FYourPluginModule::OnPIEBegin);
FEditorDelegates::EndPIE.AddRaw(this, &FYourPluginModule::OnPIEEnd);

// 实现回调函数
void FYourPluginModule::OnPIEBegin(bool bIsSimulating)
{
    // 关卡开始运行时的逻辑
}

void FYourPluginModule::OnPIEEnd(bool bIsSimulating)
{
    // 关卡停止运行时的逻辑
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 20:06:28