本篇博文介绍了,如果在UE 中如何使用第三方库,及制作成插件的方法。
DLL 文件是上篇文章中创键的具体的方法见上篇文章。下面开始介绍方法
首先,创建一个空白的 UE5 C++ 项目,然后再创建一个空白内容的插件,如下图所示
修改UeWllApi.uplugin 里面的内容 如下图所示
在 插件的文件夹中 ,添加文件夹及DLL,LIB,.H 文件(注意具体的路径不要错否则可能插件制作不成功)如下图所示
首先在插件中添加一个ThridParty的文件夹(文件夹名字不要错)如下图所示
然后在ThridParty 内再添加一个文件夹(WllApi)如下图所示
然后在 WllApi 里面添加三个文件夹 bin,inc,lib(名字不要错)如下图所示
然后再bin ,lib,分别添加x64 文件夹 inc 里面添加WllApi 文件夹 如下图所示
然后再bin ,lib,的x64文件夹下添加 Debug,Release 文件夹,然后在Debug,Release 里面添加对应的Dll ,lib文件,在inc 里面的WllApi文件夹下添加需要的头文件.如下图所示
在 插件的的Build.cs (UeWllApi.Build.cs)中添加以下代码
// Copyright Epic Games, Inc. All Rights Reserved.
using System.IO;
using UnrealBuildTool;
public class UeWllApi : ModuleRules
{
public UeWllApi(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 ...
"Projects"
}
);
DynamicallyLoadedModuleNames.AddRange(
new string[]
{
// ... add any modules that your module loads dynamically here ...
}
);
LoadPlugins(Target);
}
public void LoadPlugins(ReadOnlyTargetRules Target)
{
bool bSupport = false;
string BinPath = "", LibPath = "";//初始化BIN,LIB路径为空
string PluginPath = Path.Combine("ThridParty", "WllApi");
PrivateIncludePaths.Add(Path.Combine(PluginDirectory,PluginPath,"inc"));
if(Target.Platform==UnrealTargetPlatform.Win64)
{
if(Target.Configuration==UnrealTargetConfiguration.Debug)
{
BinPath = Path.Combine(PluginPath, "bin", "x64", "Debug");
LibPath = Path.Combine(PluginPath, "lib", "x64", "Debug");
}
else
{
BinPath = Path.Combine(PluginPath, "bin", "x64", "Release");
LibPath = Path.Combine(PluginPath, "lib", "x64", "Release");
}
bSupport = true;
}
if(bSupport)
{
PublicAdditionalLibraries.Add(Path.Combine(PluginDirectory, LibPath, "FirstDll.lib"));
PublicDelayLoadDLLs.Add("FirstDll.dll");
RuntimeDependencies.Add(Path.Combine("$(PluginDir)", BinPath, "FirstDll.dll"));
}
}
}
还有要注意添加 “Projects” 模块
具体结果如下图所示
在UeWllApi.h中添加以下代码,如下图所示
public :
static constexpr wchar_t* ModuleName{ L"UeWllApi" };//插件的名字
public:
/** IModuleInterface implementation */
virtual void StartupModule() override;
virtual void ShutdownModule() override;
public:
static FName GetModularFeatureName()
{
static FName FeatureName = FName(ModuleName);
return FeatureName;
}
static inline IModuleInterface& Get()
{
return FModuleManager::LoadModuleChecked<IModuleInterface>(ModuleName);
}
static inline bool IsAvailable()//是否可以获得
{
return FModuleManager::Get().IsModuleLoaded(ModuleName);
}
private:
FString GetLibaryPath();
void* LibraryHandle = nullptr;
在UeWllApi.cpp中添加以下代码,如下图所示
#include "UeWllApi.h"
#include"Misc/MessageDialog.h"//消息框头文件
#include "Interfaces/IPluginManager.h"//插件管理头文件
#define LOCTEXT_NAMESPACE "FUeWllApiModule"
void FUeWllApiModule::StartupModule()
{
FString LibraryPath = GetLibaryPath();
LibraryHandle = LibraryPath.IsEmpty() ? nullptr : FPlatformProcess::GetDllHandle(*LibraryPath);//当前的路径是否为空,不为空的话获取句柄
if (!LibraryHandle)
{
FMessageDialog::Open(EAppMsgType::Ok, LOCTEXT("ThridPartyLibraryError", "Failed to load WllApi Library"));
}
}
void FUeWllApiModule::ShutdownModule()
{
FPlatformProcess::FreeDllHandle(LibraryHandle);//释放句柄
LibraryHandle = nullptr;
}
FString FUeWllApiModule::GetLibaryPath()
{
FString BaseDir = IPluginManager::Get().FindPlugin(ModuleName)->GetBaseDir();//寻找插件
FString LibraryPath;
#if PLATFORM_WINDOWS
# if PLATFORM_64BITS
# if UE_BUILD_DEBUG
LibraryPath = FPaths::Combine(*BaseDir, TEXT("ThridParty/WllApi/bin/x64/Debug/FirstDll.dll"));
# else
LibraryPath = FPaths::Combine(*BaseDir, TEXT("ThridParty/WllApi/bin/x64/Release/FirstDll.dll"));
# endif
# else
# if UE_BUILD_DEBUG
LibraryPath = FPaths::Combine(*BaseDir, TEXT("ThridParty/WllApi/bin/x86/Debug/FirstDll.dll"));
# else
LibraryPath = FPaths::Combine(*BaseDir, TEXT("ThridParty/WllApi/bin/x86/Release/FirstDll.dll"));
# endif
# endif
#endif
return LibraryPath;
}
#undef LOCTEXT_NAMESPACE
IMPLEMENT_MODULE(FUeWllApiModule, UeWllApi)
添加C++类型的Blueprint Function Library,如下图所示,运行时是插件
在UeWllApiFunctionLibrary.h 添加以下代码,如下图所示
public:
UFUNCTION(BlueprintCallable,Category="UeWllApi")
static int GetSum();
在UeWllApiFunctionLibrary.cpp 添加以下代码 如图所示
int UUeWllApiFunctionLibrary::GetSum()
{
return sum(3,4);
}
到此为止插件就制作完成了,只要打包调用就可以了,可以看到在本程序内可以调用结果也正确
打包出来,在其他程序调用也正确,结果如下图所示