Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question XML Deserialize Dialog and Objectives

Discussion in 'Scripting' started by armorhide406, Oct 13, 2022.

  1. armorhide406

    armorhide406

    Joined:
    Mar 18, 2016
    Posts:
    16
    I've been trying a while now to get an XML document full of DialogItems and Objectives to be deserialized and for it to populate text objects at runtime.

    Specifically dialog lines to change in time with audio files, and then objectives per level.
    I've tried following several tutorials but can never quite get it to work. Even this one, which I thought was closer to what I had in mind.

    Can anyone point me in the right direction? I also have a Dialog class and as I understand it I'd need a script to actually deserialize it, but I also expect I'll need a separate script to access the deserialized data and populate said text objects.

    Thanks in advance
    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2.  
    3. <All>
    4.     <DialogList>
    5.         <DialogItem>
    6.             <LL-1>
    7.                 <num>1</num>
    8.                 <level>Lobby</level>
    9.                 <content>
    10.                     This is a line of dialog 1.
    11.                 </content>
    12.             </LL-1>
    13.         </DialogItem>
    14.         <DialogItem>
    15.             <LL-2>
    16.                 <num>2</num>
    17.                 <level>Lobby</level>
    18.                 <content>
    19.                     This is a line of dialog 2.
    20.                 </content>
    21.             </LL-2>
    22.         </DialogItem>
    23.         <DialogItem>
    24.             <LL-1-alt>
    25.                 <num>0</num>
    26.                 <level>Lobby</level>
    27.                 <content>
    28.                     This is an alternate line of dialog 1.
    29.                 </content>
    30.             </LL-1-alt>
    31.         </DialogItem>
    32.         <DialogItem>
    33.             <L1-1>
    34.                 <num>3</num>
    35.                 <level>1</level>
    36.                 <content>
    37.                     This is a line of dialog 3.
    38.                 </content>
    39.             </L1-1>
    40.         </DialogItem>
    41.         <DialogItem>
    42.             <L1-2>
    43.                 <num>4</num>
    44.                 <level>1</level>
    45.                 <content>
    46.                     This is a line of dialog 4.
    47.                 </content>
    48.             </L1-2>
    49.         </DialogItem>
    50.         <DialogItem>
    51.             <L1-3>
    52.                 <num>5</num>
    53.                 <level>1</level>
    54.                 <content>
    55.                     This is a line of dialog 5.
    56.                 </content>
    57.             </L1-3>
    58.         </DialogItem>
    59.         <DialogItem>
    60.             <L1-4>
    61.                 <num>6</num>
    62.                 <level>1</level>
    63.                 <content>
    64.                     This is a line of dialog 6.
    65.                 </content>
    66.             </L1-4>
    67.         </DialogItem>
    68.     </DialogList>
    69.    
    70.     <ObjectivesList>
    71.         <Objective>
    72.             <O1-1>
    73.                 This is an objective for level 1.
    74.             </O1-1>
    75.         </Objective>
    76.     </ObjectivesList>
    77. </All>
    Code (CSharp):
    1. using System.Xml;
    2. using System.Xml.Serialization;
    3. using System.IO;
    4.  
    5. [XmlRoot("All")]
    6. public class Dialog
    7. {
    8.     [XmlAttribute("num")]
    9.     public int num { get; set; }
    10.     [XmlAttribute("level")]
    11.     public string level { get; set; }
    12.     public string content { get; set; }
    13. }
    14.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    If this is to go in Unity, I highly recommend just using ScriptableObjects to hold all the contents you need. There are plenty of tutorials out there for dialog systems based on this approach, and just generally tutorials on using ScriptableObjects. They're VERY powerful in the Unity editor.

    If you insist on parsing your own serialized data, XML is the last thing I would consider. JSON is far cleaner and has a huge advantage over XML: JSON actually works.

    Remember, if you have a problem and you reach for XML... congratulations! Now you have TWO (2) problems.

    In general I highly suggest staying away from Unity's JSON "tiny lite" package. It's really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.

    Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window -> Package Manager).

    https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

    Also, always be sure to leverage sites like:

    https://jsonlint.com
    https://json2csharp.com
    https://csharp2json.io
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    One way is TextAsset which you can use as a field type.
    You then literally dragndrop the file on this field, and boom, it's natively accessible by your script.
    If this is an editor, you can also use AssetDatabase.GetAssetPath() with this asset for more functionalities, but basically you can access its contents through 'text' and it will get embedded with your project automatically.
     
    armorhide406 likes this.
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    I actually like XMLs a lot, moreso than JSON files. They are somehow more, uhm, honest.
    But Kurt's recommendation is, granted, better >>vastly superior in fact<< for most applications out there, yours is not as special.
     
  5. armorhide406

    armorhide406

    Joined:
    Mar 18, 2016
    Posts:
    16
    Ok but then how do I access data stored on a JSON file? Any quick links you have to spare me having to sort through potentially unhelpful similar results?

    I COULD use scriptable objects but from my cursory search that doesn't seem to really have to do with what I'm trying; i.e. a lot of dialog lines to change a single TextObject (subtitle) and then objectives to populate per level
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    The crux of your problem really is about storing data in such a way that you can use it later. Scriptable Objects are the go-to for this in Unity as they're a completely native way to design bags of data, with no deserialisation needed as Unity does it all for you.

    So it's pretty trivial to use scriptable objects to hold the data you want in a typical C# manner. Which, for dialogue, is often just a collection of strings.
     
  7. armorhide406

    armorhide406

    Joined:
    Mar 18, 2016
    Posts:
    16
    Well hush my mouth. Thanks to you all