找回密码
 立即注册
查看: 361|回复: 2

UE4[C++]添加自定义模块

[复制链接]
发表于 2021-12-10 06:13 | 显示全部楼层 |阅读模式
大家好,我是刘茗。
在项目开发过程中,有时会出现新增模块的需求。
文章所用引擎版本:Unreal 4.24.1
为什么需要自定义模块?

纵观UE4的源码目录,UE4的代码主要有四个部分:


仔细观察,会发现它们都是由大大小小的模块所组成。
在游戏最终发布时有些模块是游戏需要的,有些是不需要的。因此UE4在Unreal Build Tool中引入了模块机制。


通过配置,我们可以很方便的看到这个模块的用途,是作为编辑器的模块,还是作为游戏、客户端、服务器或者是独立程序的一个模块。
一、用编辑器打开项目名.uproject文件

在Modules的下面增加自定义的模块描述
  "Modules": [
    {
      "Name": "YourProject",
      "Type": "Runtime",
      "LoadingPhase": "Default"
    },
    {
      "Name": "YourModule",
      "Type": "Runtime",
      "LoadingPhase": "Default"
    }
  ]在这里可以加入其它配置项,具体配置在
Engine\Source\Runtime\Projects\Public\ModuleDescriptor.h


二、创建模块

游戏模块至少要包含一个头文件、一个C++文件和一个编译文件 (*.Build.cs),因此我们在项目的Source文件夹下创建一个自定义模块的文件夹。
并添加如下内容:

  • 一个叫做Public的文件夹,用来存放头文件[YourModuleName].h
  • 一个叫做Private的文件夹,用来存放源文件[YourModuleName].cpp、预编头文件(PreCompileHeader)[YourModuleName]PrivatePCH.h,可以将模块内通用的头文件发在这个头文件中,可以加快编译。
  • 一个编译文件,[YourModuleName].Build.cs,方括号内为自定义的模块名称。
三、Build文件

打开[YourModuleName].Build.cs,输入如下代码
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;

public class YourModuleName: ModuleRules
{
        public YourModuleName(ReadOnlyTargetRules Target) : base(Target)
        {
                PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
       
                PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });

                PublicIncludePaths.AddRange(new string[] {"YourModuleName/Public"});

                PrivateIncludePaths.AddRange(new string[] {"YourModuleName/Private"});

                // Uncomment if you are using Slate UI
                //PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
               
                // Uncomment if you are using online features
                // PrivateDependencyModuleNames.Add("OnlineSubsystem");

                // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
        }
}四、模块头文件、源文件

#pragma once
//-----------------HEADER----------
#include "ModuleManager.h"

class YourModuleNameClass: 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;
};

//-----------------CPP----------
#include "LoadingScreenModule.h"

void YourModuleNameClass::StartupModule()
{

}

void YourModuleNameClass::ShutdownModule()
{

}

IMPLEMENT_GAME_MODULE(YourModuleNameClass, YourModuleName);
五、重新生成项目

为了防止文件冲突, 删除掉Binaries和Intermediate文件夹之后再点击Uproject右键Generate。
重新生成之后,编译通过就可以看到我们注册的模块了。


六、注意事项

在UE4游戏中,至少要使用 IMPLEMENT_PRIMARY_GAME_MODULE 注册一个模块。其他模块可以使用另一个可选的 IMPLEMENT_GAME_MODULE 方法进行注册。在这里新增的模块并不是做为主模块使用,因此使用IMPLEMENT_GAME_MODULE注册。
出现多个游戏性模块时,我们可以在Target.cs 文件中找到ExtraModuleNames,添加自定义的模块名称。


参考资料

[1]游戏模块
[2]https://www.orfeasel.com/creating-custom-modules/

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
发表于 2021-12-10 06:20 | 显示全部楼层
大佬大佬~这里有个小坑,如果添加的是EditorModule,"LoadingPhase"最好设置成"PreDefault",不然编辑器里相关模块会因为加载顺序出bug
发表于 2021-12-10 06:25 | 显示全部楼层
谢谢补充!  我太菜了[捂脸]
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-8 14:29 , Processed in 0.126658 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表