UE4 新建空白插件连接MySQL 配置
UE4 连接数据库需要 文件:
一个是头文件(MySQL),一个是(第三方库文件)
在UE4 里插件创建空白插件
名字就叫:SimPleMySQL
在SimPleMySQL下新建文件夹:MySQL 里放X64
在SimPleMySQL下的Binanies ->Win64里放x64
在SimPleMySQL下的Source->SimPleMySQL->public下:
写SimpleMySQL.Build.cs配置库文件,就是一些拷贝的操作
// Copyright Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
using System.IO;
public class SimpleMySQL : ModuleRules
{
public string ProjectDirectory
{
get
{
//返回模块路径,UE4 自带的,从当前文件所在的位置跳出去,后面的点就是跳多少次
return Path.GetFullPath(Path.Combine(ModuleDirectory,"../../../../")); //项目路径
}
}
//把东西拷贝到Binaries里面
private void CopyToProjectBinaries(string FilePathName, ReadOnlyTargetRules Target)
{
//项目名称\Binaries\Win64
string BineriesDirectory = Path.Combine(ProjectDirectory, "Binaries", Target.Platform.ToString());
//文件的名字
string Filename = Path.GetFileName(FilePathName);
//路径是否存在
if (!Directory.Exists(BineriesDirectory))
{
//创建路径
Directory.CreateDirectory(BineriesDirectory);
}
//路径加名字
string ProjectFileFullName = Path.Combine(BineriesDirectory, Filename);
if (!File.Exists(ProjectFileFullName))
{
//拷贝过去
File.Copy(FilePathName, ProjectFileFullName, true);
}
RuntimeDependencies.Add(ProjectFileFullName);//添加到实时的依赖里面,这个很重要会支持自动考
}
public SimpleMySQL(ReadOnlyTargetRules Target) : base(Target)
{
//为定义标识符的一个警告
bEnableUndefinedIdentifierWarnings = false;//给警告关掉
//宏定义wind版本
PublicDefinitions.Add("NTDDI_WIN7SP1"); //定义windows版本
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
//ModuleDirectory 表示当前模块的目录
string PluginsDirectory = Path.GetFullPath(Path.Combine(ModuleDirectory, "..", ".."));//只是一个路径
string SQLLibDirectory = Path.Combine(PluginsDirectory, "MySQL"); //MySQL路径
string PlatformString = (Target.Platform == UnrealTargetPlatform.Win64) ? "x64" : "x86";
string MysqlPath = Path.Combine(SQLLibDirectory, PlatformString);//路径加名字
PublicIncludePaths.Add(MysqlPath);
RuntimeDependencies.Add(Path.Combine(MysqlPath, "libmysql.dll")); //动态链接
PublicAdditionalLibraries.Add(Path.Combine(MysqlPath, "libmysql.lib")); //静态链接
CopyToProjectBinaries(Path.Combine(MysqlPath, "libmysql.dll"), Target);
CopyToProjectBinaries(Path.Combine(MysqlPath, "libmysql.lib"), Target);
PublicIncludePaths.AddRange(
new string[] {
// ... add public include paths required here ...
}
);
PrivateIncludePaths.AddRange(
new string[] {
// ... add other private include paths required here ...
}
);
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
// ... add other public dependencies that you statically link with here ...
}
);
PrivateDependencyModuleNames.AddRange(
new string[]
{
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
// ... add private dependencies that you statically link with here ...
}
);
DynamicallyLoadedModuleNames.AddRange(
new string[]
{
// ... add any modules that your module loads dynamically here ...
}
);
}
}
在插件里新建Actor 其他的也行
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class SIMPLEMYSQL_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
public:
UFUNCTION(BlueprintCallable)
void CreateMysqlObjecto();
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
#include "MySQL/mysql.h"
// Sets default values
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AMyActor::CreateMysqlObjecto()
{
MYSQL* mysql = new MYSQL;
mysql_init(mysql);
MYSQL_RES* result;
MYSQL_ROW row;
if (mysql_real_connect(mysql, "localhost", "root", "123456", "java", 3306, NULL, 0)) {
mysql_set_character_set(mysql, "utf8");//这里
int i = mysql_query(mysql, "select * from user");
result = mysql_store_result(mysql);
while ((row = mysql_fetch_row(result)) != NULL)
{
/*std::cout << "id=" << row[0];
std::cout << "name=" << row[1] << "\n";*/
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Yellow, FString::Printf(TEXT("Hello world, this is %s"), UTF8_TO_TCHAR(row[0])), true);//和这里
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Yellow, FString::Printf(TEXT("Hello world, this is %s"), UTF8_TO_TCHAR(row[1])), true);
//GEngine->AddOnScreenDebugMessage(0, 5.0f, FColor::Yellow, TEXT("row= %s"), row[1]);
}
}
else {
/*std::printf("sb");*/
GEngine->AddOnScreenDebugMessage(0, 5.0f, FColor::Yellow, TEXT("sb"));
}
}
把用C++创建的Actor类,搞个Actor 蓝图继承它
调用方法:
值就拿出来了
仅供参考