栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

Unity使用Nuget包连接Mysql8.0

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity使用Nuget包连接Mysql8.0

数据库测试

1.确保数据库可以正常连接,通过数据库连接软件测试

安装Nuget包

安装Mysql 的Nuget 包,

https://github.com/GlitchEnzo/NuGetForUnity/releases

选择一个Release 的 unitypackage,安装进Unity

项目调整为Mono 使用.Net4.x

取消勾选 Assembly Version Validation

打开Unity Nuget manager,搜索Mysql安装

第一个即为最新版本的 mysql驱动 8.0.28

安装完成后,Assets下会多出一个Packages文件夹,里面即mysql需要用到的dll

测试

此时即可通过代码连接Mysql

参考代码:

using MySql.Data.MySqlClient;
using UnityEngine;
using UnityEngine.UI;

public class DBTest : MonoBehaviour
{
    public Button DbButton;
    public string server = "";
    public string userid = "";
    public string password = "";
    public string database = "";
    public string port = "3306";
 
    // Start is called before the first frame update
    private void Start()
    {
        DbButton.onClick.AddListener(Test);
    }

    #region 建立MySql数据库连接

    /// 
    /// 建立数据库连接.
    /// 
    /// 返回MySqlConnection对象
    private MySqlConnection GetMysqlConnection()
    {
        string M_str_sqlcon = string.Format("server={0};user id={1};password={2};database={3};port={4};", server, userid, password, database, port);
        MySqlConnection myCon = new MySqlConnection(M_str_sqlcon);
        return myCon;
    }

    #endregion 建立MySql数据库连接

    private void Test()
    {
        MySqlConnection mysqlcon = this.GetMysqlConnection();
        mysqlcon.Open();
        try
        {
            bool isOK = mysqlcon.Ping();

            if (isOK)
            {
                Debug.LogError("数据库正常");
            }
            else
            {
                Debug.LogError("数据库错误");
            }
        }
        catch (System.Exception e)
        {
            Debug.LogError("数据库错误: " + e.Message);
        }
    }
}

输入mysql 对应数据,拖入一个Button,即可测试

参考项目:
https://github.com/euphoriaer/UnityUseMysql/tree/main

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/900523.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号