找回密码
 立即注册
查看: 412|回复: 0

unreal连接MySQL

[复制链接]
发表于 2022-4-13 12:57 | 显示全部楼层 |阅读模式
采用unreal链接第三方库的方式,以连接 mysql 为例子,学习 unreal 链接第三方静态库。
主要参考 unreal 的第三方库插件的写法,在新建的 unreal 项目中,新建插件,选第三方库即可得到模板
环境

windows10
UE4 源码编译
mysql 源码编译
测试准备,适用lib,dll链接的测试

新建TestPlugin的C++空项目,连接的模块为TestPlugin项目工程。运行工程并且新建C++蓝图函数库文件,命名MyBlueprintFunctionLibrary
一、静态库链接

复制mysql源码里的lib文件夹(只有libmysql.lib,libmysql.dll有用,其他不用复制),include文件夹到Source/TestPlugin文件夹下(图路径查找的方便,就放在Build.cs文件同目录下),文件目录如图



打开TestPlugin.Build.cs文件,添加如下
  1. // Copyright Epic Games, Inc. All Rights Reserved.usingSystem.IO;usingUnrealBuildTool;publicclassTestPlugin:ModuleRules{publicTestPlugin(ReadOnlyTargetRules Target):base(Target){
  2.                 PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
  3.        
  4.                 PublicDependencyModuleNames.AddRange(newstring[]{"Core","CoreUObject","Engine","InputCore"});
  5.                 PrivateDependencyModuleNames.AddRange(newstring[]{});// 添加静态库文件
  6.                 PublicIncludePaths.Add(Path.Combine(ModuleDirectory,"include"));
  7.                 PublicAdditionalLibraries.Add(Path.Combine(ModuleDirectory,"lib","libmysql.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}}
复制代码
测试

打开新建的蓝图函数库文件MyBlueprintFunctionLibrary.h/cpp,代码如下
  1. // Fill out your copyright notice in the Description page of Project Settings.#pragmaonce#include"Windows/AllowWindowsPlatformTypes.h"#include"include/mysql.h"#include"Windows/HideWindowsPlatformTypes.h"#include"CoreMinimal.h"#include"Kismet/BlueprintFunctionLibrary.h"#include"MyBlueprintFunctionLibrary.generated.h"/**
  2. *
  3. */UCLASS()classTESTPLUGIN_API UMyBlueprintFunctionLibrary :public UBlueprintFunctionLibrary
  4. {GENERATED_BODY()public:UFUNCTION(BlueprintCallable, Category="MySQL")staticboolTestMySQL();};
复制代码
  1. // Fill out your copyright notice in the Description page of Project Settings.#include"MyBlueprintFunctionLibrary.h"boolUMyBlueprintFunctionLibrary::TestMySQL(){
  2.         MYSQL sqlStruct;mysql_init(&sqlStruct);auto f =mysql_real_connect(&sqlStruct,/*这部分自己填*/);if(f){mysql_query(&sqlStruct,"select * from user;");auto res =mysql_store_result(&sqlStruct);auto row =mysql_fetch_row(res);while(row){
  3.                         GEngine->AddOnScreenDebugMessage(0,20.0f, FColor::Red, row[0]);
  4.                         row =mysql_fetch_row(res);}mysql_close(&sqlStruct);}returnbool(f);}
复制代码
最后编译,打开关卡蓝图进行编写测试




关于UE4与windows系统的冲突

UE4之windows.h冲突
Build之后会出现TEXT重定义的警告,发现是在引用mysql.h文件的时候,TEXT宏定义在unreal和windows库文件都出现了
解决方式如下
  1. #include"Windows/AllowWindowsPlatformTypes.h"#include"include/mysql.h"#include"Windows/HideWindowsPlatformTypes.h"
复制代码
参考

github上别人的mysql插件
C++程序调用mysql数据库

UE4学习笔记–连接MySQL数据库(C++)

这个学习笔记教程的应用好像是mysql安装版的。搜索MySQL Connector的作用,会发现里面的API跟用mysql源码配置的C++工程一样
详解UE4静态库与动态库的导入与使用
UE4链接第三方库(lib和dll)
unreal自建的第三方库插件

<div id="marketingBox" class="marketing-box"><div class="marketing-content">


与50位技术专家面对面


20年技术见证,附赠技术全景图

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2025-5-7 15:03 , Processed in 0.143712 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表