Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[UNITYSCRIPT] Formatting an XML Doc and Reading in data to a 2D Array?

Discussion in 'Scripting' started by Amon, Jul 24, 2013.

  1. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    What would be the best way to store this type of data setup in an XML Doc then read it in to a 2D Array?

    I'd like for any code help people are willing to give to be in UnityScript, please!

    Code (csharp):
    1.  
    2. 111111111111
    3. 120000000000
    4. 110000000001
    5. 100000000001
    6. 100000000001
    7. 100000000001
    8. 100000000001
    9. 100000000001
    10. 100000000001
    11. 100000000001
    12. 100000000021
    13. 111111111111
    14.  
     
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Is there any particular reason for using XML? That seems inefficient and not really appropriate for that data. Just a plain text file would do.

    --Eric
     
  4. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    It may be me just assuming things but not all Platforms accept Windows .txt files, iOS etc!. Is that the case?

    That's pretty much the reason why I chose XML because it can be used on all platforms.

    Erich5h5, can you give us an example of using a txt file and load its data in to a 2d array?
     
  5. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    You can use System.IO for this sort of thing:

    http://msdn.microsoft.com/en-us/library/system.io.aspx

    But the easiest way is probably a Text Asset:

    Code (csharp):
    1. var asset : TextAsset;
    2. var myArray = new String[12,12];
    3.  
    4. function Start (){
    5.     var lines = asset.text.Split("\n" [0]);
    6.     var n = Vector2(0,0);
    7.     for (line in lines){
    8.         for (chara in line){
    9.             myArray[n.x,n.y] = chara.ToString();
    10.             n.x += 1;
    11.         }
    12.         n.x = 0;
    13.         n.y += 1;
    14.     }
    15. }
     
    Last edited: Jul 25, 2013
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Definitely not; a .txt file is just a text file, and has nothing to do with Windows (text files existing long before Windows ever did).

    --Eric
     
  7. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    Thanks tonyd. It works as in it reads the data and populates the array but the results are different to how they are laid out in the mapfile. When placing items it's all upside down.
     
  8. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    Can anyone offer any code [UNITYSCRIPT] that shows how to load a file like that in my OP using the BinaryReader method? So far I have had no luck. I would use the TextAsset method but it creates a fandango of 'Downcast' warnings.
     
  9. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    There are a lot of different ways to read XML.
    XML DOM
    XPath
    XMLReader
    Linq To XML

    For such a simple file you could use dom like
    http://www.w3schools.com/dom/dom_nodes_access.asp
    Code (csharp):
    1. xmlDoc=loadXMLDoc(Application.dataPath+"/Resources"+"/map1.xml");
    2.  
    3. x=xmlDoc.getElementsByTagName("LevelDataArray");
    4.  
    5. for (i=0;i<x.length;i++)
    6.   {
    7.   Debug.Log(x[i].childNodes[0].nodeValue);
    8.   }
    Or

    XMLReader (example not in javascript)
    http://www.xmlplease.com/read-xml-reader
    Code (csharp):
    1. myXmlReader = new XmlTextReader(Application.dataPath+"/Resources"+"/map1.xml");
    2. while ( myXmlReader.Read() ) {
    3.     if (myXmlReader.NodeType == XmlNodeType.Element  myXmlReader.LocalName == "LevelDataArray")
    4.     {
    5.     Debug.Log(myXmlReader.ReadElementContentAsString());
    6.     }
    7. }
    To read a txt file you could do this:
    http://forum.unity3d.com/threads/5084-Can-I-read-and-write-text-files-using-Javascript
    Code (csharp):
    1. import System.IO;
    2.  
    3. function Start () {
    4.     try {
    5.         // Create an instance of StreamReader to read from a file.
    6.         sr = new StreamReader(Application.dataPath+"/Resources"+"/map1.txt");
    7.         // Read and display lines from the file until the end of the file is reached.
    8.         line = sr.ReadLine();
    9.         while (line != null) {
    10.             //Break line into characters
    11.             for (chara in line){
    12.             Debug.Log(chara.ToString());
    13.             }
    14.             line = sr.ReadLine();
    15.         }
    16.         sr.Close();
    17.     }
    18.     catch (e) {
    19.         // Let the user know what went wrong.
    20.         Debug.Log("The file could not be read:");
    21.         Debug.Log(e.Message);
    22.     }
    23. }
     
    Last edited: Aug 8, 2013