UE4 Slate教程5——Slate独立程序

可以将Slate作为MFC、QT一类的工作开发独立程序,此时需要源码版的UE4,可从官方提供的Github地址下载。
下载后把BlankProgram设为启动项,并修改如下代码:
UE4 Slate教程5——Slate独立程序_第1张图片
BlankProgram.Target.cs:

using UnrealBuildTool;
using System.Collections.Generic;

[SupportedPlatforms(UnrealPlatformClass.Desktop)]
public class BlankProgramTarget : TargetRules
{
	public BlankProgramTarget(TargetInfo Target) : base(Target)
	{
		Type = TargetType.Program;// 类型为Program
		LinkType = TargetLinkType.Monolithic;// 模块的链接方式,Monolithic:1个,Modular:多个dll
        LaunchModuleName = "BlankProgram";// 启动模块

		bBuildDeveloperTools = false;// 其还会决定bCompileLeanAndMeanUE

        bUseMallocProfiler = false;// 是否启用内存分析

		bBuildWithEditorOnlyData = true;

		bCompileAgainstEngine = false;// 是否链接所有引擎程序生成的项目
        //bCompileAgainstCoreUObject = false;
        bCompileAgainstCoreUObject = true;// 需要连接到CoreUObject模块
        //bCompileAgainstApplicationCore = false;
		bCompileAgainstApplicationCore = true;

		//bIsBuildingConsoleApplication = true;
        bIsBuildingConsoleApplication = false;// 编译带窗口的
    }
}

BlankProgram.Build.cs:

using UnrealBuildTool;

public class BlankProgram : ModuleRules
{
	public BlankProgram(ReadOnlyTargetRules Target) : base(Target)
	{
		PublicIncludePaths.Add("Runtime/Launch/Public");

		PrivateIncludePaths.Add("Runtime/Launch/Private");		// For LaunchEngineLoop.cpp include

		PrivateDependencyModuleNames.Add("Core");
		PrivateDependencyModuleNames.Add("Projects");

		// 添加Slate依赖
        PrivateDependencyModuleNames.AddRange(
			new string[]
            {
				"Slate",
				"SlateCore",
                "StandaloneRenderer"
            });

    }
}

BlankProgram.cpp:

#include "BlankProgram.h"

#include "RequiredProgramMainCPPInclude.h"
#include "Framework/Application/SlateApplication.h"
#include "StandaloneRenderer.h"
#include "Widgets/DeclarativeSyntaxSupport.h"
#include "Widgets/SWindow.h"
#include "Widgets/Images/SImage.h"

DEFINE_LOG_CATEGORY_STATIC(LogBlankProgram, Log, All);

IMPLEMENT_APPLICATION(BlankProgram, "BlankProgram");// 注册模块

//INT32_MAIN_INT32_ARGC_TCHAR_ARGV()
//{
//	GEngineLoop.PreInit(ArgC, ArgV);
//	UE_LOG(LogBlankProgram, Display, TEXT("Hello World"));
//	return 0;
//}

int WINAPI WinMain(_In_ HINSTANCE hInInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR, _In_ int nCmdShow)
{
	GEngineLoop.PreInit(GetCommandLineW());
	FSlateApplication::InitializeAsStandaloneApplication(GetStandardStandaloneRenderer());

	// 建立窗口
	TSharedPtr<SWindow> MainWindow = SNew(SWindow).ClientSize(FVector2D(1280, 720))
		[
			SNew(SImage)// 这里以Image做示范
		];
	FSlateApplication::Get().AddWindow(MainWindow.ToSharedRef());

	// 消息循环
	while (!GIsRequestingExit)
	{
		FSlateApplication::Get().Tick();
		FSlateApplication::Get().PumpMessages();
	}

	FSlateApplication::Shutdown();
	return 0;
}

效果如下:
UE4 Slate教程5——Slate独立程序_第2张图片
另外,源码中有一个名为SlateViewer的项目,对于学习Slate有很大的参考价值。

你可能感兴趣的:(UE4)