UE4 添加模块

1.在工程的Source目录下创建对应的ModuleName.build.cs

这里根据需要配置后你的Include 目录 或者需要使用系统的其他模块

// Fill out your copyright notice in the Description page of Project Settings.

using UnrealBuildTool;

public class TestModule : ModuleRules
{
	public TestModule(ReadOnlyTargetRules Target) : base(Target)
	{
        PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

        PublicIncludePaths.AddRange(new string[] { "TestModule/Public" });
        PrivateIncludePaths.AddRange(new string[] { "TestModule/Public" });
        PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
        PublicDependencyModuleNames.AddRange(new string[] { "MyProject" });

        PrivateDependencyModuleNames.AddRange(new string[] { });


    }
}

2.创建Public Private文件夹 在里面添加对应的.h .cpp文件

TestModule.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"

#include "Modules/ModuleInterface.h"
#include "Modules/ModuleManager.h"

DECLARE_LOG_CATEGORY_EXTERN(TestModule, All, All) //为了使用log

class FTestModule :public IModuleInterface
{
public:
	virtual void StartupModule() override;
	virtual void ShutdownModule() override;
};
TestModule.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "TestModule.h"


IMPLEMENT_GAME_MODULE(FTestModule, TestModule); //注册你想要的的模块类对应的名称 必须

DEFINE_LOG_CATEGORY(TestModule) //log

#define LOCTEXT_NAMESPACE "TestModule"

void FTestModule :: StartupModule()
{
	UE_LOG(TestModule, Warning, TEXT("TestModule Start"));
}

void FTestModule::ShutdownModule()
{
	UE_LOG(TestModule, Warning, TEXT("TestModule Stop"));
}
#undef LOCTEXT_NAMESPACE

3.配置工程.uproject文件 这里需要注意 LoadingPhase 必须为PostEngineInit 否则使用GUnrealEd的话会为空

{
	"FileVersion": 3,
	"EngineAssociation": "4.22",
	"Category": "",
	"Description": "",
	"Modules": [
		{
			"Name": "MyProject",
			"Type": "Runtime",
			"LoadingPhase": "Default"
		},
		{
			"Name": "TestModule",  //定义的模块名称
			"Type": "Editor",    //模块类型
			"LoadingPhase": "PostEngineInit", //加载时机
			"AdditionalDependencies": [
				"Engine"
			]
		}
	]
}

4.配置.target.cs

// Fill out your copyright notice in the Description page of Project Settings.

using UnrealBuildTool;
using System.Collections.Generic;

public class MyProjectTarget : TargetRules
{
	public MyProjectTarget(TargetInfo Target) : base(Target)
	{
		Type = TargetType.Game;
 
		ExtraModuleNames.AddRange( new string[] { "MyProject" } );
        if (bBuildEditor)
        {
            ExtraModuleNames.AddRange(new string[] { "TestModule" });
        }
    }
}
发布了144 篇原创文章 · 获赞 15 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/maxiaosheng521/article/details/99632576