Unity2020連接Mysql資料庫
- 第一步 下載Mysql資料庫,
- 第二步 下載Connector/Net 5.2.0
- 第三步 在unity創建用戶注冊面板
- 第四步 測驗
第一步 下載Mysql資料庫,
我下載的是8.0.26,下載鏈接mysql下載
第二步 下載Connector/Net 5.2.0
1.我下載的版本為5.2.0,使用其他版本報過好多錯誤,在其他文章中了解到下載5.2.0,嘗試之后,確實可以,
2.下載完成之后,將Mysql.Data.dll檔案匯入unity專案中的plugins檔案夾中,
如圖所示:

第三步 在unity創建用戶注冊面板
1.如圖所示:

2.創建UIRegister腳本,來實作連接資料庫,并存盤資料,(我只測驗了連接,存盤資料庫的代碼自己查詢)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MySql.Data.MySqlClient;
public class UIRegister : MonoBehaviour
{
public InputField userName;
public InputField password;
public InputField confirmPassword;
string str;
public void ConncetMysql()
{
str = "server=127.0.0.1;port=3306;database=game2d;user=root;password=duan521;charset=utf8";
MySqlConnection conn = new MySqlConnection(str);
try
{
conn.Open();
Debug.Log("mysql連接成功!");
}
catch (MySqlException e)
{
Debug.Log(e);
}
}
private void Start()
{
ConncetMysql();
}
public void OnRegisted()
{
if (userName.text==null)
{
MessageBoxManager.Init(this.gameObject);
MessageBoxManager.ShowMessageBox("請輸入用戶名");
}
........
}
}
第四步 測驗
運行unity,彈出連接成功,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/296957.html
標籤:其他
