在project目录创建独立program

一、独立运行的consle程序

网上大多数创建program的例子都是在引擎目录下,这里尝试了如何在project目录创建program的方法,参考:使用UnrealEngine创建独立应用 - 知乎 (zhihu.com)

这里以创建一个MyProgram的独立exe进行举例

  • 拷贝引擎目录的blankProgram目录到项目的source目录下

  • 修改BlankProgram.Build.cs

    • 将BlankProgram.Target.cs 命名为MyProgram.Build.cs

    • MyProgram.Build.cs文件添加命名空间 using System.IO;

    • 修改BlankProgram的ModuleRules派生类改为MyProgram

    • 注释掉原来的相对路径包含路径代码,添加新的绝对路径包含代码

      //PublicIncludePaths.Add("Runtime/Launch/Public");
      
      //PrivateIncludePaths.Add("Runtime/Launch/Private");	
      // For LaunchEngineLoop.cpp include
      // 在引擎目录之外创建独立app需要使用绝对路径
      PrivateIncludePaths.Add(Path.Combine(EngineDirectory, "Source/Runtime/Launch/Public"));
      PrivateIncludePaths.Add(Path.Combine(EngineDirectory, "Source/Runtime/Launch/Private"));
      
  • 修改BlankProgram.Target.cs为MyProgram.Target.cs

  • 修改BlankProgram的TargetRules派生类改为MyProgram

  • 修改LaunchModuleName名称为"MyProgram"。注意此处一定要改成自己模块的名字,如果还是BlankProgram,则编译的exe,仍然时引擎的BlankProgram模块的代码,只是名字时当前exe的名字。

  • 修改cpp和头文件名称为MyProgram

  • 修改cpp包含头文件名称,log名等

    #include "MyProgram.h"
    
    #include "RequiredProgramMainCPPInclude.h"
    #include 
    
    DEFINE_LOG_CATEGORY_STATIC(LogMyProgram, Log, All);
    
    IMPLEMENT_APPLICATION(MyProgram, "MyProgram");
    INT32_MAIN_INT32_ARGC_TCHAR_ARGV()
    {
        GEngineLoop.PreInit(ArgC, ArgV);
        UE_LOG(LogMyProgram, Display, TEXT("Hello World MyProgram"));
        FEngineLoop::AppExit();
        getch();// 按任意键退出
        return 0;
    }
    

二、独立运行的Slate程序

创建独立Slate程序,先按照第一部的方法创建名为MySlateView的console程序,再在此基础上修改为Slate程序。

  • 将main改为WinMain

    int WINAPI WinMain(_In_ HINSTANCE hInInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR, _In_ int nCmdShow)
    
  • MySlateView.Build.cs 中添加依赖模块Slate,SlateCore,StandaloneRenderer,

    public class MySlateView : ModuleRules
    {
    	public MySlateView(ReadOnlyTargetRules Target) : base(Target)
    	{
    		PrivateIncludePaths.Add(System.IO.Path.Combine(EngineDirectory, "Source/Runtime/Launch/Public"));
    		PrivateIncludePaths.Add(System.IO.Path.Combine(EngineDirectory, "Source/Runtime/Launch/Private"));
    
    		PrivateDependencyModuleNames.Add("Core");
    		PrivateDependencyModuleNames.Add("Projects");
    		PrivateDependencyModuleNames.Add("Slate");
    		PrivateDependencyModuleNames.Add("SlateCore");
    		PrivateDependencyModuleNames.Add("StandaloneRenderer");
        }
    }
    
  • 修改MySlateView.Target.cs 文件中的配置

    • LaunchModuleName = “MySlateView”;
    • bCompileAgainstCoreUObject = true;
    • bCompileAgainstApplicationCore = true;
    • bIsBuildingConsoleApplication = false;
    • 完整代码如下
    using UnrealBuildTool;
    using System.Collections.Generic;
    
    [SupportedPlatforms(UnrealPlatformClass.All)]
    public class MySlateViewTarget : TargetRules
    {
    	public MySlateViewTarget(TargetInfo Target) : base(Target)
    	{
    		Type = TargetType.Program;
    		IncludeOrderVersion = EngineIncludeOrderVersion.Latest;
    		LinkType = TargetLinkType.Monolithic;
    		LaunchModuleName = "MySlateView";
    
    		// Lean and mean
    		bBuildDeveloperTools = false;
    
    		bUseMallocProfiler = false;
    
    		// Editor-only is enabled for desktop platforms to run unit tests that depend on editor-only data
    		// It's disabled in test and shipping configs to make profiling similar to the game
    		bool bDebugOrDevelopment = Target.Configuration == UnrealTargetConfiguration.Debug || Target.Configuration == UnrealTargetConfiguration.Development;
    		bBuildWithEditorOnlyData = Target.Platform.IsInGroup(UnrealPlatformGroup.Desktop) && bDebugOrDevelopment;
    
    		// Currently this app is not linking against the engine, so we'll compile out references from Core to the rest of the engine
    		bCompileAgainstEngine = false;
    		bCompileAgainstCoreUObject = true;
    		bCompileAgainstApplicationCore = true;
    		bCompileICU = false;
    		bIsBuildingConsoleApplication = false;
    	}
    }
    
    
  • 修改cpp文件

    • 添加FSlateApplication初始化代码,并设置渲染器
    • 创建主窗口
    • 创建简单的消息循环
    #include "MySlateViewApp.h"
    
    #include "RequiredProgramMainCPPInclude.h"
    
    #include 
    #include 
    #include 
    
    DEFINE_LOG_CATEGORY_STATIC(LogMySlateView, Log, All);
    
    IMPLEMENT_APPLICATION(MySlateView, "MySlateView");
    
    int WINAPI WinMain(_In_ HINSTANCE hInInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR, _In_ int nCmdShow)
    {
    	GEngineLoop.PreInit(GetCommandLineW());
        // 初始化slate程序,并设置渲染器
    	FSlateApplication::InitializeAsStandaloneApplication(GetStandardStandaloneRenderer());
        // 创建主窗口
    	TSharedPtr<SWindow> MainWindow = SNew(SWindow)
    		.ClientSize(FVector2D(1280, 768))
    		[
    			//SNew(SImage).Image(icon)
    			SNullWidget::NullWidget
    		];
    	FSlateApplication::Get().AddWindow(MainWindow.ToSharedRef());
        
        // 消息循环
    	while (!IsEngineExitRequested())
    	{
    		FSlateApplication::Get().Tick();
    		FSlateApplication::Get().PumpMessages();
    	}
    
    	FSlateApplication::Shutdown();
    	FEngineLoop::AppExit();
    	return 0;
    }
     
    

三、引用

Slate UI Framework

UnrealSlateAppTemplate

UE4 Slate创建独立窗口APP

你可能感兴趣的:(UE引擎学习,ue5,Slate)