简介
有时候需要为ue4增加一些额外的功能,比如CustomHID读写
这个时候就需要从外部导入静态库或者动态库(此例子是静态库)
初始工作
新建C++项目,在项目目录创建Lib文件夹并把lib放入此文件夹。如图:
CS文件
打开CS文件并修改
// Fill out your copyright notice in the Description page of Project Settings.
using UnrealBuildTool;
using System.IO;
public class custom_hidue4 : ModuleRules
{
private string ModulePath
{
get { return ModuleDirectory; }
}
private string ThirdPartyPath
{
get { return Path.GetFullPath(Path.Combine(ModulePath, "../../")); }
}
public custom_hidue4(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
PrivateDependencyModuleNames.AddRange(new string[] { });
PublicIncludePaths.Add(Path.Combine(ThirdPartyPath, "Lib"));
//PublicAdditionalLibraries.Add(Path.Combine(ThirdPartyPath,"Lib/", "custom_usbdll.lib"));
PublicAdditionalLibraries.Add(Path.Combine(ThirdPartyPath, "Lib/", "hidapi.lib"));
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}
代码需根据自己情况与需求进行修改
使用方式
演示的是使用蓝图库创建的:
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "CustomHID.generated.h"
/**
*
*/
UCLASS()
class CUSTOM_HIDUE4_API UCustomHID : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "HID")
static int read_usbhid(TArray<uint8> &data);
UFUNCTION(BlueprintCallable, Category = "HID")
static int write_usbhid(TArray<uint8> data);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "CustomHID.h"
unsigned char *read(int* len);
int write(unsigned char *data, int len);
int UCustomHID::read_usbhid(TArray<uint8> &data)
{
int len;
unsigned char *dat = read(&len);
if (dat == nullptr)
return 0;
for (int i = 0; i < len; i++)
data.Add(dat[i]);
return len;
}
int UCustomHID::write_usbhid(TArray<uint8> data)
{
unsigned char *dat = new unsigned char[data.Num()];
for (int i = 0; i < data.Num(); i++)
dat[i] = data[i];
return write(dat, data.Num());
}
其他
例子中用的hidapi.lib在下面
https://blog.csdn.net/weixin_41738734/article/details/103596404