Hi everyone! ![]()
I’m developing an application for Hololens 2 with MRTK, aiming to visualize data from MySQL Server in real-time.
I’m encountering an issue where the connection works successfully in Unity’s Play Mode, but fails to connect when I build and release the application. The error message I’m receiving is: “Error: The type initializer for ‘MySql.Data.MySqIClient.Replication.ReplicationManager’ threw an exception.”
Unity version I’m using: 2022.3.24f1
Reference to the MySql Connector Plugin: “Uncle-Uee / mysql-unity”
The screen seen in Unity Play Mode and Hololens :
Script for connecting to the database:
using System;
using MySql.Data.MySqlClient;
using UnityEngine;
using TMPro;
using System.Collections;
namespace MyConnect.MySql
{
public class ConnectToServerGPT : MonoBehaviour
{
public TMP_InputField inputHost, inputUser, inputPassword, inputDatabase; //輸入格gameobject
public TextMeshProUGUI ConsoleText, ConnectInfoText, RotatingspeedText1, TempText1, TimeText1, vibrationText1, HumiText1; //顯示文字gameobject
public TextMeshProUGUI RotatingspeedText2, TempText2, vibrationText2, HumiText2;
private bool ConnectStatus = false; //結束連線按鈕按下為true,反之為false
private void Start()
{
//預設輸入文字
inputHost.text = "";
inputUser.text = "";
inputPassword.text = "";
inputDatabase.text = "";
}
public void StartConnect() //"開始連接"的按鈕按下時,觸發StartConnect()
{
string SubmitHost = inputHost.text;
string SubmitUser = inputUser.text;
string SubmitPassword = inputPassword.text;
string SubmitDatabase = inputDatabase.text;
ConnectStatus = true;
StartCoroutine(Connect(SubmitHost, SubmitUser, SubmitPassword, SubmitDatabase));
}
public void StopConnect() //"關閉連接"的按鈕按下時,觸發StopConnect()
{
{
ConnectStatus = false;
//連結終止,刪除Text內的文字
TempText1.text = "0";
TimeText1.text = "0";
RotatingspeedText1.text = "0";
vibrationText1.text = "0";
HumiText1.text = "0";
TempText2.text = "0";
RotatingspeedText2.text = "0";
vibrationText2.text = "0";
HumiText2.text = "0";
ConnectInfoText.text = "";
ConsoleText.text = "";
}
}
IEnumerator Connect(string Host, string User, string Password, string Database)
{
MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
builder.Server = Host;
builder.UserID = User;
builder.Password = Password;
builder.Database = Database;
builder.Pooling = true; // 啟用連接池
string query = "SELECT * FROM datatable ORDER BY Time DESC LIMIT 1";
bool connectionCount = true;
while (true)
{
yield return new WaitForSeconds(0.5f); //每0.5秒取一次資料
if (!ConnectStatus)
{
Debug.Log("Disconnect.");
ConnectInfoText.text = "Disconnect";
ConsoleText.text = "MySQL - Closed Connection";
break; // 如果狀態為 false,則跳出循環結束連線
}
try
{
using (MySqlConnection connection = new MySqlConnection(builder.ToString()))
{
connection.Open();
if (connectionCount)
{
//連線成功
Debug.Log("MySQL - Opened Connection"); //成功連線
ConsoleText.text = "MySQL - Opened Connection";
ConnectInfoText.text = "Host: " + Host + "\n" + "User: " + User + "\n" + "Database: " + Database;
connectionCount = false;
}
using (MySqlCommand cmd = new MySqlCommand(query, connection))
{
using (MySqlDataReader reader = cmd.ExecuteReader())
{
// 檢查是否有行可讀取
if (reader.HasRows)
{
// 只有一行,直接讀取
reader.Read();
// 根據列名稱或索引讀取數據
DateTime dataTimes = reader.GetDateTime("Time");
float Humi = reader.GetFloat("humidity");
float Temp = reader.GetFloat("temperature");
float rotaspeed = reader.GetFloat("rotatingspeed");
bool vibration = reader.GetBoolean("vibration");
//輸出至面板的Text(TMP)
TimeText1.text = dataTimes + "";
TempText1.text = Temp + " ˚C";
RotatingspeedText1.text = rotaspeed + " RPM";
vibrationText1.text = "" + vibration;
HumiText1.text = Humi + " %";
//輸出至跳出顯示的Text(TMP)
TempText2.text = Temp + " ˚C";
RotatingspeedText2.text = rotaspeed + " RPM";
vibrationText2.text = "" + vibration;
HumiText2.text = Humi + " %";
}
else
{
Debug.Log("MySQL - Opened Connection \nNo rows found.");
ConsoleText.text = "No rows found.";
}
}
}
}
}
catch (Exception ex)
{
if (ex != null)
{
Debug.Log("Error: " + ex.Message);
ConsoleText.text = "Try to connect IPv4: " + Host + "\nError: " + ex.Message;
}
else
{
Debug.Log("Error: " + "ex = null");
ConsoleText.text = "Try to connect IPv4: " + Host + "\nError: " + "ex = null";
}
ConnectStatus = false; // 连接失败时设置 ConnectStatus 为 false
break;
}
yield return null;
}
}
}
}
I am a university student who started using Unity to develop applications for Hololens. Therefore, I’m not very familiar with the settings in Unity yet. I hope someone experienced can point out any potential issues. thanks in advance. ![]()
