Database Connections in Unity

Asked

Viewed 1,119 times

5

I am developing a mobile game using Unity 3D and need to have a local database on the device. Which database to use? It has how to use Mysql, Sqlite. Can anyone help me?

1 answer

2

Hello I am usually using Mysql, the command I used for a while I use and I think good, because you can use a back end language in case I used PHP.

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

Code in C#.

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 returns several strings having the data written in HTML, in case I passed the ID field as post in the part of:

 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 to let me know, in case I wanted to see the manual is here: https://docs.unity3d.com/ScriptReference/WWWForm.html

The advantage of using the back end and not using a C# command is that people cannot steal their BD connection data (reverse engineered), the data is more secure. Ai just apply the security in the back end against hack, (sql Injection, etc...), in the front end put an Obfuscator to hinder those who want to clone their game through reverse engineering.

If you prefer you can use the programming language you want and with the framework you prefer for the back end.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.