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:

12345678910111213141516171819202122232425

<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…

The class uses the Away3D BasicTemplate class, which sets up the view and basic scene super quickly. I believe there’s also a FastTemplate class, but for some reason it doesn’t work with Sprite3Ds so watch out for that! And by default the BasicTemplate has a debug function built in, so you have to switch it off. The BasicTemplate is good for setting up something quick! But I’d rather setup something myself ideally.

It creates 200 Sprite3D objects, which use a white 4×4 square of BitmapData for its texture. Those are then scattered randomly in 3D space and on each frame it pushes them forward 20 units. I added in some keyboard controls so you can mess around with it (arrows and WASD).