Hello World!


Hello World!

Hello everyone, I'm creating this blog with the purpose of sharing my and your ideas with all and good self-taught .To all hungry for game development, I open the doors of this blog to explain their ideas, critique, add and unite to the large community of development of Games with this powerful and easy to use Game Engine, Unity 3D. I'm a eternal noob, professional self-taught and i hope to show that even those with little programming experience can develop their own games with Unity 3D easily because of the plethora of references available.

Follow Tweeter

Newsreel

Loading...

empty space


Sunday, February 27, 2011

XML - Loading data from a xml file.

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:


  
    Level 1 (xml)
    Try comes close to all four objects. (xml)
     Hi, I'm a cube! (xml) 
     Hi, I'm a Cylinder! (xml) 
     Hi, I'm a Capsule! (xml) 
     Hi, I'm a Sphere! (xml)
    Very Good! (xml)
  
  
    Level 2 (xml)
    Try comes close to all four objects, again. (xml)
     Hi, I'm a cube in the level 2! (xml)
     Hi, I'm a Cylinder in the level 2! (xml)
     Hi, I'm a Capsule in the level 2! (xml)
     Hi, I'm a Sphere in the level 2! (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!


16 comments:

  1. thank you. I'll try.

    ReplyDelete
  2. Can you not do this in Javascript? I just get XmlDocument does not denote a valid type. (not found)

    ReplyDelete
    Replies
    1. Nevermind.... import System.Xml for javascript. (im new sorry)

      Delete
  3. Hi, 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?

    ReplyDelete
    Replies
    1. i'm using public TextAsset GameAsset; you need to put the file there in the inspector.

      Delete
  4. Thanks for this article...Really helpful

    ReplyDelete
  5. Wow, thx. This helps a lot.

    ReplyDelete
  6. HELP ME !
    I download xml from www than save(replace) in ressources folder , but when i want same operation on large xml with <![DATA than i get

    XmlException: Text node cannot appear in this state. Line 1, position 1.
    Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace)

    ReplyDelete
  7. Thanks this simplified walkthrough is exactly what I needed

    ReplyDelete
  8. Thanks! Helped me alot.

    Created a www get webserver xml file. Inside that xml i have the latest version number and news items.
    Used your code to process that xml data for version check and to show news items ingame :D..

    Works great now.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete