Part1. 創建和編譯Dll
VS中創建Visual C++ > Win32 Console Application 工程模板,選擇Dll,並勾上”Empty Project”。
在SolutionExplorer里選中工程,右鍵Add>New Item,選擇C++ File
在新建的文件里輸入測試代碼:
extern "C" __declspec(dllexport) float getCircleArea(float radius)
{
return 3.1416 * (radius*radius);
}
菜單Project>xxxProperties,Configuration Manager,把所有的關於位設置的都設置為x64
保存后Build Solution。
Part2 拷貝到引擎目錄
在工程中找到x64文件夾下的dll. 復制到引擎Plugins目錄,可以自己新建一個子文件夾放置自定義的插件。
這里放在D:\Program Files (x86)\Epic Games\4.13\Engine\Plugins\KenPlugins
Part3 UE4工程中加入Dll
創建一個C++ UE4工程
File>New C++ Class…
選擇Blueprint Function Library作為父類
在生成的.h文件的類定義中加入如下定義:
public: // Blueprint accessible method. UFUNCTION(BlueprintCallable, Category = "Ken Libraries") static float getCircleArea(float radius); 在.cpp中加入以下代碼 typedef float(*_getCircleArea)(float radius); // Declare the DLL function. float UMyBlueprintFunctionLibrary::getCircleArea(float radius) { FString filePath = FPaths::Combine(*FPaths::EnginePluginsDir(), TEXT("KenPlugins/"), TEXT("SampleDll.dll")); // Concatenate the plugins folder and the DLL file. if (FPaths::FileExists(filePath)) { void *DLLHandle; DLLHandle = FPlatformProcess::GetDllHandle(*filePath); // Retrieve the DLL. if (DLLHandle != NULL) { _getCircleArea DLLgetCircleArea = NULL; // Local DLL function pointer. FString procName = "getCircleArea"; // The exact name of the DLL function. DLLgetCircleArea = (_getCircleArea)FPlatformProcess::GetDllExport(DLLHandle, *procName); // Export the DLL function. if (DLLgetCircleArea != NULL) { float out = DLLgetCircleArea(radius); // Call the DLL function, with arguments corresponding to the signature and return type of the function. return out; // return to UE } } } return 1.00f; }
其中UMyBlueprintFunctionLibrary 是類的名稱,*FPaths::EnginePluginsDir()是引擎的插件目錄, TEXT("KenPlugins/")是自定義的文件夾, TEXT("SampleDll.dll")是dll名稱
Part4 藍圖中調用dll
在藍圖的Ken Libraries分類中找到 get circle radius節點即可使用