在UE插件中使用动态链接库
一.在工程中创建和使用动态链接库的方法在,我的上一篇随笔中已经介绍过了
https://www.cnblogs.com/hanabc12345/p/17686826.html
二.使用之前的三个动态库文件,在插件中使用使用
优点是:动态链接库内容放入插件,可以跟工程分离,可以给任何工程使用
1.新建C++项目MyPlugin,新建一个空地图后面调试时用,打开插件,新建插件,选择第三方库模板,命名为M,然后更新项目,用C++打开,编译无问题
2.将myDll1.dll拷贝到...\MyPlugin\Plugins\M\Binaries\ThirdParty\MLibrary\Win64文件夹下
将myDll1.lib拷贝到...\MyPlugin\Plugins\M\Source\ThirdParty\MLibrary\x64\Release文件夹下
将TesLlib.h拷贝到...\MyPlugin\Plugins\M\Source\M文件夹下
3.更改CS文件,打开...\MyPlugin\Plugins\M\Source\ThirdParty\MLibrary文件夹下MLibrary.Build.cs修改代码
using System.IO; using UnrealBuildTool; public class MLibrary : ModuleRules { public MLibrary(ReadOnlyTargetRules Target) : base(Target) { Type = ModuleType.External; if (Target.Platform == UnrealTargetPlatform.Win64) { // Add the import library PublicAdditionalLibraries.Add(Path.Combine(ModuleDirectory, "x64", "Release", "myDll1.lib")); // Delay-load the DLL, so we can load it from the right place first PublicDelayLoadDLLs.Add("myDll1.dll"); // Ensure that the DLL is staged along with the executable RuntimeDependencies.Add("$(PluginDir)/Binaries/ThirdParty/MLibrary/Win64/myDll1.dll"); } } }
删除不需要的代码,将win64下对应的名称改为自己的。
4.修改..\MyPlugin\Plugins\M\Source\M文件夹下M.Build.cs文件,增加一些内容,根据自己需要修改
PublicDependencyModuleNames.AddRange( new string[] { "Core", "CoreUObject", "Engine", "MLibrary", "Projects" // ... add other public dependencies that you statically link with here ... }
5.修改M.h文件,(我觉得这步可以省略)
#pragma once #include "Modules/ModuleManager.h" class FMModule : public IModuleInterface { public: /** IModuleInterface implementation */ virtual void StartupModule() override; virtual void ShutdownModule() override; void* ExampleLibraryHandle; //private: /** Handle to the test dll we will load */ };
6.修改M.cpp文件,这步很重要
#include "M.h" #include "Core.h" #include "Modules/ModuleManager.h" #include "Interfaces/IPluginManager.h" #include "MLibrary/ExampleLibrary.h" #include "M/TesLlib.h" #define LOCTEXT_NAMESPACE "FMModule" void FMModule::StartupModule() { // This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module // Get the base directory of this plugin FString BaseDir = IPluginManager::Get().FindPlugin("M")->GetBaseDir(); // Add on the relative location of the third party dll and load it FString LibraryPath; #if PLATFORM_WINDOWS LibraryPath = FPaths::Combine(*BaseDir, TEXT("Binaries/ThirdParty/MLibrary/Win64/myDll1.dll")); #endif // PLATFORM_WINDOWS ExampleLibraryHandle = !LibraryPath.IsEmpty() ? FPlatformProcess::GetDllHandle(*LibraryPath) : nullptr; if (ExampleLibraryHandle) { // Call the test function in the third party library that opens a message box //ExampleLibraryFunction(); } else { FMessageDialog::Open(EAppMsgType::Ok, LOCTEXT("ThirdPartyLibraryError", "Failed to load example third party library")); } } void FMModule::ShutdownModule() { // This function may be called during shutdown to clean up your module. For modules that support dynamic reloading, // we call this function before unloading the module. // Free the dll handle FPlatformProcess::FreeDllHandle(ExampleLibraryHandle); ExampleLibraryHandle = nullptr; } #undef LOCTEXT_NAMESPACE IMPLEMENT_MODULE(FMModule, M)
7.然后更新编译,成功!后面在UE中新建C++类继承蓝图函数库
然后在.h文件中新建
public: UFUNCTION(BlueprintCallable,Category="My Library") static int BFL_Add(int a,int b);
在.cpp文件中新建
#include "MyBlueprintFunctionLibrary.h" #include "M/TesLlib.h" int UMyBlueprintFunctionLibrary::BFL_Add(int a, int b) { TesLlib* testlib = new TesLlib(); int result = testlib->Add(a, b); delete testlib; return result; }
8.编译成功后,在UE之前新建的地图中打开关卡蓝图,写入下图内容,打印出正确结果,表明成功!
三.参考文献
1.UE5.1保姆级创建第三方插件库使用DLL,以及踩坑笔记_ue 插件开发_烟水寻常的博客-CSDN博客