The Tiled Map Editor and Unity3d
If you’ve been around game development for any length of time, you have probably heard of the Tiled Map Editor. It is a great open-source editor that supports orthogonal, isometric, and hexagonal maps. I’ve been eyeing it for a while and considered using it for HackBotZ but ended up settling on Ogmo Editor instead. That was a while back, and since then I’ve moved to Unity3d which does not natively support loading Tiled maps. I looked around a bit and found a lovely Tiled2Unity framework but it does not support loading Tiled maps at run-time. Instead, you import your Tiled maps into the editor and it makes prefabs of them. I however want the ability to simply open up a map in Tiled, change it, run the game and have the changes just show up. With that in mind, I began the process of coming up with some code that would parse a native Tiled .tmx format into some data structures I could work with. The .tmx format is really just an xml file with specific elements to describe map configurations, so I started on Saturday to write some simple C# to parse it. I had things going pretty well until I hit the data section of the layer element that describes exactly which tiles go where. There are several tile layer formats supported for this data : base-64 uncompressed, base-64 zlib compressed, base-64 gzip compressed, CSV, and XML. I took one of the example maps that comes with Tiled and changed the format to CSV, then XML, then the compressed formats. The difference in file size, as you might imagine, is significant. CSV and XML formatting make the files much too large for my purposes. I could have compressed the whole file, but that would not allow me to edit it in Tiled without decompressing it, so I set about looking into the two base-64 encoded compressed formats. Unfortunately, my cross-platform solution for compression in Unity3d, 7-zip’s LZMA SDK, does not support either gzip or zlib compression. I didn’t want to write compression/decompression code (although it would have been fun), so on Sunday night, I set about looking around for some C# implementations of either gzip or zlib. I found a few but ended up settling, at least for now, on ZLIB .NET which has a BSD license. When compiled, you end up with a 64kB dll that will compress/decompress zlib data. I will probably go back later and setup my 7zip helper shim to work with either library but for now it all works rather nicely. By Monday afternoon I had a solution that would pull in a .tmx file, parse it, and instantiate tiles in the correct locations in Unity3d.
The next thing to do is refactor the game code for buggers to utilize a single unity3d scene instead of loading a scene for each level, and that along with modifications/fixes to my Tiled loader is what I will be working on today.