Accessing MySQL database using c# on unity?

前端 未结 6 359
面向向阳花
面向向阳花 2021-01-01 23:06

I have just started using C# and do not know much about it. I am using the 3d game engine called Unity and trying to write a c# script to access the MySQL database that I am

相关标签:
6条回答
  • 2021-01-01 23:42

    Unity3D uses stripped Mono.net which doesn't support database directly. You can write your own DLL in C# .NET which does data access for you. Then add it in your Unity3D project.

    While you are writing your own DLL, make sure when you bring that DLL to your project, you also copy referenced DLL in the same folder. Best approach would be to use Mono.NET.

    0 讨论(0)
  • 2021-01-01 23:52

    You can find a procedure in Unity Wiki with PHP, MySQL and C#/JavaScript.

    It consists of three steps

    1. Create a blank MySQL Database and a table.
    2. Create a PHP server side script (This will connect to the MySQL table, receive data from a Unity script (step 3), and query the database (the examples given are either inserting data or selecting))
    3. Create the Unity Controller Script (This will connect to the PHP script created in step 2)
    0 讨论(0)
  • 2021-01-01 23:57

    I have faced the same problem yesterday and I've found a satisfying solution that works both on PC and Android.

    1: Download .DLL file from here matching your Visual studio project target .NET version (for me 3.5, version 6.9.8.0 worked just fine). If you download a wrong version you will get an error in Unity editor. links to download the file: https://www.dllme.com/dll/files/mysql_data_dll.html or this one: https://downloads.mysql.com/archives/c-net/

    2: Unpack the .DLL file and include it in the project (put it anywhere inside of the Assets folder).

    3: Program your connection to the database ;) here is a short example:

     using System;
     using System.Data;
    
     using MySql.Data;
     using MySql.Data.MySqlClient;
    
     public class Tutorial4
     {
         public static void Main()
         {
             string connStr = "server=localhost;user=root;database=world;port=3306;password=******";
             MySqlConnection conn = new MySqlConnection(connStr);
             try
             {
                 Console.WriteLine("Connecting to MySQL...");
                 conn.Open();
    
                 string sql = "SELECT COUNT(*) FROM Country";
                 MySqlCommand cmd = new MySqlCommand(sql, conn);
                 object result = cmd.ExecuteScalar();
                 if (result != null)
                 {
                     int r = Convert.ToInt32(result);
                     Console.WriteLine("Number of countries in the world database is: " + r);
                 }
    
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
    
             conn.Close();
             Console.WriteLine("Done.");
         }
     }
    
    0 讨论(0)
  • 2021-01-01 23:59

    A late answer, but maybe this will help others.

    I can actually access my server side sql database on my non-game Unity app. In my case, my app controls whether the user serial number true or not. After users entered their serial key my app starts to compare the serial numbers on sql database. You can use this Server Side Highscores Tutorial for your start point. I did the same. Follow these steps then you should be able to acces your sql database on Unity.

    0 讨论(0)
  • 2021-01-01 23:59

    Database (SQLite) Setup for Unity Create new folder

    1. Create new folder under Assets Folder Rename it Plugins.

    2. Copy sqlite3.def and sqlite3.dll into Assets/Plugins in your unity project .You can download these files here http://www.sqlite.org/download.html for windows (Precompiled Binaries for Windows)

    3. Download SQLite Browser http://sourceforge.net/projects/sqlitebrowser/ or http://sqliteadmin.orbmu2k.de/ download SQLite Administrator tool
    4. Create Database in Assets folder in your unity project using SQLite Browser.
    5. Copy System.Data.dll and Mono.Data.Sqlite.dll from **C:\Program Files (x86)\Unity \Editor\Data\Mono\lib\mono\2.0* and paste them in your Assets/Plugins* folder in your unity project.
    6. Add these namespaces using Mono.Data.Sqlite; using System.Data; using System;
    7. string conn= "URI=file:" + Application.dataPath + "/PickAndPlaceDatabase.s3db";

    Replace PickAndPlaceDatabase.s3db with your database name

    under Assets Folder Rename it Plugins .

    void Start () {
    
                 string conn = "URI=file:" + Application.dataPath + "/PickAndPlaceDatabase.s3db"; //Path to database.
                 IDbConnection dbconn;
                 dbconn = (IDbConnection) new SqliteConnection(conn);
                 dbconn.Open(); //Open connection to the database.
                 IDbCommand dbcmd = dbconn.CreateCommand();
                 string sqlQuery = "SELECT value,name, randomSequence " + "FROM PlaceSequence";
                 dbcmd.CommandText = sqlQuery;
                 IDataReader reader = dbcmd.ExecuteReader();
                 while (reader.Read())
                 {
                     int value = reader.GetInt32(0);
                     string name = reader.GetString(1);
                     int rand = reader.GetInt32(2);
    
                     Debug.Log( "value= "+value+"  name ="+name+"  random ="+  rand);
                 }
                 reader.Close();
                 reader = null;
                 dbcmd.Dispose();
                 dbcmd = null;
                 dbconn.Close();
                 dbconn = null;
             }
    

    these is useful link:

    Further SQLite Help Visit : http://www.tutorialspoint.com/sqlite/

    http://wiki.unity3d.com/index.php/Webservices_In_Unity then you should add this library into your C# code :

    using AssemblyCSharp.portal.wwwww.com;
    

    now you can use it:

    using UnityEngine;
    using System.Collections;
    using AssemblyCSharp.portal.wwwww.com;
    
    public class Game5_Player : MonoBehaviour
    {
        public string Logo;
        void Start ()
        {
            crm1 s = new crm1();
            Logo= s.getCompanyLogo ();
        }
    }
    
    0 讨论(0)
  • 2021-01-02 00:07

    You will need to get the C# MySQL driver: http://dev.mysql.com/downloads/connector/net/

    And then you will need to follow the MySQL manual for setting it up and using it. It's a standard ADO.NET driver, so you should be able to follow most any C# SQL tutorials out there for additional help.

    0 讨论(0)
提交回复
热议问题