UE4 手把手教你做插件(1) 从代码引用插件
创始人
2024-05-26 08:16:49
0

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玩家控制器游戏模式作用

然后你会拥有如下的文件结构:

新建工程下的目录:

介绍一下文件结构和调用关系是:

​​​​​​​第三方插件调用第三方库 

3,创建第三方插件的类:

(1)按照图上的步骤来:​​​​​​​

​​​​​​​

​​​​​​​

最后,打开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 pluginFString BaseDir = IPluginManager::Get().FindPlugin("MyThirdPlugin2")->GetBaseDir();// Add on the relative location of the third party dll and load itFString LibraryPath;
#if PLATFORM_WINDOWSLibraryPath = FPaths::Combine(*BaseDir, TEXT("Binaries/ThirdParty/MyThirdPlugin2Library/Win64/ExampleLibrary.dll"));
#elif PLATFORM_MACLibraryPath = FPaths::Combine(*BaseDir, TEXT("Source/ThirdParty/MyThirdPlugin2Library/Mac/Release/libExampleLibrary.dylib"));
#elif PLATFORM_LINUXLibraryPath = FPaths::Combine(*BaseDir, TEXT("Binaries/ThirdParty/MyThirdPlugin2Library/Linux/x86_64-unknown-linux-gnu/libExampleLibrary.so"));
#endif // PLATFORM_WINDOWSExampleLibraryHandle = !LibraryPath.IsEmpty() ? FPlatformProcess::GetDllHandle(*LibraryPath) : nullptr;if (ExampleLibraryHandle){// Call the test function in the third party library that opens a message boxExampleLibraryFunction();}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

5,第三方库的代码修改 及其 编译方法

(1)ExampleLibrary.cpp代码修改

改一个你喜欢的弹窗吧~

EXAMPLELIBRARY_EXPORT void ExampleLibraryFunction()
{
#if defined _WIN32 || defined _WIN64MessageBox(NULL, TEXT("你成功调用了我(* ^ *)~"), TEXT("Third Party Plugin"), MB_OK);
#elseprintf("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

 (2)MyGameModeBase

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,设置世界场景运行游戏

设置游戏模式,并且运行

 

运行结果: 

 

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...