Search Unity

Loading an XML file

Discussion in 'Scripting' started by MatOfLink, Jun 22, 2009.

  1. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    Thank you. So no javascript solution?
     
  2. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    There probably is one, but I don't know of one offhand. If all you want to do is plot Vector3s it's actually probably better to "roll your own" method for converting them to CSV (comma separated values) which can be read by any graphing software. For a few Vector3's your output would be

    1.100000,1.200000,1.300000
    2.100000,2.200000,1.400000
    etc.

    Commas separate the columns and a return character separates the rows.
     
  3. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    My idea is: Everytime a user i.e. clicks a button in a gui, this information is sent to a file(text/xml) for then later to be able do statistics on the values sent from unity to the file. Think of it as a logging tool, where I want to log all user behaviour in-game, and then afterwards present/plot 'how the user moved around' 'what buttons he clicked' and so fort.
    Any pointers are welcomed! :)
     
  4. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    It's an interesting idea... the amount of effort it would take largely depends on the amount and diversity of information you want to save. From what you have said it seems like this will probably be a web-based application (given that you are tracking the user data, presumably they need to be online for that to be effective). If it is indeed meant to be a web-application you will have to find some way of saving the information to your server because you wont directly be able to save the .xml file. My suggestion would be to send the data to a MySQL database on your server. In this format it would make it very easy to do graphing and other data-analysis. You can send the data to your server using Unity's WWW class as POST data... in that case you will need to have some server-side solution (like some simple PHP scripts) for entering the info into the database.

    It's of course a lot easier if this is a desktop application because you will be able to read/write files.
     
    Last edited: Mar 9, 2011
  5. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    It is supposed to be on a desktop! :) Its a project I run on uni with a touchscreen. The data collection of the users is to be translated with respect to usability, and user profiling.
    Everytime a user clicks a button, this button will be tagged, so the info sent will be specific for that object. This will later be translated from xml through NumPy/SciPY. The user positions while interacting will be logged to show visual search parameters for the specific user.
    So again. In JS on a desktop from a running application to a locally placed XML file.
     
  6. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    I don't have experience serializing data to XML in Unityscript/Javascript... but I can only assume that it has come up before. You can mix C# and JS in your projects so if you wanted your serializing class to be in C#, while the rest of the project is in JS that should work fine. If you decide to do that then the two links I posted earlier will be very helpful.
     
  7. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    Ok, thanx I will look into it as soon as possible.
     
  8. Anthony85

    Anthony85

    Joined:
    Mar 9, 2011
    Posts:
    30
    Interesting, I too am looking to find a way to impliment XMLs in unity. I've very surprised the engine doesn't support them Out-of-The-Box

    I'm probably in way over my head at the moment, (that happens to me ALOT with things like this) since i know very little about writing code for parsers, but anyways, I've already started work on a model that i intend to put into unity at some stage (i'll probably put it in the island demo for starters) anyways, i've already written a basic, rough XML file for it, The type of game this model would usually be in is an RTS. I've had some experience modding RTS games that use XMLs extensively for most data landling (especially where ingame units are concerned), I followed the structure of XMLs from two games I have as an example when writing this one. I know it's nowhere near finished, but if you guys want to look over what i've written thus far, and tell me what you think, i'll post the code i've got. :p
     
  9. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    There's a box now? :)

    Anyhow it actually DOES support them out-of-the-box so to speak with the System.Xml namespace if you are using C#. It doesn't have it's own methods for converting XML into GameObjects because something like that would have be to specifically tailored to the game you are making.

    If you have already started building XML files which represents units for an RTS game for example, that's great.... you can use them to populate data inside your game. However it is more useful I think to make the Class structure of your units in the game first, and then think about how you are going to serialize them (aka: convert them to xml) and vice-versa.
     
  10. Anthony85

    Anthony85

    Joined:
    Mar 9, 2011
    Posts:
    30
    Well, thanks for the tidbit of advice :) I do know one or two people who are apparently good with coding, so i suppose i could ask them to give me a hand when the time comes (I need to figure out Unity first), well, in case anyone is interested (or has ideas), here's the xml code for a Trireme I'm making in max.

    Code (csharp):
    1. <Units>
    2.     <Unit Name="Trireme">
    3.         <TextID>TEXT_TRIREME</TextID>
    4.         <TextTooltipID>TEXT_TOOLTIP_TRIREME</TextTooltipID>
    5.         <Icon>Assets/Textures/UI/Icons/Trireme_256x256</Icon>
    6.         <LOS>60</LOS>
    7.         <Cost ResourceType="Wood">2000</Cost>
    8.         <Cost ResourceType="Metal">500</Cost>
    9.         <Salvage ResourceType="Wood">1000</Salvage>
    10.         <MaxSpeedKmph>9.0</MaxSpeedKmph>
    11.         <MaxSpeedKmph>13.9</MaxSpeedKmph>
    12.         <ArmorType></ArmorType>
    13.         <Unittype>Unit</Unittype>
    14.         <Unittype>Ship</Unittype>
    15.         <Unittype>Military</Unittype>
    16.         <UnitAIType>Combative</UnitAIType>
    17.         <Unittype>Targetable</Unittype>
    18.         <Unittype>LogicalTypeIsValidRamAttackTarget</Unittype>
    19.         <MovementType>Water</MovementType>
    20.         <ModelName>Assets/Models/Ships/Trireme.fbx</ModelName>
    21.         <InitialHitPionts>15000</InitialHitPionts>
    22.         <MaxHitPionts>20000</MaxHitPionts>
    23.         <Flag>Walkable</Flag>
    24.         <Flag>Burnable</Flag>
    25.         <Flag>Selectable</Flag>
    26.         <Flag>CollidesWithProjectiles</Flag>
    27.         <Flag>HideInFogOfWar</Flag>
    28.         <Flag>ShowOnMiniMap</Flag>
    29.         <Action Name="RamAttack">
    30.             <Parameter Name="RequiresTarget" Value="True"></Parameter>
    31.             <Parameter Name="Damage" Type="Crush" Value="10000"></Parameter>
    32.             <Parameter Name="Target" Type="IsValidRamAttackTarget"></Parameter>
    33.             <Parameter Name="MaxRange" Value="0.1"></Parameter>
    34.             <Parameter Name="MaximumSpeedMultiplier" Value="2.5"></Parameter>
    35.         </Action>
    36.     </Unit>    
    37. </Units>
    Like i said in my last post, this is a WIP xml, and is very rough, will most likely go through much refinement before it's usable, I just thought i'd throw an xml together and get a basic idea of what i want units in a game to have/be able to do. It's structured in a very similar way to the xml files that Age of Mythology uses. I haven't even written the string file yet, or even finished the model, or even started any game. :p
     
  11. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    Would it be poosible to write to a txt? Then the hurdle lies in the interpretation of the file, not the creation.. am I wrong? and yes, in JS please ;)
     
  12. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    Well you are kind of out of the scope of the thread now. If you want to know how to write to and save a file in JS then I am sure someone could help you... Personally I don't know offhand what the methods are in JS to do this. Start another thread about this topic, but only after searching if someone has already asked this question.
     
  13. montblack

    montblack

    Joined:
    Jul 21, 2010
    Posts:
    3
    help i have this problem with WWW object

    this is my error message

    Rejected because no crossdomain.xml policy file was found
    and this is my code

    string url = "http://unocol.com/jj/Desnudo.xml";
    IEnumerator Start() {
    WWW www = new WWW(url);
    yield return www;
    if (www.error == null) {
    //no error occured
    string xml = www.data;
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xml);
    Debug.Log("xml"+www.data);
    } else {
    Debug.Log("ERROR: " + www.error);
    }
    www.Dispose();
    www = null;
    }
     
  14. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    I'm getting this error in XMLParser.js

     
  15. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    the error says exactly what the problem is
     
  16. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    That error is cleared ...
    now it gives new error

     
  17. megisto

    megisto

    Joined:
    Dec 25, 2009
    Posts:
    127
    I am looking at this post, as it seems to be the best one to start using XML in Unity. I see it is some years old, so i don't know it is still the best solution. Is it?

    The second question: will it works on web/standalone/ipad/... devices?

    Thank you, i'll post here my progress.
     
  18. George Foot

    George Foot

    Joined:
    Feb 22, 2012
    Posts:
    399
    Is there a strong reason not to just use the standard serialization? If you feed it simple classes, the resulting XML is pretty tidy and hand-editable, and it can handle more complex structures too, though the file becomes more fragile and so less appealing to edit directly.

    For me the main downside is efficiency with really large documents, but for most things it's fine to instantiate an object hierarchy in memory from the file and browse that by hand.
     
  19. pgomes

    pgomes

    Joined:
    Mar 13, 2012
    Posts:
    11
    A word of advice: When using System.Xml and deploying to iOS take care not to select ".NET 2.0 Subset" Api compatibility level in the player settings. I have experienced consistent crashes at runtime.

    I'm assuming System.Xml uses some features not included in the subset. For a reference to these limitations see:
    http://docs.xamarin.com/ios/about/limitations
     
    Last edited: Apr 19, 2012
  20. 3D_Content

    3D_Content

    Joined:
    Apr 19, 2011
    Posts:
    7
    Thanks for all XML-examples!

    Example from page one is working with example-XML and I am trying to use my own XML now. I have this XML which I am using:

    Code (csharp):
    1. Test: MyTest
    2. <gameproject xmlns="">
    3.   <game>
    4.     <game_number>723493</game_number>
    5.     <game_name>My Game</game_name>
    6.     <levels_total>2</levels_total>
    7.     <levels>
    8.       <level>
    9.         <level_number>01</level_number>
    10.         <difficulty>Easy</difficulty>
    11.         <time>100</time>
    12.       </level>
    13.       <level>
    14.         <level_number>02</level_number>
    15.         <difficulty>Medium</difficulty>
    16.         <time>200</time>
    17.       </level>
    18.     </levels>
    19.   </game>
    20. </gameproject>
    But I get this error:

    IndexOutOfRangeException: Array index is out of range.
    www1+$Helper$2+$.MoveNext () (at Assets/www1.js:24)​

    Line 24 from my script:
    Code (csharp):
    1. Debug.Log(tSimulationData[8]["level_number"]);
    I can´t arrays and hashtables well, so if someone can help me with this problem. I will read "level_number" from level 01.
     
  21. Forrest_Gimp

    Forrest_Gimp

    Joined:
    Jul 11, 2012
    Posts:
    4
    I'm trying to use Flimgoblins XML-parser, and it seems to work reading the xml I feed it. What I couldn't figure out is how to access any of the data in the object I get from parsing. I can't access any of the nodes using [ ] , no matter if I put in a key as string (with or without quotes), neither using a numerical index. Sticking to the SolarSystem-example, what would I have to use to get something like the color of mars?
     
  22. CommunityUS

    CommunityUS

    Joined:
    Sep 2, 2011
    Posts:
    240
    You've probably already figured this out but in interest of keeping things answered.

    Code (csharp):
    1. Debug.Log(node["system"][0]["body"][4]["@color"]);
    //red