问题引入
在学习UE4官方教程 ,“塔防游戏“遇到了要在当前模块添加一个新的游戏加载模块
在网上找了些文档后做个步骤总结
1, 首先在项目文件下->Source 添加一个要添加的模块文件夹,如:TowerGameLoadingScreen
2, 在第一步文件夹下添加一个Private文件夹,一个Public文件夹,还有个 <模块名>.Build.cs文件 如:TowerGameLoadingScreen.Build.cs
3, 打开TowerGameLoadingScreen.Build.cs文件添加如下的代码:
// Copyrigth 1998-2020 Epic Games, Inc. ALL Rigths Reserved.
using UnrealBuildTool;
// This moudle must be loaded "PreLoadScreen" in the .uproject file, otherwise it will not hook in time!
public class TowerGameLoadingScreen : ModuleRules
{
public TowerGameLoadingScreen(ReadOnlyTargetRules Target) : base(Target)
{
PrivatePCHHeaderFile = "Public/TowerGameLoadingScreen.h";
PCHUsage = PCHUsageMode.UseSharedPCHs;
PublicDependencyModuleNames.AddRange(
new string[]{
"Core",
"CoreUObject",
"Engine",
}
);
PrivateDependencyModuleNames.AddRange(
new string[] {
"MoviePlayer",
"Slate",
"SlateCore",
"InputCore",
}
);
}
}
该文件里定了这个模块引用的其他模块,预编译的头文件,还可以定义要引用的一些私有的头文件,和公有的头文件
4,Public和Private文件下分别添加一个 <模块名>.h, <模块名>.cpp 如:
TowerGameLoadingScreen.h
TowerGameLoadingScreen.cpp
5,打开上一步建立的.h 和.cpp 文件添加如下代码
TowerGameLoadingScreen.h
// Copyright 1998-2020 Epic Game, Inc. All Rights Reserved.
#pragma once
#include "Engine.h"
#include "Modules/ModuleInterface.h" // 这里要这样引用否则引用不到
/** Module interface for the game`s loading screens */
class ITowerGameLoadingScreenModule : public IModuleInterface
{
public:
/** Kicks off the loading screen for in game loading (not startup) */
virtual void StartInGameLoadingScreen() = 0;
};
TowerGameLoadingScreen.cpp
// Copyright 1998-2020 Epic Games, Inc. All Rights Reserved.
#include "TowerGameLoadingScreen.h"
// This module must be loaded "PreLoadingScreen" in the .uproject file, otherwise it will not hook in time !
class FTowerGameLoadingScreenModule : public ITowerGameLoadingScreenModule
{
public:
virtual void StartupModule() override
{
}
virtual bool IsGameModule() const override
{
return true;
}
virtual void StartInGameLoadingScreen() override
{
}
};
// 这个地方根据官方文档要这样使用,用于UBT来自动搜寻模块,编译添加DLL文件
IMPLEMENT_GAME_MODULE(FTowerGameLoadingScreenModule, TowerGameLoadingScreen)
6, 打开<项目名>.uproject,找到Modules中添加你的模块如:
"Modules": [
{
"Name": "MyTower",
"Type": "Runtime",
"LoadingPhase": "Default",
"AdditionalDependencies": [
"Engine",
"CoreUObject",
"AIModule",
"Slate",
"SlateCore"
]
},
{
"Name" : "TowerGameLoadingScreen",
"Type" : "Runtime",
"LoadingPhase": "PreLoadingScreen"
}
],
7,以上步骤完成并保存后,右击<项目名>.uproject 选择Generate Visual Studio project file
8, 打开Vs就可以看到你新加的模块了,这个时候你进行编译是不会编译你新加的模块的,要在<主项目模块>.Build.cs中添加对这个模块的引用如下:
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
public class MyTower : ModuleRules
{
public MyTower(ReadOnlyTargetRules Target) : base(Target)
{
PrivateDependencyModuleNames.AddRange(
new string[] {
"TowerGameLoadingScreen"
}
);
}
}
在进行编译就可以编译新加的模块了,这个是因为这个模块不是UBT的目标,在主模块进行引用后,UBT就会找到这个模块并进行编译(我猜的_)。