UE4中使用第三方库Ⅱ
UE4使用第三方库Ⅱ
针对我几百年前写的一篇文章:UE4使用第三方库 看看真的很Low,想删了重写,却很舍不得,对不起这么骚的文风,大家看完笑笑也就算过去了.
写之前,有一个自认为很重要的观念不得不先说,那就是编程模块化,插件化.这么做的好处是,移植性好,项目清晰地清澈见底,易维护.
我们的目的是什么:把第三方库制作成UE4的一个模块,像虚幻4插件那样,可移植到各个项目中.我要用了,就拿过来,不想用了直接删除.通俗一点来说,第三库就是那种召之即来挥之即去的货色,耦合性低.
新建一个虚幻四项目,我用的是4.20版本的引擎,依次点击新建项目>C++>名称为TestPlugin的工程文件.然后我们要新建一个第三库的插件,点击编辑>插件>新插件>选空白,打个比方我想做一个OpenCV的第三方库,那么我把这个插件起名就叫OpenCV吧.
然后点创建插件.
打开我们刚创建好的插件(项目>Plugins>OpenCV),新建一个文件夹叫ThirdParty,用来存放Opencv,也就是第三方的C++库,如图所示:
打开ThirdParty文件夹,再创建一个文件夹,命名为OpenCV,表示是OpenCV的C++库. 然后在OpenCV文件夹内新建几个文件夹,命名为Win64,Win32...等等表示库是32位的还是64位的,基本上现在都是64位了,在Win64,和Win32下分别创建三个文件夹Bin,Include,Lib,如图所示:
Win32也是如此:
如果更多平台,也类似这样添加,接下来的步骤就是吧OpenCV的C++库依次填入Bin,Include,Lib中,如图:
库已经准备完成了,这个插件库文件夹架构是非常合理的,严丝合缝,不用质疑,虽然我无法解释.
VS中打开Plugins/OpenCV/OpenCV.uplugin,把"Type": "Developer",改成"Type": "Runtime",,如图:
为什么这样改,百度一下就知道.
打开Plugins/OpenCV/Source/OpenCV.Build.cs
添加引用空间:
添加私有函数:
然后底部写入:
完整代码参考:
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
using System.IO;
public class OpenCV : ModuleRules
{
//第三方库的路径
private string ThirdPartyPath
{
get { return Path.GetFullPath(Path.Combine(ModuleDirectory, "../../ThirdParty")); }
}
public OpenCV(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
PublicIncludePaths.AddRange(
new string[] {
// ... add public include paths required here ...
}
);
PrivateIncludePaths.AddRange(
new string[] {
// ... add other private include paths required here ...
}
);
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
// ... add other public dependencies that you statically link with here ...
}
);
PrivateDependencyModuleNames.AddRange(
new string[]
{
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
// ... add private dependencies that you statically link with here ...
}
);
DynamicallyLoadedModuleNames.AddRange(
new string[]
{
// ... add any modules that your module loads dynamically here ...
}
);
//第三方OpenCV的C++库
string OpenCVPath = Path.Combine(ThirdPartyPath, "OpenCV");
if (Target.Platform == UnrealTargetPlatform.Win64)//64位平台
{
string OpenCVIncludePath = Path.Combine(OpenCVPath, "Win64", "Include");
string OpenCVLibPath = Path.Combine(OpenCVPath, "Win64", "Lib");
PublicIncludePaths.Add(OpenCVIncludePath);
PublicLibraryPaths.Add(OpenCVLibPath);
PublicAdditionalLibraries.Add("opencv_world341.lib");
PublicAdditionalLibraries.Add("opencv_world341d.lib");
}
else if (Target.Platform == UnrealTargetPlatform.Win32)//32位平台
{
//32位的OpenCV库,仿照上面Win64的自己填写
}
//else if (Target.Platform == UnrealTargetPlatform.Mac)
//{
// //Mac平台的,暂时管不到这个平台
//}
}
}
然后到这里,我们的第三方库OpenCV插件就制作完成了 ,牛逼之处,不言而喻.
接下来,我们在需要用到该插件的项目,也就是我们的TestPlugin中,加入这个模块.
打开Games/TestPlugin/Source/TestPlugin/TestPlugin.Build.cs,如图:
在PublicDependencyModuleNames.AddRange中添加"OpenCV"模块
然后在需要用到OpenCV库的地方,添加头文件:
//opencv头文件
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
哦,对了,还要把Win64/Bin里面的文件全部拷贝到项目Binaries/Win64/目录下(32位同理),项目运行时要用,这是动态库.
编译通过,结果如下:
文笔早已没有愤青时候的骚气,真讨厌一本正经的自己.