UE4 中的Module与插件

一、新建模块(Module)

(1)在编辑器中打开项目名.uproject文件,可以并按格式添加新模块

{
	"FileVersion": 3,
	"EngineAssociation": "4.15",
	"Category": "",
	"Description": "",
	"Modules": [
		{
			"Name": "UE4CookBook",
			"Type": "Runtime",
			"LoadingPhase": "Default",
			"AdditionalDependencies": [
				"Engine"
			]
		},
		{
			"Name": "MyModule",
			"Type": "Runtime",
			"LoadingPhase": "Default",
			"AdditionalDependencies": [
				"Engine"
			]
		}
	]
}


(2)在Source文件夹下创建新模块名命名的文件夹

UE4 中的Module与插件_第1张图片

(3)文件夹下新建文件MyModule.Build.cs , MyModule.h , MyModule.cpp文件,以及Public和Private文件夹用来存放该模块所用到的头文件和cpp文件

UE4 中的Module与插件_第2张图片

MyModule.Build.cs

using UnrealBuildTool;

public class MyModule : ModuleRules
{
    public MyModule(TargetInfo Target)
    {
        //Public module names that this module uses.
        //In case you would like to add various classes that you're going to use in your game
        //you should add the core,coreuobject and engine dependencies.
        //Don't forget to add your project's name as a dependency (GDBlogPost in my case)
        PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "UE4CookBook" });

        //The path for the header files
        PublicIncludePaths.AddRange(new string[] { "MyModule/Public" });

        //The path for the source files
        PrivateIncludePaths.AddRange(new string[] { "MyModule/Private" });
    }
}

MyModule.h

#pragma once

#include "ModuleManager.h"

DECLARE_LOG_CATEGORY_EXTERN(MyModule, All, All);

class FMyModule : public IModuleInterface
{
public:

	/* This will get called when the editor loads the module */
	virtual void StartupModule() override;

	/* This will get called when the editor unloads the module */
	virtual void ShutdownModule() override;
};

MyModule.cpp

#include "MyModule.h"

DEFINE_LOG_CATEGORY(MyModule);

#define LOCTEXT_NAMESPACE "FMyModule"

void FOrfeasModule::StartupModule()
{
	UE_LOG(OrfeasModule, Warning, TEXT("MyModule has started!"));
}

void FOrfeasModule::ShutdownModule()
{
	UE_LOG(OrfeasModule, Warning, TEXT("MyModule has shut down"));
}

#undef LOCTEXT_NAMESPACE

IMPLEMENT_MODULE(FMyModule, MyModule)

(4)右键.uproject文件,选择生成VS项目文件

UE4 中的Module与插件_第3张图片

(5)打开项目名.sln文件进行编译

(6)打开项目,便可以在窗口/开发者工具/模块下找到新建的模块,新建C++代码时也可选择新建的模块


二、新建插件(Plugin)

(1)选择插件-新建插件,选择插件类型Blank,填写插件名称,创建插件

UE4 中的Module与插件_第4张图片

(2)创建完成后在项目文件夹下会生成Plugins文件夹,里面存放着插件相关的.Build.cs文件,.h文件和.cpp文件

(3)在插件下新建类,需在创建时选择对应的插件目录,创建完成后在plugin文件夹中自动创建.h和.cpp文件

UE4 中的Module与插件_第5张图片

三、引用Module

创建完模块或者插件后,在项目.Build.cs文件中,一定要记得添加模块名

UE4 中的Module与插件_第6张图片

你可能感兴趣的:(虚幻引擎)