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 will need some server side script in a Web server (PHP for example) and then you can call those scripts using the WWW class.
Check some PHP+unity tutorial and you will get a good start
You're going to want some kind of datalayer to talk to the database!
Just some method that wants to use the datalayer:
Then our static class that will talk to the database-layer:
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:A simple overview of the flow would be:
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!
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.