Hi, I would like to implement in my game in the “new game” scene, something that ask you to tell your name, your age and something more and then create a character that has those attributes (that they are going to be showed with GUI.Label).
I know that the use is with XML but I would like to have an example or a tutorial that uses that, only for see how it use it.
And yes I rode a little bit of XML Serialization
EDIT: the game will be for MAC and PC. I don’t want anything about webplayer things.
using System.IO;
using System.Xml.Serialization;
using UnityEngine;
public class SerObject {
private string name;
public string Name {
get { return name; }
set { name = value; }
}
}
public class test : MonoBehaviour {
void Start () {
SerObject so = new SerObject ();
so.Name= "test";
XmlSerializer ser = new XmlSerializer (typeof (SerObject));
TextWriter textWriter = new StreamWriter(@"test.xml");
ser.Serialize(textWriter, so);
textWriter.Close ();
}
}
Why not just save data in a normal text file as plan text?
sample.txt
name=John Doe
age=18
somethingelse=whatever
myscript.js
var url : String = "http://www.mywebsite.com/datafiles/";
var name : String;
var age : int;
var something : String;
function load(filename : String)
{
var w : WWW = new WWW(url + filename);
yield w;
fields = w.text.Split("\n"[0]);
argName = fields[0].Split("="[0]);
name = argName[1];
argAge = fields[1].Split("="[0]);
age = parseint(argAge[1]);
argSomething = fields[2].Split("="[0]);
something = argSomething[1];
}
even simpler…
Even simpler…
save data as “John;18;Something”
var w : WWW = new WWW(url + filename);
yield w;
fields = w.text.Split(";"[0]);
name = fields[0];
age = parseint(fields[1]);
something = fields[2];
Not being sarcastic or anything, just asking cause I have NEVER been able to figure out what makes XML any more useful than a plain text file. To me XMS seems like more work than it’s worth… So just asking.
But you are saying Desktop and don’t want any Webplayer code ??? Not really sure what you are trying to say. If you are saying that the game will be played offline only and you don’t want to store the data on the web then forget all of the above and just do this:
function save()
{
playerPrefs.SetString("name",name);
playerPrefs.SetInt("age",age);
playerPrefs.SetString("something",something);
}
function load()
{
name = playerPrefs.SetString("name","Default player");
age = playerPrefs.SetInt("age",18);
something = playerPrefs.SetString("something","something");
}
I would like to use xml because i will not only have 4 things. I would have there a lot of things that i thing a txt couldn’t make as clear as I want.
I bougth zerano’s rpg toolkit and it has the xml system that i want, but he uses is for web version (it can be used for standlone version, but it have things of web and i don’t like that for my game). Also it has a lot of things on the xml and i would like to start with xml with something easy.
For example, it would be nice only a project that has one scene that ask you the name, the age and something else like the current life. Then other scene that ask you which player do you want to load and then LOADS the info of this character that is saved in the xml.
Then you can create like 5 different characters and then in a scene load the one you want and load his info.
You will have to put some effort in it, i’m not going to create that for you.
The script i posted will serialize the SerObject to XML. You can add anything you want in the SerObject class that you want serialized.
There are alot of tutorials and examples to find about the subject on the net.