【问题标题】:Connect Unity App to mySQL database将 Unity App 连接到 mySQL 数据库
【发布时间】:2017-09-08 23:48:51
【问题描述】:

所以我在 Unity 中有这个应用程序,我正在尝试将它连接到我制作并连接到网站的 mySQL 数据库。我可以通过网站上的表格将内容插入数据库。那是有效的。

接下来我要做的是连接 Unity 应用程序,以便它也可以访问数据库中的内容。我正在用 C# 和 php 编写代码。我希望统一应用程序向网站询问数据库中的某些信息。然后网站应该查看数据库并将一些信息返回给统一应用程序。

这不是一个重复的问题。我已经查看了这里的问题,但仍然无法正常工作。现在我的统一应用程序能够向我的网页发送一条消息,然后我的网页会正确回显。 (我知道这不是我谈到的功能,但我现在只是在测试)。但是,当我尝试从我的网页中获取我的 Unity 应用程序中的响应时,我调试的只是<html>

您可以访问我的网站:http://historicstructures.org/forms.html

这是我的 php 代码:

<html> 

<style type="text/css">
    body {background-color:#666666; color: white;}
</style> 
<body>
<h1 align = "center">
<img src="housebackground.jpg" alt="Mountain View" style="width:97%;height:228px;" ></h1>
<h1 align = "center">Submission Status</h1>
<p align = "center">
<?php


//this is the variable that is being recieved from the unity script
$AuthorName = $_POST["Author"];

//here i am printing it out so that it will be sent back to the unity script 
    // i am also echoing it onto the webpage so that i know it is getting the variable 
    //correctly from the unity script
echo $AuthorName;
header("Access-Control-Allow-Origin: *");
print($AuthorName);

//gets all the variables the user inputted to form
$StructureName = $_POST["StructureName"];
$Author = $_POST["Author"];
$YearBuilt = $_POST["YearBuilt"];
$EraBuilt = $_POST["EraBuilt"];
$YearDestroyed = $_POST["YearDestroyed"];
$EraDestroyed = $_POST["EraDestroyed"];
$Latitude = $_POST["Latitude"];
$Longitude = $_POST["Longitude"];
$Structurelink = "no exist yet";

//checks to make sure the information is in the right format
$isValid = true; 
$errCode = 0; 

if ($Latitude<-90 || $Latitude>90){
    $isValid = false;
    $errCode = 1; 

}

if ($Longitude<-180 || $Longitude>180){
    $isValid = false;
    $errCode = 2; 

}

if ($YearBuilt<-400 || $YearBuilt>400){
    $isValid = false;
    $errCode = 3; 

}

 if ($YearDestroyed<-400 || $YearDestroyed>400){
    $isValid = false;
     $errCode = 4; 

}



//if the informationt the user gave was correct, then insert into database
if ($isValid ==true){

    $servername = "localhost";
    $username = "...";
    $password = "...";
    $dbname = "StructureInfo";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 


    $sql = "INSERT INTO info (ID, StructureName, Author, YearBuilt, EraBuilt, YearDestroyed, EraDestroyed, Latitude, Longitude, StructureLink)
    VALUES ('null','$StructureName','$Author','$YearBuilt','$EraBuilt','$YearDestroyed','$EraDestroyed','$Latitude','$Longitude','$Structurelink')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }


    $conn->close(); 

}


//the user has an error in the information they inputted 
else{
    echo "Your submission was invalid and so it was not submitted. ";

    switch ($errCode) {
    case 1:
        echo "Your latitude is out of bounds.";
        break;
    case 2:
        echo "Your longitude is out of bounds. ";
        break;
    case 3:
        echo "Your year built is out of bounds. ";
        break;
    case 4:
        echo "Your year destroyed is out of bounds. ";
        break;
    default:
        echo "Go back and review your data to make sure it is correct.";
    }
}

?>
</p>

<br><br>
</body>
</html>

这是我的统一代码,它附加到一个按钮,我不知道把它放在哪里,所以我对按钮的 onclick 是这个 js 的主要内容:

#pragma strict

function Start () {

}

function Update () {

}

function GetFromDB(){
     var url = "http://historicstructures.org/action_page_post.php";
     var form = new WWWForm();
     form.AddField( "Author", "Jess" );

     var www = new WWW( url, form );

     // wait for request to complete
     yield www;

     // and check for errors
     if (www.error == null)
     {
         Debug.Log(www.text);
     } else {
     // something wrong!
         Debug.Log("WWW Error: "+ www.error);
     }
}

GetFromDB();

【问题讨论】:

标签: c# php mysql database unity3d


【解决方案1】:

首先,您的代码是 Javascript/Unityscript,但您标记了 C#。我认为您应该使用 C#,因为它比 Javascript/Unityscript 具有更多的功能和支持。

这是附在按钮上的统一代码,我不确定 把它放在哪里,所以我对按钮的 onclick 是这个 js 的主要内容

创建一个 C# 脚本,然后订阅 Button 的 onClick 事件。当按下按钮时,启动将连接到您的数据库的协程。

public Button button;

void OnEnable()
{
    button.onClick.AddListener(() => { StartCoroutine(GetFromDB()); });
}

void OnDisable()
{
    button.onClick.RemoveAllListeners();
}

IEnumerator GetFromDB()
{
    var url = "http://historicstructures.org/action_page_post.php";
    var form = new WWWForm();
    form.AddField("Author", "Jess");

    WWW www = new WWW(url, form);

    // wait for request to complete
    yield return www;

    // and check for errors
    if (String.IsNullOrEmpty(www.error))
    {
        UnityEngine.Debug.Log(www.text);
    }
    else
    {
        // something wrong!
        UnityEngine.Debug.Log("WWW Error: " + www.error);
    }
}

如果您是 C# 新手,this Unity 教程应该可以帮助您入门。您可以找到其他 Unity UI 事件示例here

编辑:

但是,当我尝试在我的 Unity 应用程序中获取响应时 在我的网页上,我调试的只是&lt;html&gt;

我在您原来的问题中没有看到&lt;html&gt;。那是因为 &lt;html&gt; 可以在 stackoverflow 上使用来排列文本。我将您的问题和formatted&lt;html&gt; 编辑为代码以使其显示。

您的代码没有问题。 Unity 只是没有显示从服务器接收到的所有其他数据,因为您的代码中有一个新行。只需单击您在编辑器中看到的&lt;html&gt; 日志,它就会显示来自服务器的所有其他数据。您必须在编辑器中单击该错误才能查看其余数据。

请注意,您当前的脚本将向 Unity 输出错误:

您的提交无效,因此未提交。

那是因为您没有填写所有要求的表格。

应该这样做:

form.AddField("Author", "Jess");
form.AddField("YearDestroyed", "300");
form.AddField("YearBuilt", "300");
form.AddField("Longitude", "170");
form.AddField("Latitude", "60");
form.AddField("StructureName", "IDK");

还有几件事

1.从您的服务器中删除 html 代码。如果您想使用 POST/GET,那不应该存在。如果那里有 html 代码,将很难提取数据。因此,您的代码应该只从&lt;?php 开始并以?&gt; 结束

2。如果您要从服务器接收超过 1 个数据,请使用 json。

在 PHP 端,使用 json_encode 将您的数据转换为 json,然后使用 printecho 发送到 Unity。谷歌json_encode 了解更多信息。

在 Unity C# 端,使用 this post 中的 JsonUtilityJsonHelper 反序列化来自服务器的数据。

3。最后,无需构建错误消息并从服务器输出到 Unity,只需从服务器发送错误代码即可。

在 Unity C# 方面,创建一个将错误代码转换为完整错误消息的类。

【讨论】:

  • 感谢您的回复!!对不起 C# mix up 。我的整个应用程序都是用 C# 编写的,但我在 JS 中找到了一个教程,这就是我使用它的原因。好的,所以我仍然遇到同样的问题,即网页正在接收消息,但来自网站的返回消息仍然没有返回到我的统一应用程序。我使用 wwwforms 的代码有问题吗?我还尝试在应用程序启动时从使用按钮转换为仅运行函数 GetFromDB(),因为最终我会在需要数据库中的某些内容时调用它。
  • 你能编辑你的问题并用你完整的 php 脚本更新它吗?另外,你能提供我你用来做这个的网址吗?我想亲自尝试一下,看看会发生什么。最后,您可以从网络浏览器访问该网址吗?如果你不能,你可能无法从 Unity 中使用。
  • 好的,我刚刚编辑了 php 脚本,这样它就完成了。与统一脚本对话的唯一部分位于顶部,如 cmets 所示。我还将网址添加到上述问题中。是的,它可以从网络浏览器访问。我在 godaddy 托管它。另外,我意识到它很容易受到sql攻击。我会尽快解决这个问题。哈哈,我只是想让它现在就开始工作。
  • 检查我在回答中所做的编辑。
  • 非常感谢!!!我只是尝试将一个变量从我的统一脚本发送到 php 脚本,然后返回它工作!哇,谢谢你的耐心。一个简单的问题是json是必要的吗?如果我只是从 php 脚本中回显信息并用逗号分隔它们然后统一解析它,为什么它不起作用?
猜你喜欢
  • 2012-03-17
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
相关资源
最近更新 更多