UE4 手把手教你做插件(1) 从代码引用插件 |
您所在的位置:网站首页 › 漫画第三方插件怎么用 › UE4 手把手教你做插件(1) 从代码引用插件 |
0,前言
我看的是 技术宅阿棍儿 的视频,B站有。 系列视频:从代码引用插件_哔哩哔哩_bilibili 看不懂,只能边查资料边看,讲的顺序有点乱 1,根据视频提示创建第三方插件注意:如果只有空白插件的情况,需要你创建一个C++类,就能够看到很多插件类型了 具体看着:Creating New Plugins - non-content only - missing templates? - #3 by JollyTarkaVFX - C++ - Epic Developer Community Forums (将这个插件放在了ue引擎或者选择放在项目下面,建议后者) 2,创建游戏模式可以参考以下文章,很简单,就看前面的两步就OK: (以下过程会让你的UE不断重启) UE4开发三:创建游戏模式、角色、控制器_mergerly的博客-CSDN博客_ue4玩家控制器游戏模式作用 然后你会拥有如下的文件结构: 新建工程下的目录: 介绍一下文件结构和调用关系是: 第三方插件调用第三方库 最后,打开vs,重启就行,现在应该编译不过的,需要修改MyThirdPlugin2.Build.cs的代码,朝后面翻翻能找到~ 4,第三方插件的代码修改 (1)修改ThirdLibInvoker类的代码ThirdLibInvoker.h class MYTHIRDPLUGIN2_API UThirdLibInvoker : public UObject { GENERATED_BODY() void* ExampleLibraryHandle; public: ~UThirdLibInvoker(); void InvokeLib(); };ThirdLibInvoker.cpp (这里其实是将MyThirdPlugin2.cpp的代码拷贝过来,憋看到就害怕了~) // Fill out your copyright notice in the Description page of Project Settings. #include "ThirdLibInvoker.h" #include "Core.h" #include "Modules/ModuleManager.h" #include "Interfaces/IPluginManager.h" #include "MyThirdPlugin2Library/ExampleLibrary.h" UThirdLibInvoker::~UThirdLibInvoker() { FPlatformProcess::FreeDllHandle(ExampleLibraryHandle); ExampleLibraryHandle = nullptr; } void UThirdLibInvoker::InvokeLib() { if (ExampleLibraryHandle == nullptr) { // Get the base directory of this plugin FString BaseDir = IPluginManager::Get().FindPlugin("MyThirdPlugin2")->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/MyThirdPlugin2Library/Win64/ExampleLibrary.dll")); #elif PLATFORM_MAC LibraryPath = FPaths::Combine(*BaseDir, TEXT("Source/ThirdParty/MyThirdPlugin2Library/Mac/Release/libExampleLibrary.dylib")); #elif PLATFORM_LINUX LibraryPath = FPaths::Combine(*BaseDir, TEXT("Binaries/ThirdParty/MyThirdPlugin2Library/Linux/x86_64-unknown-linux-gnu/libExampleLibrary.so")); #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")); } } } (2)修改MyThirdPlugin2的代码MyThirdPlugin2.h 删掉:void * exemplexxxx(具体啥名字忘记了) 加上:class UThirdLibInvoker * Lib; MyThirdPlugin2.cpp void FMyThirdPlugin2Module::StartupModule() { // 将这些代码复制到ThirdLibInvoker.cpp里面去 Lib = NewObject(); Lib->InvokeLib(); } void FMyThirdPlugin2Module::ShutdownModule() { //删掉这里面的代码 } (3)MyThirdPlugin2.Build.cs修改添加CoreUObject 改一个你喜欢的弹窗吧~ EXAMPLELIBRARY_EXPORT void ExampleLibraryFunction() { #if defined _WIN32 || defined _WIN64 MessageBox(NULL, TEXT("你成功调用了我(* ^ *)~"), TEXT("Third Party Plugin"), MB_OK); #else printf("Loaded ExampleLibrary from Third Party Plugin sample"); #endif } (2) 编译方式1,VS新建一个工程叫MyThirdLibPluginLibrary,我放在了这里:UE4_PluginAndSlate\Plugins\MyThirdPlugin2\Source\ThirdParty\MyThirdPlugin2Library\ExampleLibrary\MyThirdLibPluginLibrary 2,工程中添加ExampleLibrary.cpp, ExampleLibrary.h两个文件 3,修改MyThirdLibPluginLibrary工程属性(点击工程,右键选择属性): 找到这两个文件的路径,修改输出目录为这两个文件的路径,如下图 然后点击右上角配置管理器,改为release: 4,将生成的dll拷贝到编辑器寻找的路径下面 我们可以看到ThirdLibInvoker.cpp代码里面是通过这句话来加载第三方库的,编辑器只会朝这个路径下寻找ExampleLibrary.dll,因此需要将新生成的ExampleLibrary.dll拷贝过去 LibraryPath = FPaths::Combine(*BaseDir, TEXT("Binaries/ThirdParty/MyThirdPlugin2Library/Win64/ExampleLibrary.dll")); 4,游戏模块的代码修改 (1).cs代码统一修改UE4_PluginAndSlate.Build.cs UE4_PluginAndSlate.Target.cs UE4_PluginAndSlateEditor.Target.cs MyGameModeBase.h 添加beginplay函数 class UE4_PLUGINANDSLATE_API AMyGameModeBase : public AGameModeBase { GENERATED_BODY() protected: virtual void BeginPlay() override; };MyGameModeBase.cpp void AMyGameModeBase::BeginPlay() { Super::BeginPlay(); UThirdLibInvoker* Lib = NewObject(); Lib->InvokeLib(); } 5,设置世界场景运行游戏设置游戏模式,并且运行 运行结果: |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |