How to add MySQL capabilities to a Unity application

In this short tutorial I’m going to explain how you can add MySQL capabilities to your Unity application. This will allow you to connect to a MySQL database from your application and fill the database with some data (e.g. users’ scores). To do so, you first of all need to obtain a MySQL Connector. Not every version of the Connector, however, will work with Unity. To obtain the Connector do the followiung steps:

  1. Go to https://dev.mysql.com/downloads/connector/net/
  2. Click on the “Looking for previous GA?” link.
  3. Select Version: 6.7.9 (This is the last version that contains the library compiled with the .Net 2.0 Framework)
  4. Select Platform: .Net & Mono
  5. Click on the “Download” button.
  6. Extract the files to a directory of your choice.

Next we will create a new database schema with a table that can store string values. You can do this using MySQL Workbench.

  1. Open MySQL Workbench
  2. Connect to the local database.
  3. Create a new schema in the connected server and call it unity.
  4. Set unity schema as default schema by clicking on it with the right mouse button and selecting “Set as Default Schema”.
  5. Create a new table in the unity schema, call it scores, and define 3 columns: idscores as INT, playername as VARCHAR(45), and playscore as INT.
  6. Set the following flags for the idscores column: Primary Key, Not Null, and Auto Increment.
  7. Set the following flag for the columns playername and playerscore: Not Null.

Now we need a new user who will have privileges to write in and read from the unity schema.

  1. Click in the navigator panel of the MySQL Workbench on the User and Priveleges link.
  2. Click on the button “Add Account”.
  3. Define a username of your choice.
  4. Leave Authentification Type as Standard (I had a problem to connect to the DB from a Linux machine while using other Authentification Type)
  5. Leave Limit to Hosts Matching as %. This will allow you to connect from any machine.
  6. Define a password of your choice.
  7. Leave Account Limits and Administrative Roles settings unchanged.
  8. On the Schema Privileges panel click on the button “Add Entry…”.
  9. Activate the option “Selected schema”, select the unity schema, and click on “Ok”.
  10. Next provides the object rights: SELECT and INSERT for the entry and click on “Apply”.

Finally, we will write a small Unity application that will connect to a MySQL database and write a test string into a table.

  1. Create a new Unity project
  2. Drag-and-Drop the MySQL.Data.dll file into the Assets folder (You are interested in the library that lies in the v2.0 directory)
  3. You have also copy the System.Data.dll file into the Assets folder. If you work on Windows, then you can find the file in: C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0
  4. Next create a new C# script and put the following code into it:

 

 
using UnityEngine; 
using MySql.Data.MySqlClient; 

  public class testSQL : MonoBehaviour { 
    MySqlConnection connection; 

    // Use this for initialization 
    void Start () { 
      SetupSQLConnection(); 
      TestDB(); 
      CloseSQLConnection(); 
    } 

    private void SetupSQLConnection() { 
      if (connection == null) { 
        string connectionString = "SERVER=xxx.xxx.xxx.xxx;" + "DATABASE=unity;" + "UID=unityuser;" + "PASSWORD=yourpassword;"; 
        try { 
          connection = new MySqlConnection(connectionString); 
          connection.Open(); 
        } catch (MySqlException ex) { 
          Debug.LogError("MySQL Error: " + ex.ToString()); 
        } 
      } 
    } 

    private void CloseSQLConnection() { 
      if (connection != null) { 
        connection.Close(); 
      } 
    } 

    public void TestDB() { 
      string commandText = string.Format("INSERT INTO scores (playername, playerscore) VALUES ({0}, {1})", "'megaplayer'", 10); 
      if (connection != null) { 
        MySqlCommand command = connection.CreateCommand(); 
        command.CommandText = commandText; 
        try { 
          command.ExecuteNonQuery(); 
        } catch (System.Exception ex) { 
          Debug.LogError("MySQL error: " + ex.ToString()); 
        } 
      } 
     } 
   } 

Add an empty GameObject to your scene and put the script on it. Now each time you start the game a new entry will be added to your database. You can check it in the MySQL Workbench by clicking with the right mouse button on the table, selecting “Select Rows – Limit 1000”, and clicking in the appeared window on the lightning symbol.

Using MPlayer to Play a Video on a Wall Display

Given:

  • a wall display made of 35 displays
  • 3 display PCs (each PC drives up to 12 displays)
  • each display is configured as a separated X-Screen
  • each display PC is able to connect to another display PC over ssh without a password

Requirements:

  • mplayer
  • socat
  • ssh

Approach:

MPlayer synchronizes master and slave instances by sending UDP packages with a frame number to the port 23867. Thus, we have to configure port forwarding on each  display PC to be able to run multiple slave instances on a single PC. We use socat to achieve that.

$ socat -u UDP4-RECVFROM:23867,fork - | 
tee >(socat -u - UDP4-SENDTO:192.168.1.XXX:24800) 
>(socat -u - UDP4-SENDTO:192.168.1.XXX:24801) 
>(socat -u - uDP4-SENDTO:192.168.1.XXX:24802) 
>(socat -u - uDP4-SENDTO:192.168.1.XXX:24803) 
>(socat -u - uDP4-SENDTO:192.168.1.XXX:24804) 
>(socat -u - uDP4-SENDTO:192.168.1.XXX:24805) 
>(socat -u - uDP4-SENDTO:192.168.1.XXX:24806) 
>(socat -u - uDP4-SENDTO:192.168.1.XXX:24807) 
>(socat -u - uDP4-SENDTO:192.168.1.XXX:24808) 
>(socat -u - uDP4-SENDTO:192.168.1.XXX:24809) 
>(socat -u - uDP4-SENDTO:192.168.1.XXX:24810) 
>(socat -u - uDP4-SENDTO:192.168.1.XXX:24811) 
> /dev/null

Next we start mplayer slaves on each display PC.

$ mplayer -display :0.6 -vf crop=384:204:0:204 -noborder -geometry 1920x1080 -udp-slave -udp-port 24800 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &
$ mplayer -display :0.7 -vf crop=384:204:0:408 -noborder -geometry 1920x1080 -udp-slave -udp-port 24801 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &
$ mplayer -display :0.9 -vf crop=384:204:0:612 -noborder -geometry 1920x1080 -udp-slave -udp-port 24802 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &
$ mplayer -display :0.10 -vf crop=384:204:384:612 -noborder -geometry 1920x1080 -udp-slave -udp-port 24803 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &
$ mplayer -display :0.11 -vf crop=384:204:768:612 -noborder -geometry 1920x1080 -udp-slave -udp-port 24804 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &

We can use ssh to start mplayer slaves remotely on display PCs

$ ssh 10.20.152.32 'mplayer -display :0.4 -vf crop=384:204:384:0 -noborder -geometry 1920x1080 -udp-slave -udp-port 24800 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &'
$ ssh 10.20.152.32 'mplayer -display :0.1 -vf crop=384:204:768:0 -noborder -geometry 1920x1080 -udp-slave -udp-port 24801 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &'
$ ssh 10.20.152.32 'mplayer -display :0.9 -vf crop=384:204:1152:0 -noborder -geometry 1920x1080 -udp-slave -udp-port 24802 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &'
$ ssh 10.20.152.32 'mplayer -display :0.5 -vf crop=384:204:384:204 -noborder -geometry 1920x1080 -udp-slave -udp-port 24803 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &'
$ ssh 10.20.152.32 'mplayer -display :0.3 -vf crop=384:204:768:204 -noborder -geometry 1920x1080 -udp-slave -udp-port 24804 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &'
$ ssh 10.20.152.32 'mplayer -display :0.10 -vf crop=384:204:1152:204 -noborder -geometry 1920x1080 -udp-slave -udp-port 24805 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &'
$ ssh 10.20.152.32 'mplayer -display :0.6 -vf crop=384:204:384:408 -noborder -geometry 1920x1080 -udp-slave -udp-port 24806 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &'
$ ssh 10.20.152.32 'mplayer -display :0.8 -vf crop=384:204:768:408 -noborder -geometry 1920x1080 -udp-slave -udp-port 24807 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &'
$ ssh 10.20.152.32 'mplayer -display :0.11 -vf crop=384:204:1152:408 -noborder -geometry 1920x1080 -udp-slave -udp-port 24808 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &'
$ ssh 10.20.152.33 'mplayer -display :0.5 -vf crop=384:204:1536:0 -noborder -geometry 1920x1080 -udp-slave -udp-port 24800 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &'
$ ssh 10.20.152.33 'mplayer -display :0.6 -vf crop=384:204:1536:204 -noborder -geometry 1920x1080 -udp-slave -udp-port 24801 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &'
$ ssh 10.20.152.33 'mplayer -display :0.7 -vf crop=384:204:1536:408 -noborder -geometry 1920x1080 -udp-slave -udp-port 24802 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &'
$ ssh 10.20.152.33 'mplayer -display :0.9 -vf crop=384:204:1536:612 -noborder -geometry 1920x1080 -udp-slave -udp-port 24803 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &'
$ ssh 10.20.152.33 'mplayer -display :0.10 -vf crop=384:204:1152:612 -noborder -geometry 1920x1080 -udp-slave -udp-port 24804 c01-global-home/video/Sintel.2010.1080p.mkv /dev/null 2>&1 &'

Of course, we can wrap all these commands in a single bash script.

Next, we start mplayer master instance.

$ mplayer -display :0.5 -vf crop=384:204:0:0 -noborder -geometry 1920x1080 -udp-master -udp-ip 192.168.1.255 -udp-port 23867 c01-global-home/video/Sintel.2010.1080p.mkv

Experiment: Concentration / Peripheral Vision – pilot study (closed)

We are looking for volunteers to take part in our experiment. In the study we try to find out if dynamic visual events in peripheral vision have any impact on users’ concentration/attention. If you are interested in participating, please contact me per email or use the form below to provide information on what date and time you are able to come by Experiment duration: 25-30 minutes Location: University of Applied Sciences Bonn-Rhein-Sieg (Campus Sankt Augustin) Reward: 5-Euro Amazon Gutschein Contact: anton.sigitov [at] h-brs.de | M. Sc. Anton Sigitov | Research Associate | Institute of Visual Computing | Bonn-Rhein-Sieg University of Applied Sciences 

  • Subscribe to the Experiment-Newsletter
  • Interested in participating in upcoming experiments? Please subscribe to the newsletter!