当前位置: 首页 > news >正文

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

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

新建工程下的目录:

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

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

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

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

​​​​​​​

​​​​​​​

最后,打开vs,重启就行

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<UThirdLibInvoker>();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<UThirdLibInvoker>();Lib->InvokeLib();
}

5,设置世界场景运行游戏

设置游戏模式,并且运行

 

运行结果: 

 

http://www.lryc.cn/news/10485.html

相关文章:

  • 【Mybatis源码解析】一级缓存和二级缓存源码解析
  • 你知道MES实施的要点吗?
  • 告诉你为什么为什么 SELECT COUNT(*) FROM table 在 InnoDB 引擎中比 MyISAM引擎中的速度慢
  • Redis 命令和Redis key键
  • 如何入侵服务器
  • 在Windows10上安装虚拟机---VMware 17 Pro下载与安装
  • 生命周期函数、组件
  • 蓝桥杯 stm32 PWM 测量频率
  • Docker CPU 资源控制
  • 小红书数据平台:笔记爆文率提升的三大秘诀公式!
  • Spring MVC 之Tomcat启动流程
  • 大疆车载更新产品矩阵,覆盖从主动安全到城区领航的全场景
  • 总结Anisble中的任务执行控制并练习
  • PMP好考吗,有多大的价值?
  • http常用状态码(204,304, 404, 504,502)含义
  • 记录锁,间隙锁,插入意向锁,临键锁兼容关系
  • map相关接口(map接口、HashMap、LinkedHashMap、TreeMap)
  • 抽象工厂模式(Abstract Factory Pattern)
  • Linux驱动学习笔记
  • tarfile — 访问 Tar 压缩文件
  • C++14深度探索之C++基础-共享指针与弱指针使用
  • 【React全家桶】reac组件通信
  • 2023,再转行去做软件测试还有前途吗?
  • Java程序API数据接口
  • 剑指offer 7 数组中和为0的三个数
  • DockerFile
  • Vue-Router 介绍及路由原理分析
  • git代码提交后jenkins构建和自动部署
  • 2023面试题目总结
  • Vue常用指令及声明周期