any idea how I can import MySql.Data to Unity properly? Managed to import it to unity using NuGet, but as it’s dependencies need different versions of System.Runtime.CompilerServices.Unsafe I only get it running by disabling version checking causing various random freezes in the IDE. I already tried different versions of MySql.Data, but the conflict occurs for all options.
I’m trying to solve this since 5 days since now, I found differently solutions but most of the threads where from 2017- and not working with current unity versions. There are also applications in the assets store, but with tons of unnecessary overloads or abstraction layers.
Well, I did use the .NET mysql connector in the past without much issues in Unity. Though I rarely used it as it usually doesn’t make much sense to make direct database connnections. A Unity game usually does not ship with an sql server and you never want to open an sql server to the internet. So there’s usually no need for that unless you develop a really rare and specialized piece of software.
Yes, I’m completely with you for 95% of the usecases, but my intention is to make a mirror based multiplayer game with dedicated servers. These servers should have direct database access, the clients should not and will for sure also not receive the configuration files. As API requests to an Rest API are pretty expensive, and I want to place another Redis Cache layer in game as there might really be performance bottlenecks, an nativ MySQL intergration is a must have.
I will check the MySQL Connector in a few minutes, but I fear that this means a lot of refactoring in the existing code…
As far as I can see this actually is the MySql.Data Library, which is not working for me with the latest version using NuGet. May I ask how you installed it?
Well, as I said, the last time I used it was several years ago. Though I just
downloaded the Mono / .NET version of the connector (zip file).
I extracted the v4.5.2 folder and copied it into my Unity test project
Unity complains about two libraries in that folder that I don’t use anyways, so I just deleted them. Specifically “MySql.Web.dll” and “MySql.Data.EntityFramework.dll” as well as their xml and potential meta data files
Finally I quickly created this test script and added a test database to my local mysql server, created a “user” table and run a query. I successfully get the user name and user id from the database:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MySql.Data.MySqlClient;
public class MySQLTest : MonoBehaviour
{
string dbHost = "127.0.0.1";
string dbUserName = "unity";
string sbPassword = "XXXXXX";
string dbName = "unitytest";
void Start()
{
using (var conn = new MySqlConnection($"server={dbHost};uid={dbUserName};pwd={sbPassword};database={dbName}"))
{
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = "select * from user;";
var reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
int id = reader.GetInt32("id");
string name = reader.GetString("name");
Debug.Log("user: " + name + " " + id);
}
}
}
}
}
So I haven’t use this in years but got it working within 10 minutes including the download.
It’s quite possible that you may only need a few files from that folder, though I couldn’t be bothered right now to figure out what the bare minimum dependencies are. Anyways, it worked first try, so it was pretty straight forward.
I don’t know how you’re currently handling your queries. However, that’s the reason you usually want to have your own abstraction layer between your database connection and your application. That simplifies the exchange of the db driver / connector.
Thank you so much, that’s actually working like a charm. I can’t believe how simple that was, after searching days and hours for proper solutions, but as this connector ships it’s dependencies in proper versions it simply works