c# - 使用离线 mySQL 数据库在 Unity for android 中制作游戏

标签 c# php android database unity3d

<分区>

我在做一个琐事,最后有 5 个输入字段用于用户数据,例如姓名、姓氏、电话、文件编号和电子邮件。并用 mySQL 和 phpmyadmin 创建了一个数据库来存储这些数据。当我为 Windows 构建时,一切正常,但我必须为 Android 平板电脑构建,但我无法设法让数据库正常工作,如果有人能帮助我,我会很高兴!

这是我用于数据库管理的 C# 统一脚本:

using System.Collections;
using UnityEngine.UI;
using UnityEngine;

public class GestorBD : MonoBehaviour
{

public InputField txtnombre;
public InputField txtapellido;
public InputField txtci;
public InputField txttel;
public InputField txtmail;

public string nombrenombre;
public string apellidoapellido;
public int cici;
public int tel;
public string mail;

public bool sesionIniciada = false;

/// Respuestas WEB
/// 
/// 200 = datos encontrados 
/// 201 = usuario registrado
/// 
/// 400 = no pudo establecer coneccion
/// 401 = no enconto datos
/// 402 = el usuario ya existe



public void IniciarSesion()
{
    StartCoroutine(Login());
    StartCoroutine(datos());
}
public void Registrarnombre()
{
    StartCoroutine(Registrar());
}

IEnumerator Login()
{
    WWW coneccion = new WWW("http://127.0.0.1/trivia/login.php?nom=" + 
txtnombre.text + "&ape=" + txtapellido.text + "&ci=" + txtci.text + "&tel=" 
+ txttel.text + "&mai=" + txtmail);
    yield return (coneccion);
    if (coneccion.text == "200")
    {
        print("el usuario si existe");
    }
    else if (coneccion.text == "401")
    {
        print("usuario o contrasena incorrectos");
    }
    else
    {
        print("error en la coneccion con la base de datos");
    }
}

IEnumerator datos()
{
    WWW coneccion = new WWW("http://127.0.0.1/trivia/datos.php?nom=" + 
txtnombre.text + "&ape=" + txtapellido.text + "&ci=" + txtci.text + "&tel=" 
+ txttel.text + "&mai=" + txtmail);
    yield return (coneccion);
    if (coneccion.text == "401")
    {
        print("usuario o contrasena incorrectos");
    }
    else
    {
        string[] nDatos = coneccion.text.Split('^');
        if (nDatos.Length != 2)
        {
            print("error en la coneccion");
        }
        else
        {
            nombrenombre = nDatos[0];
            apellidoapellido = nDatos[1];
            cici = int.Parse(nDatos[2]);
            tel = int.Parse(nDatos[3]);
            mail = nDatos[4];
            sesionIniciada = true;
        }
    }
}

IEnumerator Registrar()
{
    WWW coneccion = new WWW("http://127.0.0.1/trivia/registro.php?nom=" + 
txtnombre.text + "&ape=" + txtapellido.text + "&ci=" + txtci.text + "&tel=" 
+ txttel.text + "&mai=" + txtmail.text);
    yield return (coneccion);
    if (coneccion.text == "402")
        Debug.LogError("usuario ya existe!");

    else if (coneccion.text == "201")
    {
        nombrenombre = txtnombre.text;
        apellidoapellido = txtapellido.text;
        sesionIniciada = true;

    }
    else
    {
        Debug.LogError("error en la coneccion con la base de datos");
    }

  }


}

这是我用于登录的 php 文件:

<?php

$servidor  = 'localhost';
$user      = 'root';
$password  = '';
$baseDatos = 'trivia';

$conexion = new mysqli($servidor, $user, $password, $baseDatos);

$nom      = $_GET['nom'];
$ape      = $_GET['ape'];
$ci       = $_GET['ci'];
$tel      = $_GET['tel'];
$mai     = $_GET['mai'];

if (!$conexion)
{
     echo "error";
}
else
{
    $sql  = "SELECT * FROM usuarios WHERE nombre LIKE '$nom' AND apellido 
    LIKE '$ape' AND ci LIKE '$ci' AND tel LIKE '$tel' AND mail LIKE '$mai'";
    $resultado = mysqli_query($conexion, $sql);
    if (mysqli_num_rows($resultado)>0)
    {
      echo "bien";
    }
    else
    {

      echo "mal";
    } 
 }

?>

我的数据 php 文件:

<?php

$servidor  = 'localhost';
$user      = 'root';
$password  = '';
$baseDatos = 'trivia';

$conexion = new mysqli($servidor, $user, $password, $baseDatos);

$nom      = $_GET['nom'];

if (!$conexion)
{
    echo "400";
}
else
{
     $sql  = "SELECT * FROM usuarios WHERE nombre LIKE '$nom'";
     $resultado = mysqli_query($conexion, $sql);
     if (mysqli_num_rows($resultado)>0)
     {
      while ($row = mysqli_fetch_assoc($resultado)){
        echo 

 $row['nombre']."^".$row['apellido']."^".$row['ci']."^".$row['tel']
 ."^".$row['mail'];
      }
    }
    else
    {

      echo "401";
    } 
 }

?>

和我用于注册的 php 文件:

<?php

$servidor  = 'localhost';
$user      = 'root';
$password  = '';
$baseDatos = 'trivia';

$conexion = new mysqli($servidor, $user, $password, $baseDatos);

$nom      = $_GET['nom'];
$ape      = $_GET['ape'];
$ci       = $_GET['ci'];
$tel      = $_GET['tel'];
$mai      = $_GET['mai'];

if (!$conexion)
{
     echo "400";
}
else
{
     $sql  = "SELECT * FROM usuarios WHERE ci LIKE '$ci'";

     $resultado = mysqli_query($conexion, $sql);
     if (mysqli_num_rows($resultado)>0)
     {
       echo "402";
     }
     else
     {
       $sql  = "INSERT INTO usuarios (id, nombre, apellido, ci, tel, mail) 
VALUES (NULL, '$nom', '$ape', '$ci', '$tel', '$mai')";
       $resultado = mysqli_query($conexion, $sql);
       echo "201";
     } 
  }

?>

谢谢!

最佳答案

Localhost 不能在 android 中工作,因为它没有像你的电脑那样设置本地服务器。如果你想存储在本地数据库中,你需要使用类似 sqlite( https://www.sqlite.org ) 的东西。 assetstore 中还有用于 sqlite 的免费 Assets :https://www.assetstore.unity3d.com/en/#!/content/20660

对于远程服务器,使用实际的服务器凭据将使其工作。

关于c# - 使用离线 mySQL 数据库在 Unity for android 中制作游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45759102/

相关文章:

android - 用户和当前进程都没有 android.permission.USE_FINGERPRINT

c# - 在 asp.net 中自定义打印属性

c# - 是否可以隐藏继承的数据成员?

php - 为什么mysql会忽略conf.d中的选项文件?

php - 避免在时间之间插入 sql

java - 以 5 秒、4 秒、3 秒的间隔调用函数

java - Android 图片缓存

c# - 限制 C# 中的泛型类型

C#:如何抑制 UserControl 以将焦点放在第一个子控件上?

int = 0 上的 php switch 语句错误