Database Connections in Unity

5

I'm developing a mobile game using Unity 3D and need to have a local database on the device. Which database to use? You have to use MySQL, SQLite. Can anyone help me?

    
asked by anonymous 04.10.2017 / 22:53

1 answer

1

Hello, I'm usually using Mysql, the command I used for some time I use and I think it's good, because you can use a backend language in case I used PHP.

The command consists of returning an HTML string and you transform into a vector where you can retrieve the data. In case you have to separate the data by a string, in case I chose p * "asterisk".

C # code.

var form = new WWWForm();
        //aqui você adiciona como método post o campo ID
        form.AddField("id", id);
        WWW w = new WWW("http://localhost/teste.php", form);
        yield return w;   
        if (w.error != null) {
            print(w.error); //if there is an error, tell us
        } 
        else {
            formText = w.text; //here we return the data our PHP told us
            stringToEdit = formText;
            string[] temp = w.text.Split("*".ToCharArray());
            stringToEdit = w.text;

            nome_pers = temp[0];
            STR = int.Parse(temp[1]);
            AGI = int.Parse(temp[2]);
            DEX = int.Parse(temp[3]);
            INT = int.Parse(temp[4]);
            VIT = int.Parse(temp[5]);
            CAR = int.Parse(temp[6]);
}

In PHP or in the language you wanted, you make the request in the database and return several strings by typing in the data in the HTML, in case I passed the ID field as a post in the

 form.AddField("id", id);
    WWW w = new WWW("http://localhost/teste.php", form);
    yield return w;   

And the php code did so:

<?php
include 'conectar.php';
mysql_query("SET NAMES 'utf8'");
$id = mysql_real_escape_string($_POST['id']);
$id = substr($id, 0,-7); // retorna "d"


$sql = "SELECT 'id_conta' , 'id_personagem' , 'id_raça' , 'str' , 'agi' , 'dex' , 'int' , 'vit' , 'car' , 'nome' , partida.ordem, partida.id_partida
FROM 'peronagem'
INNER JOIN partida ON ( id_personagem = partida.id_pe )
WHERE id_personagem = '$id'";
$query = mysql_query($sql);


while($row = mysql_fetch_array($query)) { 

    echo $row['nome'].'*';
    echo $row['str'].'*';
    echo $row['agi'].'*';
    echo $row['dex'].'*';
    echo $row['int'].'*';
    echo $row['vit'].'*';
    echo $row['car'].'*';
    echo $row['ordem'].'*';
    echo $row['id_conta'].'*';
    echo $row['id_raça'].'*';
}

? >

I made this code a long time ago so if you have a better way let me know if you wanted to see the manual is here: link

The advantage of using the back end and not using a C # command is that people can not steal your connection data to the DB, so the data is safer. Ai just apply security in the back end against hack.

    
06.10.2017 / 21:22