使用unity(C#)在Web服务器上保存/加载xml文件 [英] save/load xml file on a web server using unity (C#)

查看:150
本文介绍了使用unity(C#)在Web服务器上保存/加载xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为学校构建在线测试系统,其中问题和答案存储在学校网站服务器上的xml文件中,问题是我不知道如何将xml文件保存在服务器上,并且我该如何回电? 到目前为止,我的密码

I am building an online tests system for a school, where the questions and answers are saved in an xml file on the school website server, the problem is i don't know how can i save the xml file on the server and how can i call it back ? My Code till now

 using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System.Xml.Serialization;
    using System.IO;
    using System;
    public class XMLSerializer : MonoBehaviour {
        public string studentname;

        public void fff() {

            string path = "d:/" + "studentanswer"+ ".xml";
            Debug.Log (path);
            XmlSerializer serializer = new XmlSerializer (typeof(Level1));

            Level1 ak47 = new Level1{ Items=new [] {new mainitems { Question = "a", Answer = "2" },new mainitems { Question="b",Answer="5"}}};

            StreamWriter writer = new StreamWriter(path);
            serializer.Serialize(writer, ak47);
            writer.Close();

        }

    }

推荐答案

您将需要某种数据层与数据库对话!

You're going to want some kind of datalayer to talk to the database!

只是一些想要使用数据层的方法:

// This would probably be your fff() method.
public void SaveXml(string xmlData) {
    StartCoroutine(DataLayer.SaveXml(xmlData));
}

然后我们的静态类将与数据库层通信:

using UnityEngine;
static class DataLayer
{
    public static string url = "http://www.yourdomain.com/DatabaseLayer.php";

    public static IEnumerator SaveXml(string xmlData)
    {
        WWWForm wwwForm = new WWWForm();
        wwwForm.AddField("action", "save");
        wwwForm.AddField("xmldata", xmlData);

        WWW www = new WWW(url, wwwForm);

        // Wait until the request has been sent and a response from the server been recieved:
        yield return www;

        if (www.error == null) {
            // Success! Debug.Log what the server displays
            Debug.Log(www.text);
        }
    }

,然后是DatabaseLayer.php(我们以前在url中引用过)(不必是php,它可以是您选择的任何一种可以接收POST并与数据库对话的语言.设置非常简单),没有安全性的简单示例:

and then the DatabaseLayer.php (that we're previously referencing in url) (doesn't have to be php, it can be any language of your choice that can recieve a POST and talk to the database. Php is just very simple to set up) A quick example without security of how it could look like:

$con = new mysqli("your.host","your_user","your_pwd","your_db");
if ($con->connect_errno)
{
    echo "Failed to connect to db: " . $con->connect_error;
}


// If xmlData has been sent and the POST parameter "action" == "save"
if ($_POST['xmlData'] && $_POST['action'] == "save") 
{
    $xmlData = $_POST['xmlData'];
    $query = 'insert into YourTable (xmlDataColumnName) values ("' . $xmlData . '")';


    if ($res = $con->query($query)) {
        echo 'Level saved';
    } else {
        echo $con->error;
    }
}

对流程的简单概述如下:

A simple overview of the flow would be:

Unity MonoBehaviour游戏脚本->静态数据层类<-> Php<->数据库

Unity MonoBehaviour Game Scripts --> Static Datalayer class <--> Php <--> Database

如果您想获得有关如何使用协程的更多信息和示例,以及在MonoBehaviour中完成协程时获取信息的信息(在上面的概述中将箭头"从静态数据层类添加回Unity MonoBehaviour游戏脚本中)在我写的回调

If you want further information and examples on how to use Coroutines and getting information when the coroutine is done in the MonoBehaviour (adding the "arrow" back from Static datalayer class to Unity MonoBehaviour Game Scripts in the overview above) check this little guide on callbacks I wrote!

关于从数据库中获取信息,这是同一回事,而不仅仅是在php层中插入数据库,而是从数据库中选择并打印它,这将使我们在Static DataLayer类中接收到该信息在www.text中. www.text是您所请求的网站上显示的所有内容.

Regarding getting information from the database, it's the same thing but instead of just inserting in the database in the php-layer, you select it from the database and print it, which will make us recieve that information in the Static DataLayer Class in the www.text. www.text is everything that is displayed on the website you're requesting.

这篇关于使用unity(C#)在Web服务器上保存/加载xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆