百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分析 > 正文

「UE5之Viewport系列:01」在编辑器界面上创建一个预览视窗

liebian365 2024-10-15 13:52 76 浏览 0 评论

目标:UE5编辑器下现实自定义预览窗口

UE5关卡编辑器的场景视窗,还有双击StaticMesh后打开的预览场景视窗,其实都是一个SEditorViewport控件。只不过它们属于不同的子类,派生关系如下:

本篇的目标是尝试在一个独立的窗口中,显示一个预览视窗(SEditorViewport控件)。

步骤1. 创建UE5工程

新建UE5的C++空白项目,记得选择C++,初学者包可以根据自己需要选择是否勾选

步骤2. 创建插件

选择编辑菜单下的插件按钮打开插件面板,编辑器Standalone窗口为模板,这样可以有一个独立的窗口作为起点。

插件名字是TestPreviewViewport

它将在编辑器的工具栏中生成一个按钮,点击它会显示一个窗口分页控件(SDockTab),而其内容则指定在FTestPreviewViewportModule::OnSpawnPluginTab中:

现在没有修改任何代码,运行看一下效果。点击窗口菜单就能看到新创建的插件按钮TestPreviewViewport,点击按钮看到UE5系统默认插件面板



步骤3. 定义新的SEditorViewport

首先,我需要一个SEditorViewport,然而它本身有纯虚函数:

virtual TSharedRef<FEditorViewportClient> MakeEditorViewportClient() = 0;

因此它是抽象类,不能直接实例化。于是想到使用它的一个子类,而对于它的所有子类:

一一查看后,发现只有SLevelViewport被模块_API宏标记了,这意味着只有它可以被外部使用。但我不想使用关卡的视窗,因为我担心它过于复杂。

最终的结论是,没有合适的子类供我使用,我只能新定义一个SEditorViewport的子类,新定义的子类名字是STestEditorViewport。

暂时的STestEditorViewport.h

#pragma once
#include "CoreMinimal.h"
#include "SEditorViewport.h"

class STestEditorViewport : public SEditorViewport
{
public:
   SLATE_BEGIN_ARGS(STestEditorViewport){}
   SLATE_END_ARGS()

   void Construct(const FArguments& InArgs);
   
   STestEditorViewport();
   ~STestEditorViewport();

protected:
   virtual TSharedRef<FEditorViewportClient> MakeEditorViewportClient() override;
   
   
};

暂时的STestEditorViewport.cpp

#include "STestEditorViewport.h"
#include"EditorViewportClient.h"

void STestEditorViewport::Construct(const FArguments& InArgs)
{
   // 调用父类构造函数
   SEditorViewport::Construct(SEditorViewport::FArguments());
   
}

STestEditorViewport::STestEditorViewport()
{
}

STestEditorViewport::~STestEditorViewport()
{
}

TSharedRef<FEditorViewportClient> STestEditorViewport::MakeEditorViewportClient()
{
   // 创建FEditorViewportClient
   TSharedPtr<FEditorViewportClient> EditorViewportClient = MakeShareable(new FEditorViewportClient(nullptr));
   return EditorViewportClient.ToSharedRef();
}

注意MakeEditorViewportClient(),他需要得到一个FEditorViewportClient对象,由于它本身不是抽象类,所以我可以直接使用这个类。

此时新创建的STestEditorViewport和插件还没有关联起来,需要我们修改FTestPreviewViewportModule::OnSpawnPluginTab

当前预览窗口打开后,是一个和关卡编辑器里的场景一模一样的场景:

调试源代码发现,原来在未传入预览场景的情况下,GetWorld()将会返回GWorld,此即为关卡编辑器内显示的场景。

步骤4. 创建FPreviewScene

为了使用自己的场景而不是默认的关卡编辑器内的场景,需要自己提供一个FPreviewScene对象。

为此,先创建一个FPreviewScene的智能指针(防止对象被销毁)在STestEditorViewport中:

TSharedPtr<class FPreviewScene> PreviewScene;




然后,在创建FEditorViewportClient时就可以将自己的场景传递到其构造函数中了:

看起来空无一物,但是左下角的坐标表示现在确实在一个3D场景中。

步骤5. 显示视窗工具栏

SEditorViewport中有一个虚函数,它将指定视窗的工具栏:

virtual TSharedPtr<SWidget> MakeViewportToolbar() { return TSharedPtr<SWidget>(nullptr); }

默认情况下如果不在子类中实现这个函数,它将返回空,即不生成工具栏。

查看官方的代码SStaticMeshEditorViewport,它将返回SStaticMeshEditorViewportToolbar

看定义发现它似乎在SStaticMeshEditorViewportToolbar的基础上增加了LOD相关的功能。当前我不需要其他附加的功能,因此对于我的STestEditorViewport,我希望直接使用视窗工具栏的基类,即SCommonEditorViewportToolbarBase

但现在有个问题是,SCommonEditorViewportToolbarBaseConstruct要求一个ICommonEditorViewportToolbarInfoProvider指针:
void Construct(const FArguments& InArgs, TSharedPtr<class ICommonEditorViewportToolbarInfoProvider> InInfoProvider);

查看ICommonEditorViewportToolbarInfoProvider的定义:

class ICommonEditorViewportToolbarInfoProvider
{
public:
	// Get the viewport widget
	virtual TSharedRef<class SEditorViewport> GetViewportWidget() = 0;

// 	FLevelEditorModule& LevelEditorModule = FModuleManager::GetModuleChecked<FLevelEditorModule>(TEXT("LevelEditor"));
// 	TSharedPtr<FExtender> LevelEditorExtenders = LevelEditorModule.GetMenuExtensibilityManager()->GetAllExtenders();
	virtual TSharedPtr<FExtender> GetExtenders() const = 0;

	// Called to inform the host that a button was clicked (typically used to focus on a particular viewport in a multi-viewport setup)
	virtual void OnFloatingButtonClicked() = 0;
};

它似乎是和视窗工具栏所对接而必须实现的接口。观察官方使用,是直接让SStaticMeshEditorViewport继承了这个接口。因此我也学着让我的STestEditorViewport继承它:


而实现上,就是和SStaticMeshEditorViewport中的一样:

最后,在MakeViewportToolbar中指定返回一个SCommonEditorViewportToolbarBase,并将自己转换为一个ICommonEditorViewportToolbarInfoProvider接口作为参数:


效果,可以看到左上角出现了视窗工具栏

步骤6. 向预览场景中添加组件

向预览场景中添加组件是通过FPreviewScene::AddComponent完成的:

virtual void AddComponent(class UActorComponent* Component,const FTransform& LocalToWorld, bool bAttachToRoot=false);

顺带一提,不用担心组件会被意外销毁,因为FPreviewScene是一个FGCObject,它会将所有组件添加引用:

void FPreviewScene::AddReferencedObjects( FReferenceCollector& Collector )
{
   Collector.AddReferencedObjects( Components );
   Collector.AddReferencedObject( PreviewWorld );
}

现在,向预览场景中添加一个测试用的模型,STestEditorViewport新增一个私有函数AddTestModelToScene用来生成一个box

void STestEditorViewport::AddTestModelToScene()
{
   //读取内置模型
   UStaticMesh* SM = LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Engine/EngineMeshes/Cube.Cube'"), NULL, LOAD_None, NULL);
   //创建组件
   UStaticMeshComponent* SMC = NewObject<UStaticMeshComponent>();
   SMC->SetStaticMesh(SM);
   //向预览场景中增加组件
   PreviewScene->AddComponent(SMC,FTransform::Identity);
   
}

效果:



扩展

上面的代码可以说是无法更省略的最简单创建一个预览视窗的代码了。

然而实际使用中,可能会出现一些必须要新定义一些子类来指定更个性化的行为的情况(就像《UE4自定义资源编辑器开发-预览窗口》所做的一样),这时候可能需要对下面的类进行继承:

FPreviewScene

FEditorViewportClient

SCommonEditorViewportToolbarBase

最终代码

STestEditorViewport.h

#pragma once
#include "CoreMinimal.h"
#include "SCommonEditorViewportToolbarBase.h"
#include "SEditorViewport.h"

class STestEditorViewport : public SEditorViewport, public  ICommonEditorViewportToolbarInfoProvider
{
public:
   SLATE_BEGIN_ARGS(STestEditorViewport){}
   SLATE_END_ARGS()

   void Construct(const FArguments& InArgs);
   
   STestEditorViewport();
   ~STestEditorViewport();

public:
   // ICommonEditorViewportToolbarInfoProvider interface
   virtual TSharedRef<SEditorViewport> GetViewportWidget() override;
   virtual TSharedPtr<FExtender> GetExtenders() const override;
   virtual void OnFloatingButtonClicked() override;
   
protected:
   /** SEditorViewport interface */
   virtual TSharedRef<FEditorViewportClient> MakeEditorViewportClient() override;
   virtual TSharedPtr<SWidget> MakeViewportToolbar() override;

private:
   //预览窗口
   TSharedPtr<class FPreviewScene> PreviewScene;
   void AddTestModelToScene();
   
};

STestEditorViewport.cpp

#include"STestEditorViewport.h"

#include"EditorViewportClient.h"
#include"PreviewScene.h"
#include"SCommonEditorViewportToolbarBase.h"

void STestEditorViewport::Construct(const FArguments& InArgs)
{
   // 调用父类构造函数
   SEditorViewport::Construct(SEditorViewport::FArguments());
   
}

STestEditorViewport::STestEditorViewport()
{
}

STestEditorViewport::~STestEditorViewport()
{
   // This is the interface that the host of a SCommonEditorViewportToolbarBase must implement

}

TSharedRef<SEditorViewport> STestEditorViewport::GetViewportWidget()
{
   return SharedThis(this);
}

TSharedPtr<FExtender> STestEditorViewport::GetExtenders() const
{
   TSharedPtr<FExtender> Result(MakeShareable((new FExtender)));
   return Result;
}

void STestEditorViewport::OnFloatingButtonClicked()
{
   
   
}

TSharedRef<FEditorViewportClient> STestEditorViewport::MakeEditorViewportClient()
{
   
   PreviewScene = MakeShareable(new FPreviewScene()); // 创建FPreviewScene

   //向预览场景中加一个测试模型
   AddTestModelToScene();
   
   // 创建FEditorViewportClient
   TSharedPtr<FEditorViewportClient> EditorViewportClient = MakeShareable(new FEditorViewportClient(nullptr, PreviewScene.Get()));
   return EditorViewportClient.ToSharedRef();
}

TSharedPtr<SWidget> STestEditorViewport::MakeViewportToolbar()
{
   return SNew(SCommonEditorViewportToolbarBase, SharedThis(this));
}

void STestEditorViewport::AddTestModelToScene()
{
   //读取内置模型
   UStaticMesh* SM = LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Engine/EngineMeshes/Cube.Cube'"), NULL, LOAD_None, NULL);
   //创建组件
   UStaticMeshComponent* SMC = NewObject<UStaticMeshComponent>();
   SMC->SetStaticMesh(SM);
   //向预览场景中增加组件
   PreviewScene->AddComponent(SMC,FTransform::Identity);
   
}

TestPreviewViewport.cpp中的OnSpawnPluginTab函数:

TSharedRef<SDockTab> FTestPreviewViewportModule::OnSpawnPluginTab(const FSpawnTabArgs& SpawnTabArgs)
{
   
   FText WidgetText = FText::Format(
      LOCTEXT("WindowWidgetText", "Add code to {0} in {1} to override this window's contents"),
      FText::FromString(TEXT("FTestPreviewViewportModule::OnSpawnPluginTab")),
      FText::FromString(TEXT("TestPreviewViewport.cpp"))
      );
   /*
   return SNew(SDockTab)
      .TabRole(ETabRole::NomadTab)
      [
         // Put your tab content here!
         SNew(SBox)
         .HAlign(HAlign_Center)
         .VAlign(VAlign_Center)
         [
            SNew(STextBlock)
            .Text(WidgetText)
         ]
      ];
      */
   
   return  SNew(SDockTab)
      .TabRole(ETabRole::NomadTab)
      [
         //创建预览视窗控件
         SNew(STestEditorViewport)
      ];
   
}

相关推荐

“版本末期”了?下周平衡补丁!国服最强5套牌!上分首选

明天,酒馆战棋就将迎来大更新,也聊了很多天战棋相关的内容了,趁此机会,给兄弟们穿插一篇构筑模式的卡组推荐!老规矩,我们先来看10职业胜率。目前10职业胜率排名与一周前基本类似,没有太多的变化。平衡补丁...

VS2017 C++ 程序报错“error C2065:“M_PI”: 未声明的标识符&quot;

首先,程序中头文件的选择,要选择头文件,在文件中是没有对M_PI的定义的。选择:项目——>”XXX属性"——>配置属性——>C/C++——>预处理器——>预处理器定义,...

东营交警实名曝光一批酒驾人员名单 88人受处罚

齐鲁网·闪电新闻5月24日讯酒后驾驶是对自己和他人生命安全极不负责的行为,为守护大家的平安出行路,东营交警一直将酒驾作为重点打击对象。5月23日,东营交警公布最新一批饮酒、醉酒名单。对以下驾驶人醉酒...

Qt界面——搭配QCustomPlot(qt platform)

这是我第一个使用QCustomPlot控件的上位机,通过串口精确的5ms发送一次数据,再将读取的数据绘制到图表中。界面方面,尝试卡片式设计,外加QSS简单的配了个色。QCustomPlot官网:Qt...

大话西游2分享赢取种族坐骑手办!PK趣闻录由你书写

老友相聚,仗剑江湖!《大话西游2》2021全民PK季4月激燃打响,各PK玩法鏖战齐开,零门槛参与热情高涨。PK季期间,不仅各种玩法奖励丰厚,参与PK趣闻录活动,投稿自己在PK季遇到的趣事,还有机会带走...

测试谷歌VS Code AI 编程插件 Gemini Code Assist

用ClaudeSonnet3.7的天气测试编码,让谷歌VSCodeAI编程插件GeminiCodeAssist自动编程。生成的文件在浏览器中的效果如下:(附源代码)VSCode...

顾爷想知道第4.5期 国服便利性到底需优化啥?

前段时间DNF国服推出了名为“阿拉德B计划”的系列改版计划,截至目前我们已经看到了两项实装。不过关于便利性上,国服似乎还有很多路要走。自从顾爷回归DNF以来,几乎每天都在跟我抱怨关于DNF里面各种各样...

掌握Visual Studio项目配置【基础篇】

1.前言VisualStudio是Windows上最常用的C++集成开发环境之一,简称VS。VS功能十分强大,对应的,其配置系统较为复杂。不管是对于初学者还是有一定开发经验的开发者来说,捋清楚VS...

还嫌LED驱动设计套路深?那就来看看这篇文章吧

随着LED在各个领域的不同应用需求,LED驱动电路也在不断进步和发展。本文从LED的特性入手,推导出适合LED的电源驱动类型,再进一步介绍各类LED驱动设计。设计必读:LED四个关键特性特性一:非线...

Visual Studio Community 2022(VS2022)安装图文方法

直接上步骤:1,首先可以下载安装一个VisualStudio安装器,叫做VisualStudioinstaller。这个安装文件很小,很快就安装完成了。2,打开VisualStudioins...

Qt添加MSVC构建套件的方法(qt添加c++11)

前言有些时候,在Windows下因为某些需求需要使用MSVC编译器对程序进行编译,假设我们安装Qt的时候又只是安装了MingW构建套件,那么此时我们该如何给现有的Qt添加一个MSVC构建套件呢?本文以...

Qt为什么站稳c++GUI的top1(qt c)

为什么现在QT越来越成为c++界面编程的第一选择,从事QT编程多年,在这之前做C++界面都是基于MFC。当时为什么会从MFC转到QT?主要原因是MFC开发界面想做得好看一些十分困难,引用第三方基于MF...

qt开发IDE应该选择VS还是qt creator

如果一个公司选择了qt来开发自己的产品,在面临IDE的选择时会出现vs或者qtcreator,选择qt的IDE需要结合产品需求、部署平台、项目定位、程序猿本身和公司战略,因为大的软件产品需要明确IDE...

Qt 5.14.2超详细安装教程,不会来打我

Qt简介Qt(官方发音[kju:t],音同cute)是一个跨平台的C++开库,主要用来开发图形用户界面(GraphicalUserInterface,GUI)程序。Qt是纯C++开...

Cygwin配置与使用(四)——VI字体和颜色的配置

简介:VI的操作模式,基本上VI可以分为三种状态,分别是命令模式(commandmode)、插入模式(Insertmode)和底行模式(lastlinemode),各模式的功能区分如下:1)...

取消回复欢迎 发表评论: