金品誠企網(wǎng)站建設(shè)b站2023年免費入口
0,前言
我看的是?技術(shù)宅阿棍兒 的視頻,B站有。
系列視頻:從代碼引用插件_嗶哩嗶哩_bilibili
看不懂,只能邊查資料邊看,講的順序有點亂
1,根據(jù)視頻提示創(chuàng)建第三方插件?
注意:如果只有空白插件的情況,需要你創(chuàng)建一個C++類,就能夠看到很多插件類型了
具體看著:Creating New Plugins - non-content only - missing templates? - #3 by JollyTarkaVFX - C++ - Epic Developer Community Forums?(將這個插件放在了ue引擎或者選擇放在項目下面,建議后者)
2,創(chuàng)建游戲模式
可以參考以下文章,很簡單,就看前面的兩步就OK:?(以下過程會讓你的UE不斷重啟)
???????UE4開發(fā)三:創(chuàng)建游戲模式、角色、控制器_mergerly的博客-CSDN博客_ue4玩家控制器游戲模式作用
然后你會擁有如下的文件結(jié)構(gòu):
新建工程下的目錄:
介紹一下文件結(jié)構(gòu)和調(diào)用關(guān)系是:
???????第三方插件調(diào)用第三方庫?
3,創(chuàng)建第三方插件的類:
(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() {// 將這些代碼復(fù)制到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("你成功調(diào)用了我(* ^ *)~"), 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代碼統(tǒng)一修改
?UE4_PluginAndSlate.Build.cs
UE4_PluginAndSlate.Target.cs
UE4_PluginAndSlateEditor.Target.cs
?(2)MyGameModeBase
MyGameModeBase.h
添加beginplay函數(shù)
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,設(shè)置世界場景運行游戲
設(shè)置游戲模式,并且運行
?
運行結(jié)果:?
?