Hello everyone, in this post i pretend to show how to use a external file(xml) with texts or values in your game. I made the main script in c#. The intention is to show a conversation when you comes close to a object. Let's Work!
This is the code of the xml file which i call gamexmldata.xml:
Ok this is our data, now we need to assign these values to variables or arrays.
with this method we're created a Array with the all level node info inside and to get this values you need just do this:
So that's it. Here you can download the fully functioning project. There are many possibilities, this is only one way, I hope you enjoyed it, give your comments!
This is the code of the xml file which i call gamexmldata.xml:
Level 1 (xml) Try comes close to all four objects. (xml) Very Good! (xml) Level 2 (xml) Try comes close to all four objects, again. (xml) Congratulations! (xml)
Ok this is our data, now we need to assign these values to variables or arrays.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
public class LoadXmlData : MonoBehaviour // the Class
{
public TextAsset GameAsset;
static string Cube_Character = "";
static string Cylinder_Character = "";
static string Capsule_Character = "";
static string Sphere_Character = "";
List<Dictionary<string,string>> levels = new List<Dictionary<string,string>>();
Dictionary<string,string> obj;
void Start()
{ //Timeline of the Level creator
GetLevel();
}
public void GetLevel()
{
XmlDocument xmlDoc = new XmlDocument(); // xmlDoc is the new xml document.
xmlDoc.LoadXml(GameAsset.text); // load the file.
XmlNodeList levelsList = xmlDoc.GetElementsByTagName("level"); // array of the level nodes.
foreach (XmlNode levelInfo in levelsList)
{
XmlNodeList levelcontent = levelInfo.ChildNodes;
obj = new Dictionary<string,string>(); // Create a object(Dictionary) to colect the both nodes inside the level node and then put into levels[] array.
foreach (XmlNode levelsItens in levelcontent) // levels itens nodes.
{
if(levelsItens.Name == "name")
{
obj.Add("name",levelsItens.InnerText); // put this in the dictionary.
}
if(levelsItens.Name == "tutorial")
{
obj.Add("tutorial",levelsItens.InnerText); // put this in the dictionary.
}
if(levelsItens.Name == "object")
{
switch(levelsItens.Attributes["name"].Value)
{
case "Cube": obj.Add("Cube",levelsItens.InnerText);break; // put this in the dictionary.
case "Cylinder":obj.Add("Cylinder",levelsItens.InnerText); break; // put this in the dictionary.
case "Capsule":obj.Add("Capsule",levelsItens.InnerText); break; // put this in the dictionary.
case "Sphere": obj.Add("Sphere",levelsItens.InnerText);break; // put this in the dictionary.
}
}
if(levelsItens.Name == "finaltext")
{
obj.Add("finaltext",levelsItens.InnerText); // put this in the dictionary.
}
}
levels.Add(obj); // add whole obj dictionary in the levels[].
}
}
}
with this method we're created a Array with the all level node info inside and to get this values you need just do this:
string lvlName = "";
levels[actualLevel-1].TryGetValue("name",out lvlName);
string tutorial = "";
levels[actualLevel-1].TryGetValue("tutorial",out tutorial);
levels[actualLevel-1].TryGetValue("Cube",out Cube_Character);
levels[actualLevel-1].TryGetValue("Cylinder",out Cylinder_Character);
levels[actualLevel-1].TryGetValue("Capsule",out Capsule_Character);
levels[actualLevel-1].TryGetValue("Sphere",out Sphere_Character);
string finaltext = "";
levels[actualLevel-1].TryGetValue("finaltext",out finaltext);
So that's it. Here you can download the fully functioning project. There are many possibilities, this is only one way, I hope you enjoyed it, give your comments!

thank you. I'll try.
ReplyDeletegot it
ReplyDeleteCan you not do this in Javascript? I just get XmlDocument does not denote a valid type. (not found)
ReplyDeleteNevermind.... import System.Xml for javascript. (im new sorry)
Deletethanks for the example
ReplyDeletecool blog
ReplyDeleteHi, I'm trying to understand the script example but I'm confused by something: the name "gamexmldata.xml", which is the xml file itself is never mentioned in the XML reader proc. So how can it know that you mean to load that file?
ReplyDeletei'm using public TextAsset GameAsset; you need to put the file there in the inspector.
Delete