UE5 使用SlateViewer模版创建窗口(记录)
复制源码中Engine\Source\SlateViewer文件夹后重命名AMySlateView,替换代码中的SlateViewer为AMySlateView,执行GenerateProjectFiles.bat更新源码,创建AMyCustomSlate的c++类
#pragma once
#include "CoreMinimal.h"
#include "Widgets/Docking/SDockTab.h"
int MyRunSlateView(const TCHAR* Commandline);
TSharedRef<SDockTab> SpawnBrowserTab(const FSpawnTabArgs& Args);
#include "AMyCustomSlate.h"
#include "ISourceCodeAccessModule.h"
#include "LaunchEngineLoop.h"
#include "StandaloneRenderer.h"
int MyRunSlateView(const TCHAR* Commandline)
{
FTaskTagScope TaskTagScope(ETaskTag::EGameThread);
GEngineLoop.PreInit(Commandline);
ProcessNewlyLoadedUObjects();
FModuleManager::Get().StartProcessingNewlyLoadedObjects();
FSlateApplication::InitializeAsStandaloneApplication(GetStandardStandaloneRenderer());
FSlateApplication::InitHighDPI(true);
ISourceCodeAccessModule& SourceCodeAccessModule = FModuleManager::LoadModuleChecked<ISourceCodeAccessModule>( FName( "SourceCodeAccess" ) );
IModuleInterface& VisualStudioSourceCodeAccessModule = FModuleManager::LoadModuleChecked<IModuleInterface>( FName( "VisualStudioSourceCodeAccess" ) );
SourceCodeAccessModule.SetAccessor(FName("VisualStudioSourceCodeAccess"));
TSharedRef<SWindow> BrowserWindowWidget =
SNew(SWindow)
.Title(FText::FromString("this is test"))
.ClientSize(FVector2D(800,600))
.AutoCenter(EAutoCenter::None)
.HasCloseButton(true)
.LayoutBorder(FMargin(0));
FSlateApplication::Get().AddWindow(BrowserWindowWidget);
// 主循环
while (!IsEngineExitRequested())
{
FTaskGraphInterface::Get().ProcessThreadUntilIdle(ENamedThreads::GameThread);
FStats::AdvanceFrame(false);
FTSTicker::GetCoreTicker().Tick(FApp::GetDeltaTime());
FSlateApplication::Get().PumpMessages();
FSlateApplication::Get().Tick();
FPlatformProcess::Sleep(0.01);
GFrameCounter++;
}
// 清理和退出
FSlateApplication::Shutdown();
FModuleManager::Get().UnloadModulesAtShutdown();
GEngineLoop.AppPreExit();
GEngineLoop.AppExit();
return 0;
}
在SlateViewerMainWindows.cpp中调用AMyCustomSlate中的MyRunSlateView函数
// Copyright Epic Games, Inc. All Rights Reserved.
#include "AMyCustomSlate.h"
#include "SlateViewerApp.h"
#include "Windows/WindowsHWrapper.h"
/**
* WinMain, called when the application is started
*/
int WINAPI WinMain( _In_ HINSTANCE hInInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR, _In_ int nCmdShow )
{
// do the slate viewer thing
//RunSlateViewer(GetCommandLineW());
MyRunSlateView(GetCommandLineW());
return 0;
}