Ok, I had a hell of a time figuring out how to load in XML for a Unity project I’m working on and use it to build a level. Fortunately the Unity Community is vibrant and helpful, even though you may have to do digging to find what you’re looking for. It can be difficult getting started, especially when working in C# as many of the examples are coded in Javascript.

Here’s an example of some game xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<stageData>

<stage>

<map>1, 2, 3, A, B, C</map>

<map>4, 5, 6, 7, 8, 9</map>

<map>2, 1, 3, 5, 6, 2</map>

</stage>

<map>0, 0, 0, 0, 0, 0</map>

<map>1, 1, 1, 1, 1, 1</map>

<map>A, A, A, A, A, A</map>

<map>3, 3, 3, 3, 3, 3</map>

<map>C, C, C, C, C, C</map>

</stage>

</stageData>

Keep in mind that there’s a million ways to go about doing this. I tried to justify why I did it this way, and the best I can say is “I just felt like it.” Haha!

I ended up using a C# port of this Lightweight XML parser. The benefit of it supposedly is that it’s only 8k compared to a megabyte or so that would be added if you used the native C# class, System.XML. I used the C# port by chirhotec in this post. I had to make a small change, commenting out the “string nodeContents” line because the variable wasn’t used.
Continue Reading…