UE4中调用DLL并提供蓝图接口

好久不写文章啦,主要最近工作略忙,一直木有抽出时间来研究Unreal。
当然啦,也不会亏待大家啦。这次带来的教程有点重磅。。。。
我们讨论如何在Unreal的蓝图中调用我们预定义好的DLL。本教程分为两大部分:

一、如何创建基于C++的DLL库

方法很简单,创建工程时选择Visual C++>Win32,创建的工程名称为 CreateAndLinkDLLTut。之后在工程引导中选择DLL和空工程即可。创建完成之后添加新类。此处命名为CreateAndLinkDLL。工程目录结构如下图:
UE4中调用DLL并提供蓝图接口_第1张图片
之后将下面的代码分别粘贴到.h和.cpp文件中,代码如下:
#pragma once  

#define DLL_EXPORT __declspec(dllexport)	//shortens __declspec(dllexport) to DLL_EXPORT

#ifdef __cplusplus		//if C++ is used convert it to C to prevent C++'s name mangling of method names
extern "C"
{
#endif

	bool DLL_EXPORT getInvertedBool(bool boolState);
	int DLL_EXPORT getIntPlusPlus(int lastInt);
	float DLL_EXPORT getCircleArea(float radius);
	char DLL_EXPORT *getCharArray(char* parameterText);
	float DLL_EXPORT *getVector4(float x, float y, float z, float w);

#ifdef __cplusplus
}
#endif


#pragma once

#include "string.h"
#include "CreateAndLinkDLLFile.h"


//Exported method that invertes a given boolean.
bool getInvertedBool(bool boolState)
{
	return bool(!boolState);
}

//Exported method that iterates a given int value.
int getIntPlusPlus(int lastInt)
{
	return int(++lastInt);
}

//Exported method that calculates the are of a circle by a given radius.
float getCircleArea(float radius)
{
	return float(3.1416f * (radius * radius));
}

//Exported method that adds a parameter text to an additional text and returns them combined.
char *getCharArray(char* parameterText)
{
	char* additionalText = " world!";

	if (strlen(parameterText) + strlen(additionalText) + 1 > 256)
	{
		return "Error: Maximum size of the char array is 256 chars.";
	}

	char combinedText[256] = "";

	strcpy_s(combinedText, 256, parameterText);
	strcat_s(combinedText, 256, additionalText);

	return (char*)combinedText;
}

//Exported method that adds a vector4 to a given vector4 and returns the sum.
float *getVector4(float x, float y, float z, float w)
{
	float* modifiedVector4 = new float[4];

	modifiedVector4[0] = x + 1.0F;
	modifiedVector4[1] = y + 2.0F;
	modifiedVector4[2] = z + 3.0F;
	modifiedVector4[3] = w + 4.0F;

	return (float*)modifiedVector4;
}

点击保存后,设置BuildSetting为Release和x64生成项目,拿到Project/x64/Release下的dll文件(笔者的为CreateAndLinkDLLTut.dll)

二、在Unreal中调用

创建Unreal工程,基于C++的BasicCode模板(笔者的工程名称为CreateAndLinkDLLProj)。创建成功后会自动打开VS和UnrealEditor。此时需要在工程的根目录下创建Plugins/MyTutorialDLLs文件夹并将第一步中生成的DLL粘贴到该目录下(后面会在蓝图节点中用到该路径和DLL名称)
之后创建C++类,父类为BlueprintFunctionLibrary(这样我们可以直接在蓝图中调用),笔者的类名为CreateAndLinkDLLTut。分别粘贴下面的.h和.cpp代码到新建的类中。

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "CreateAndLinkDLLTutBFL.generated.h"

/**
 * 
 */
UCLASS()
class CREATEANDLINKDLLPROJ_API UCreateAndLinkDLLTutBFL : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

public:

	UFUNCTION(BlueprintCallable, Category = "My DLL Library")
		static bool importDLL(FString folder, FString name);


	UFUNCTION(BlueprintCallable, Category = "My DLL Library")
		static bool importMethodGetInvertedBool();

	UFUNCTION(BlueprintCallable, Category = "My DLL Library")
		static bool importMethodGetIntPlusPlus();

	UFUNCTION(BlueprintCallable, Category = "My DLL Library")
		static bool importMethodGetCircleArea();

	UFUNCTION(BlueprintCallable, Category = "My DLL Library")
		static bool importMethodGetCharArray();

	UFUNCTION(BlueprintCallable, Category = "My DLL Library")
		static bool importMethodGetVector4();


	UFUNCTION(BlueprintCallable, Category = "My DLL Library")
		static bool getInvertedBoolFromDll(bool boolState);

	UFUNCTION(BlueprintCallable, Category = "My DLL Library")
		static int getIntPlusPlusFromDll(int lastInt);

	UFUNCTION(BlueprintCallable, Category = "My DLL Library")
		static float getCircleAreaFromDll(float radius);

	UFUNCTION(BlueprintCallable, Category = "My DLL Library")
		static FString getCharArrayFromDll(FString parameterText);

	UFUNCTION(BlueprintCallable, Category = "My DLL Library")
		static FVector4 getVector4FromDll(FVector4 vector4);


	UFUNCTION(BlueprintCallable, Category = "My DLL Library")
		static void freeDLL();
	
	
	
};

// Fill out your copyright notice in the Description page of Project Settings.

#include "CreateAndLinkDLLProj.h"
#include "CreateAndLinkDLLTutBFL.h"

typedef bool(*_getInvertedBool)(bool boolState); // Declare a method to store the DLL method getInvertedBool.
typedef int(*_getIntPlusPlus)(int lastInt); // Declare a method to store the DLL method getIntPlusPlus.
typedef float(*_getCircleArea)(float radius); // Declare a method to store the DLL method getCircleArea.
typedef char*(*_getCharArray)(char* parameterText); // Declare a method to store the DLL method getCharArray.
typedef float*(*_getVector4)(float x, float y, float z, float w); // Declare a method to store the DLL method getVector4.

_getInvertedBool m_getInvertedBoolFromDll;
_getIntPlusPlus m_getIntPlusPlusFromDll;
_getCircleArea m_getCircleAreaFromDll;
_getCharArray m_getCharArrayFromDll;
_getVector4 m_getVector4FromDll;

void *v_dllHandle;

#pragma region Load DLL

// Method to import a DLL.
bool UCreateAndLinkDLLTutBFL::importDLL(FString folder, FString name)
{
	FString filePath = *FPaths::GamePluginsDir() + folder + "/" + name;

	if (FPaths::FileExists(filePath))
	{
		v_dllHandle = FPlatformProcess::GetDllHandle(*filePath); // Retrieve the DLL.
		if (v_dllHandle != NULL)
		{
			return true;
		}
	}
	return false;	// Return an error.
}
#pragma endregion Load DLL

#pragma region Import Methods

// Imports the method getInvertedBool from the DLL.
bool UCreateAndLinkDLLTutBFL::importMethodGetInvertedBool()
{
	if (v_dllHandle != NULL)
	{
		m_getInvertedBoolFromDll = NULL;
		FString procName = "getInvertedBool";	// Needs to be the exact name of the DLL method.
		m_getInvertedBoolFromDll = (_getInvertedBool)FPlatformProcess::GetDllExport(v_dllHandle, *procName);
		if (m_getInvertedBoolFromDll != NULL)
		{
			return true;
		}
	}
	return false;	// Return an error.
}

// Imports the method getIntPlusPlus from the DLL.
bool UCreateAndLinkDLLTutBFL::importMethodGetIntPlusPlus()
{
	if (v_dllHandle != NULL)
	{
		m_getIntPlusPlusFromDll = NULL;
		FString procName = "getIntPlusPlus";	// Needs to be the exact name of the DLL method.
		m_getIntPlusPlusFromDll = (_getIntPlusPlus)FPlatformProcess::GetDllExport(v_dllHandle, *procName);
		if (m_getIntPlusPlusFromDll != NULL)
		{
			return true;
		}
	}
	return false;	// Return an error.
}

// Imports the method getCircleArea from the DLL.
bool UCreateAndLinkDLLTutBFL::importMethodGetCircleArea()
{
	if (v_dllHandle != NULL)
	{
		m_getCircleAreaFromDll = NULL;
		FString procName = "getCircleArea";	// Needs to be the exact name of the DLL method.
		m_getCircleAreaFromDll = (_getCircleArea)FPlatformProcess::GetDllExport(v_dllHandle, *procName);
		if (m_getCircleAreaFromDll != NULL)
		{
			return true;
		}
	}
	return false;	// Return an error.
}

// Imports the method getCharArray from the DLL.
bool UCreateAndLinkDLLTutBFL::importMethodGetCharArray()
{
	if (v_dllHandle != NULL)
	{
		m_getCharArrayFromDll = NULL;
		FString procName = "getCharArray";	// Needs to be the exact name of the DLL method.
		m_getCharArrayFromDll = (_getCharArray)FPlatformProcess::GetDllExport(v_dllHandle, *procName);
		if (m_getCharArrayFromDll != NULL)
		{
			return true;
		}
	}
	return false;	// Return an error.
}

// Imports the method getVector4 from the DLL.
bool UCreateAndLinkDLLTutBFL::importMethodGetVector4()
{
	if (v_dllHandle != NULL)
	{
		m_getVector4FromDll = NULL;
		FString procName = "getVector4";	// Needs to be the exact name of the DLL method.
		m_getVector4FromDll = (_getVector4)FPlatformProcess::GetDllExport(v_dllHandle, *procName);
		if (m_getVector4FromDll != NULL)
		{
			return true;
		}
	}
	return false;	// Return an error.
}

#pragma endregion Import Methods

#pragma region Method Calls

// Calls the method getInvertedBoolFromDll that was imported from the DLL.
bool UCreateAndLinkDLLTutBFL::getInvertedBoolFromDll(bool boolState)
{
	if (m_getInvertedBoolFromDll != NULL)
	{
		bool out = !bool(m_getInvertedBoolFromDll(boolState)); // Call the DLL method with arguments corresponding to the exact signature and return type of the method.
		return out;
	}
	return boolState;	// Return an error.
}

// Calls the method m_getIntPlusPlusFromDll that was imported from the DLL.
int UCreateAndLinkDLLTutBFL::getIntPlusPlusFromDll(int lastInt)
{
	if (m_getIntPlusPlusFromDll != NULL)
	{
		int out = int(m_getIntPlusPlusFromDll(lastInt)); // Call the DLL method with arguments corresponding to the exact signature and return type of the method.
		return out;
	}
	return -32202;	// Return an error.
}

// Calls the method m_getCircleAreaFromDll that was imported from the DLL.
float UCreateAndLinkDLLTutBFL::getCircleAreaFromDll(float radius)
{
	if (m_getCircleAreaFromDll != NULL)
	{
		float out = float(m_getCircleAreaFromDll(radius)); // Call the DLL method with arguments corresponding to the exact signature and return type of the method.
		return out;
	}
	return -32202.0F;	// Return an error.
}

// Calls the method m_getCharArrayFromDLL that was imported from the DLL.
FString UCreateAndLinkDLLTutBFL::getCharArrayFromDll(FString parameterText)
{
	if (m_getCharArrayFromDll != NULL)
	{
		char* parameterChar = TCHAR_TO_ANSI(*parameterText);

		char* returnChar = m_getCharArrayFromDll(parameterChar);

		return (ANSI_TO_TCHAR(returnChar));
	}
	return "Error: Method getCharArray was probabey not imported yet!";	// Return an error.
}

// Calls the method m_getVector4FromDll that was imported from the DLL.
FVector4 UCreateAndLinkDLLTutBFL::getVector4FromDll(FVector4 vector4)
{
	if (m_getVector4FromDll != NULL)
	{
		float* vector4Array = m_getVector4FromDll(vector4.X, vector4.Y, vector4.Z, vector4.W);

		return FVector4(vector4Array[0], vector4Array[1], vector4Array[2], vector4Array[3]);
	}
	return FVector4(-32202.0F, -32202.0F, -32202.0F, -32202.0F);	// Return an error.
}
#pragma endregion Method Calls


#pragma region Unload DLL

// If you love something  set it free.
void UCreateAndLinkDLLTutBFL::freeDLL()
{
	if (v_dllHandle != NULL)
	{
		m_getInvertedBoolFromDll = NULL;
		m_getIntPlusPlusFromDll = NULL;
		m_getCircleAreaFromDll = NULL;
		m_getCharArrayFromDll = NULL;
		m_getVector4FromDll = NULL;

		FPlatformProcess::FreeDllHandle(v_dllHandle);
		v_dllHandle = NULL;
	}
}
#pragma endregion Unload DLL


上面的类中定义了蓝图接口函数,可以加载和卸载DLL,并且提供了引入和调用DLL内部函数的方法。之后我们可以在蓝图中调用。创建蓝图类(父类可以是Actor)。之后按照下图连接来测试即可。
UE4中调用DLL并提供蓝图接口_第2张图片
将新创建的蓝图类拖动到场景中运行,结果如下,证明我们已经成功在蓝图中调用了DLL中的方法。
UE4中调用DLL并提供蓝图接口_第3张图片
这是非常简单的调用DLL,其中涉及的知识点比较多(如封装DLL的注意点,如何在C++中动态加载DLL并调用其中的方法,如何在C++中声明可以在蓝图中调用的函数,BlueprintFunctionLibrary的用法等等),希望读者能够认真阅读。

你可能感兴趣的:(UE4_Blueprint,UE4)