UE4 新建空白插件连接MySQL 配置

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 蓝图继承它
调用方法:

在这里插入图片描述
值就拿出来了
在这里插入图片描述

仅供参考

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Li~蒙一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值