Tiled2Unity

(Note: ‘Minimalist’ example artwork is attributed to Blarget2 via OpenGameArt.org)

If you’ve been working on a 2D tile-based game in the last couple of years then there’s a decent chance you’re familiar with Thorbjørn Lindeijer’s excellent Tiled Map Editor. It’s what I used when creating the maps for Mega Man in Unity and I’ve now retooled and de-hacked my Tiled-to-Unity exporter (called Tiled2Unity Utility because naming things is hard) for general consumption by my fellow game developers  – free of charge under the MIT License. I sincerely hope others find it useful.

You can download the Tiled2Unity Utility from the Download Page.

Features:

  • Builds a Unity prefab out of your TMX map file
  • Supports all TMX layer formats (XML, CSV, Base64, gzip/zlib compressed)
  • Multiple layers and tilesets supported
  • Exports Object Layer as polygons, polylines, rectangles, circles
  • Tile Layer collisions supported (with slopes, odd-shaped polygons)
  • Polygon colliders can be concave, have holes, and be composed of separate polygons
  • Can assign Tag, Sorting Layer, Order in Layer, and (Physics) Layer of exported GameObjects through properties in Tiled
  • Support for customized creation of Unity Prefabs
  • Easy to use: In most cases, you simply export a TMX file into your Unity project and place the automatically generated prefab in your scene – no further edits needed

Limitations:

  • Orthographic map support only
  • Ellipse objects are ignored unless they are circular (there is no ellipse collider in Unity)
  • Object rotation is currently not supported
  • Tiled2Unity Utility only runs on Windows out of the box (Prefabs exported into Unity will work on all devices though)

Note that I made a purposeful decision not to support isometric maps. It adds far too much complexity for something that I think is a bit of a hack. I feel orthographic maps should be used at all times and the illusion of top-down or side-scroller or isometric should come from the art, not some weirdo coordinate system. (Your mileage may vary but it’s not like the Nintendo Entertainment System had an isometric mode.)

Tiled2Unity Uses Newer Features of the Tiled Map Editor

In order to assign collision data to tiles you will need to use the new Tile Collision Editor in Tiled.

Tile Collision Editor Menu Item

This Tiled feature is currently in beta so you’ll need to get a version of the program from the daily builds here: http://files.mapeditor.org/daily/

For my development and testing I’ve used the May 23rd, 2014 build. (Builds older than April 24th, 2014 will crash if you have collision data in external tilesets.)

Getting Started with Tiled2Unity

Before you begin exporting Tiled data into Unity, your project needs to be aware of how it will import the *.unity2tiled.xml files that Tiled2Unity spits out. The scripts for this are part of the Tiled2Unity.unitypackage file found in the install directory.

There are two ways to import this package into your Unity project:

  1. From the Tiled2Unity install folder: Double click on the Tiled2Unity.unitypackage file
  2. From within Tiled2Unity: Just select the “Import Unity Package to Project” menu item

Import Tiled2Unity Unitypackage

In both cases, Unity will take you through the steps of installing the package into your Unity project. There’s a number of scripts and folders here. You’ll need them all.

Import all items

Now we’re ready to export some Tiled maps into Unity.

Using Tiled2Unity Examples

Your Tiled2Unity install contains a couple of TMX file examples. We’re going to open the minimalist.tmx file in Unity …

Minimalist map

We don’t need to run Tiled in order to export a TMX file into Unity, but I’ve set up a command in Tiled that will run Tiled2Unity for me.  This allows me to make my map edits in Tiled and then quickly export to Unity by pressing F5.

Setting Tiled2Unity command in Tiled

"c:\Program Files (x86)\Tiled2Unity\Tiled2Unity.exe" %mapfile c:\MyUnity\Test

The command is made up of 3 parts:

  1. The path to your Tiled2Unity install
  2. The %mapfile macro (this will be the same for all users)
  3. The path to your Unity project (you can leave this blank and use the Tiled2Unity GUI instead)

Hitting F5, now Tiled2Unity pops up with our map already loaded and ready to export to our Unity project …

Running Tiled2Unity from Tiled

Now, pressing the Big Ass Export Button will export an XML file (in this case, minimalist.tiled2unity.xml) to our Unity project where our importer scripts will create the meshes, textures, and materials needed and roll them all up into a prefab named after our TMX file.

Minimalist map in Unity

Now we can place the prefab into our scene hierarchy. (If our map was to be purely decorative then we’d be done.)

Minimalist prefab placed in scene

Adding Collision Geometry

Chances are most maps we make in Tiled for use in video games will require some collision geometry on them for sprites to interact with. Other solutions I’ve found on the internet or the Unity Asset Store require you to hand-place collision objects by hand in Tiled which I think is a total PITA. I’m going to show you how we can easily add complicated collision geometry (with holes, slopes, concave shapes, etc.) to our tile layers that are exported to Unity as PolygonCollider2Ds.

With Tiled still open on our minimalist map, bring up the Tiled Collision Editor an add a Rectangle or Polygon object that fully encloses the solid dark grey tile of our mini-tile tileset …

Add collision to tile

Let’s leave the other tiles alone for now and see how we’re doing. Press F5 again to bring up our Tiled2Unity exporter and press the Preview Exported Map button to see how the geometry would appear in Unity …

First collision preview

Here we can see how the geometry of just one tile will be combined into a polygon collider as the tile is repeated throughout the map. Note that, in the resulting collision mesh, there is actually four shapes here — two of which are rather awkward. Except for not matching the visuals, that’s actually okay, the PolygonCollider2D in Unity would handle this fine.

Let’s go back to Tiled and add polygons to our sloped tiles …

Adding sloped collision

(I recommend that Snap to Grid and/or Snap to Fine Grid is enabled when placing polygons in Tiled)

Previewing again we can see how sloped geometry will be added to the collision mesh …

Second preview

Again, this geometry could be exported into our Unity project and the PolygonCollider2D created from it would just work.

Going over all our darker tiles and adding polygons/rectangles I get a final preview that I’m pretty happy with …

Final preview

The best part of this is as we edit the map the collisions assigned to each tile move along with them. You add the geometry to your tiles once, create all the map data you like, and let the exporter take care of the rest.

Here’s the final exported prefab with our PolygonCollider2D in Unity …

Map with collision in Unity

And there you go, a 2D tile-based map, authored in Tiled, with collision, in your Unity scene.

Find any bugs? Is something not working as expected? Got a feature request that makes sense and won’t kill my social life? Feel free to email me.

By the way, the magic behind this polygon construction is courtesy of Angus Johnson’s exceptional (and open source) Clipper library. In my opinion, layer collisions is the most essential feature of Tiled2Unity and Johnson’s Clipper library worked for me the first time I tried to use it. C’est Magnifique! *kisses fingertips*

{ 68 comments }

Mega Man in Unity

by Seanba on March 16, 2014

Mega Man in Unity

After shipping Epic Mickey 2 (and before the heartbreaking closure of Junction Point Studios) I had a couple of months of comp time to unwind and sharpen the saw a bit.

I decided to spend my some time during that break getting to know the Unity game engine.

To be honest, I had to fight some strong biases in starting that exercise. I’m not sure how widely known the following sentiment is, but for programmers in big-budget, high-end AAA game development the idea of using an engine like Unity is considered weak sauce. Better to spend your time in C++ learning more about concurrency and data object model programming.

But I’ve been longing for some technology to help me prototype gaming ideas quickly and it sounded like Unity was the best contender.

I also decided that this would be the first time in my double life as a hobbyist game programmer that I was actually going to finish something. To that point I chose a beloved game from my childhood and decided to remake, in part, the original Mega Man from the NES golden days. I figured my love for the franchise would help get me past the eventual malaise that smothers every hobby project of mine once real life kicks in.

I’ve just recently finished the exercise and you can play the game in your browser here …

Mega Man in Unity

Only Cutman’s Stage

Like most old-time gamers I’ve spoken to, my experience with Mega Man started with Cutman’s stage — so that is the focus for this game. Other limitations on this Unity-powered game include …

  • Mega Man has no additional powers from any Robot Masters. The Mega Buster is his only weapon.
  • Enemies do not drop powerups, free lives, or any other items.
  • There is no scoring.

Obviously this is not a complete Mega Man game but fans of the original title will hopefully appreciate the 10 minutes or so of oldschool gameplay.

And fellow hobbyists might appreciate that this was accomplished with the free version of Unity.

Some Assembly Was Required

Still, with Unity being a 3D engine there were two important technologies I spent time on in order to display 2D assets:

  1. Sprites: I used the freely-available SpriteManager utility for sprite animation. In addition, I wrote a utility that took animations from GraphicsGale and generated code and data for each sprite.
  2. Tile-based Maps: I wrote a utility that transformed map data from Tiled Map Editor (a great little program for 2D enthusiasts, btw) into a Wavefront OBJ file that could be imported into Unity.

Ironically, I finished this project just as Unity 4.3 was being released so there’s a good bit of work here (especially with the sprites) I was planning to use for other titles that is now obsolete. That’s okay though as I’d much rather see sprites being first class citizens within Unity anyhow.

Was it Time Well Spent?

I started this project in December of 2012 and within a couple of weeks I pretty much had everything in place, with Mega Man running and jumping around Cutman’s level and a couple of the enemy sprites doing their thing. I was (and still am) pleased with how easy it is to work with Unity, even when you are trying to do something that game engine wasn’t really built for, like 2D sprite-based gaming.

After that short time I could have (and maybe should have) moved on to something original as the exercise of “learning” Unity was accomplished — but what I can say, I love that little Blue Bomber dude. I had fun with this.

How long did this take? It’s hard to say as I don’t do project management on my hobbies (what a depressing concept!). Yes, I started this nearly a year-and-a-half ago, but man, I also had to layoff some 30 of my closest programmer friends, shut down a studio, and find another job in that time as well. I only worked on this in spurts. Now, I’m just happy with the end result and I’m eager to focus on something else with my spare time. I hope some Mega Man fans give it a try and enjoy a small piece of nostalgia too.

And as far as Unity goes: I use it for all my hobby game programming projects now. I’d like to see the 2D components mature some more (Unity 4.3 is a great start to that) but overall I can’t think of any other (free) game engine that comes close to this one.

NOTE: I feel I should apologize for the occasional jittery scrolling and screen tearing in the playable Unity game. This appears to be an issue with the Web Player that other developers struggle with and as far as I’ve researched there’s no easy solution. If I’m wrong, email me the details. I’ll gladly fix it.

 

I’m embarrassed I didn’t realize it would be a problem until after the game was complete as I should have tested on the Web Player throughout development. That’s a total rookie mistake there.

{ 5 comments }

Note: The guys at 1 More Castle came up with a great idea: a day of blogging completely devoted to the review of games, new or old, retail or indie, that we may consider great. It’s an exercise of positive reflection on the titles that make us the gamers we are.

They’ve deemed April 8th to be international Review A Great Game Day. Dozens of such articles are available on their webpage now … and more are coming. I highly recommend a visit.

Below is my entry focused on a game that I hold dear. (Naturally, I waited until the last minute to write it.)

Double Dragon II: The Revenge: The Review

Title: Double Dragon II: The Revenge
Developer: Technos Japan
Publisher: Acclaim Entertainment
Platform: Nintendo Entertainment System
Release: January, 1990
Genre: Beat Em’ Up

The original Double Dragon game was something that completely blew my mind when I first saw it in arcades back in the late 1980s. When it became available for the Nintendo Entertainment System I saved up for months to bring it home.

On that day, my brother and I rushed into the house, ripped off the plastic, blew as hard as we could into the cartridge slot and then … what the hell is this? No cooperative two-player?

Thank god that years later Technos Japan would redeem the series with what I consider to be one of most essential games of any NES collection: Double Dragon II: The Revenge.

Cooperative Play … and Much More

Double Dragon 2 is of the rare breed of games that can be enjoyed both with a friend or solo – even by today’s standards with games like Halo or Gears of War.

Double Dragon 2 (Two Players)

Equally impressive, especially for the time, is the rich repertoire of moves available to the player. Fans of the series will enjoy the feature that Double Dragon brought to fighting games: The ability to grab your opponent by the hair or collar and whale on them. Yet even here you have several choices at your command: You can kick the bad guy repeatedly, you can toss him over your shoulder, you can elbow drop, and you can “upper kick” – my favorite move that launches your opponent upwards – hopefully into some environmental hazard.

Additionally, Double Dragon 2 introduced a number of well-timed super moves. I never grew tired of jumping into a cyclone kick, landing, and then following up with a high-jump kick …

… that is, if I could pull it off …

But There Are Frustrations

For its greatness, Double Dragon 2 does manage to be an unfairly punishing title at times. The biggest complaint is, sadly, the controls. Being on the NES, the developers had no choice but to somehow map 3 actions (left attack, right attack, and jump) onto 2 buttons. The choice of making jump a simultaneous A + B button press was unavoidable but I often can’t get the timing down right.

For the most part, this is a forgivable flaw, but when combined with the areas of the game where you are expected to platform it becomes a controller-smashing-inducing experience.

Meant to Punch

On many play sessions (yes, with my kid brother) it was these areas where the game broke from its true design (Platforming?! In a fighting game?) that kept both of us from seeing the game through to its end. I don’t mind losing when I have a lack of skill or I make a poor decision but stuff like this can be a real drag.

Still Great Fun

But when focused on fighting it’s difficult for me to think of a better game in the genre. In addition to your fists and feet it can also be fun to have the environment finish off your opponents instead. My favorite is timing my movement and attacks to ditch enemies out of a flying helicopter in Mission 3 – Battle in the Chopper but there are plenty of other ledges and spikes throughout the game that can do the work for you.

Double Dragon2. Off the ledge!

These are the kind of gameplay elements that would make great achievements or trophies on today’s consoles and makes me wish that publishers would take more care in re-introducing us to our best-loved games from another time. Emulation on Virtual Console, PlayStation Network, and Xbox Live Arcade is one thing but I’d love see faithful recreations that give us something more (like online co-op) and Double Dragon 2 would be a great guinea pig for such an experiment.

Until then, I’ll just have to bring the original Double Dragon 2 cartridge (Game Over Videogames is good retailer to find one) to my brother’s house. Too bad he still lives up in Canada, the hoser.

{ 2 comments }

Yeah, yeah, the title is a bit cheesy. (I was originally going to go with “The Galaxy is at Peace with Super Metroid Symphony”.)

super-metroid-symphony

This past Tuesday, Black Robinson delivered a synthetic symphony love letter to Metroid fans to mark the 19th anniversary of the Super Metroid release in Japan. I’m a big fan of retro video game music and this now tops out as my favorite album in the genre.

I remember vividly playing Super Metroid for the first time and being totally blown away by the prologue with Samus Aran blinking behind her visor and, my god, the music from that portion is covered in this album in a way that just brings tears to my eyes.

Super Metroid Intro

Thankfully, Blake recreated every single piece of music from the game so every fan can joyfully reflect on their own favorite memories from Super Metroid. It’s hard to believe that just one dude was able to pull off. It’s a tremendous work of art and a labor of love on his part. For video game music fans it simply doesn’t get any better than this.

The album is available on iTunes for the completely undervalued cost of $9.99. I bought it again on Loudr for my gamer geek brother today, setting my own price of $20, not because I’m a nice guy (I am) but so I can share something awesome and, hopefully, do my little part to encourage Blake to give us some more great music.

I’m just blown away by this. It’s excellent.

Super Metroid Symphony by Blake Robinson

{ 0 comments }

A couple of years ago I decided to move all my hobby source code from my local Subversion service to an online service.

My needs were pretty simple:

  • Interface locally through a Subversion client (like TortoiseSVN)
  • At least 500MB, but the more the better
  • Private, with SSL encryption
  • And, like my email, free

Assembla was highly recommended by peers, but wasn’t free, so I went with something else that fit the bill. Since then this service moved their ideal free, private, secure, 500MB, ad-free package to a less attractive free, private, non-secure, 200MB one – and with badly aligned, eyesore advertisements to boot.

Not that I can really blame them, mind you. They do need to make money, and it’s not like I program for free (at my day job anyhow). So no hard feelings.

But it turns out that Assembla is now offering a free plans so I’m trying them on for size. Their free SVN hosting is private and secure – and currently comes with a 2GB data store which is more than enough for my hobby projects that tend to be heavy on code and light on artistic resources.

It’s early into my relationship with Assembla, but at first blush I’m impressed with how easy it was to create a new repository and start adding code to it. I also appreciate their online dashboard for being well-designed, clean, and functional. You’ll see some ads there, but they are respectfully out of the way.

We’ll see how it goes, but so far so good.

{ 8 comments }

If you’ve worked as a game developer in Austin for the past 5 or 6 years you’ve no doubt heard the buzz: Austin game dev is in a bit of rut.

But with such a strong showing in 2010, I’m calling it over.  After years seemingly dominated by cancelled titles and studio closures it feels like we’ve hit our stride. It was a great year.

Note: It’s possible in my roundup that I may have missed some key releases from Austin talent. Let me know and I’ll fix that (and my apologies in advance).

Comic Jumper

Comic Jumper 

Twisted Pixel Games moved to Austin just two short years ago and have already shipped 3 high-quality games, including this year’s Comic Jumper. These guys give me the impression they really love their work and don’t take themselves too seriously.

I don’t even question a purchase from this studio anymore. If they make it, I buy it.

Darksiders

Darksiders

If I was forced to pick a favorite, and I wasn’t clichéd enough to pick my own game, then Darksiders would probably be it. Vigil Games took some knocks for borrowing gameplay elements from other titles, but I think we need more games that are heavily influenced by The Legend of Zelda and The God of War. I really enjoyed it. Great story and voice-acting too.

Disney Epic Mickey

Disney Epic Mickey

The game I shipped this year, and proud of it. What I love best about Disney Epic Mickey is the union of “game I want to play” and “game my kids want to watch me play”.

We’re having a great time together with this – except for that one time I got lost and my youngest daughter yelled, “I’m bored, let’s play Mario!” Looks like I’m only sending one kid to college. Let’s see how bored she is then.

Donkey Kong Country Returns

Donkey Kong Country Returns

I’ve got to give Retro Studios credit for keeping this one under wraps for so long. I had no idea they were working on Donkey Kong Country Returns until they announced it at E3 alongside Epic Mickey.

It’s another strong showing from Retro Studios, which has been giving Austin solid game dev cred all decade.

Star Wars Force Unleashed II (Wii)

Force Unleashed 2 (Wii)

From what I’ve heard, Red Fly Studios made the Wii version of Star Wars Force Unleashed II – a separate game from the Xbox and PS3 offerings – from scratch in the timeframe of one year. And I’ve read more than one review that says this version was better than the HD versions, which certainly had many more resources thrown at them. Most impressive.

This is the only game I haven’t played yet, which is horrible since so many good friends worked on it. I’m going to have to do something about that soon.

And The Rest …

And the restCertain Affinity collaborated on the development of Call of Duty: Black Ops. Although the game is primarily developed in California by Treyarch, it’s got to be a big deal for Certain Affinity to have worked on the most highly anticipated and best-selling game of the year.

And I’m sure there was a lot of solid new iPhone, Flash, or Facebook game apps developed in Austin this year but I have to apologize for not having that on my radar.

But in this space I will take special notice of Spacetime Studios’ game, Pocket Legends. I worked at Spacetime for a short while before coming to Junction Point / Disney. They’re a fun bunch of smart developers – and it sounds like they’ve had a great year with their mobile MMO. That they’ve been able to tap into that market so quickly is a testament to their technology and culture.

Congrats Austin Developers

With all this great (shipped!) software there’s no doubt 2010 was a bit of a rough ride for many. I hope my peers found the challenges worth it and are looking to continue improving and growing for 2011. And let’s find a way to get these suckers out the door without working such crazy hours, eh? I’ve got kids now, you know?

Congratulations on a terrific year, and thanks for making Austin a cool place to live and work.

{ 1 comment }

Writing is hard.

Today marks my first-year anniversary with this blog – and that pretty much sums up my thoughts in three words.

It’s kind of funny I’ve been surprised by this fact as everything I’ve ever thought worth pursuing before has turned out to be a difficult affair:

  • Mastering a musical instrument
  • Speaking a new language
  • Drawing, painting and other fine arts
  • Making video games

By the way, that last example has been an especially sore point for me for years, as the general public tends to think game developers just sit around playing games all day with perhaps the odd task here and there to tighten up the graphics.

So consider this my apology to writing professionals and enthusiasts everywhere. This stuff is crazy hard – just like any other worthy pursuit – and I’m sorry I took it for granted.

But this experiment has taught me there is value at keeping at it, if for no other reason than to be more than just a guy with a résumé. I care about my craft and this is a good forum to grow outside my day job (which has often been a night and weekend job too this past year as we brought Epic Mickey to ship).

So here’s looking forward to year two from a writer trying to suck less. And special thanks to the half-dozen or so industry friends and family members for watching.

{ 0 comments }

Clever Code != Friendly Code

by Seanba on November 7, 2010

The other day I was looking at some old code I’ve written.

Ugh.

Now, I tend to agree with Jeff Atwood’s Suck Less Every Year mantra when he says:

You should be unhappy with code you wrote a year ago. If you aren’t, that means either A) you haven’t learned anything in a year, B) your code can’t be improved, or C) you never revisit old code. All of these are the kiss of death for software developers.

But it’s strange how even the “good” or “smart” code from times gone by has a horrible code smell to it now.

Case in point:

using namespace std;

for_each(obsFirst, obsLast, bind2nd(mem_fun(&Observer::LightMoved), light));

Quick – what does that code above perform? Unless you’re an old-school Standard Template Library user (the usage has been simplified with Technical Report 1) then you’re probably going to have to look something up.

std::for_each is itself easy enough to grok:

using namespace std;

for_each(first, last, someFunction);

This simply invokes a function or function object (anything supporting the parentheses operator) for each object within the range from first to last, passing in the object as an argument to someFunction.

But say you need to pass in two arguments to the function called? Then you’ll need std::bind2nd.

using namespace std;

for_each(first, last, bind2nd(someFunction, arg2));

This will call someFunction(arg1, arg2), getting each instance of arg1  from the range of first to last.

Actually, that code above won’t compile, because std::bind2nd  requires, as its first argument, a functor object with particular class traits, so we have to wrap the function appropriately with an adaptor.

using namespace std;

for_each(first, last, bind2nd(ptr_fun(someFunction), arg2));

You can see things are starting to get complicated here. Now say you don’t want to call a free function for each object in a range with a second argument, but rather a member function on the object. That’s why std::mem_fun is needed, to wrap a member function into a form needed by std::bind2nd.

using namespace std;

for_each(first, last, bind2nd(mem_fun(&Foo::SomeMemberFunction), arg2));

Yuck, what a mess! But written another way, this is exactly what that code above does …

for (fooIterator = first; fooIterator != last; ++fooIterator)

{

    *fooIterator->SomeMemberFunction(arg2);

}

… which is actually a much better way of explaining the whole eyesore. So why not just write the code that way to begin with and be done with it?

To be honest, I read about for_each, bind2nd, and mem_fun in some book about STL and thought it was pretty clever, so I started a new habit of using those template functions whenever I could.

And, unfortunately for me, the very first time I had a code review with a for_each  loop of this fashion in it, the reviewer had these reactions:

  • First impression: “What the f*** is this s***?”
  • After explaining: “Oh cool, I didn’t even know you could do that with STL.”

I wish he had of just kept with his first impression and told me to rewrite that line of code into something readable, but he was impressed with the new knowledge I shared with him, and I couldn’t help but feel smart about the whole stupid thing.

Funny enough, I was asked to add a code comment though:

using namespace std;

 

// Call observer->LightMoved(light) for all observers

for_each(obsFirst, obsLast, bind2nd(mem_fun(&Observer::LightMoved), light));

Bleh. I just think that comment makes it all the more obvious that there is something wrong with the code.

Keep it simple, stupid

There was a time in my development as a programmer where I really cared if people thought I was being clever, and had beguiled myself into thinking that the intelligence of a programmer was best demonstrated by the complexity of his code. I mean, hey, if I’m writing code that just any programmer can easily understand then I mustn’t be that advanced, right? Besides, it’s their job as engineers to keep up on the latest libraries or standards so they can speak my language anyways. Can’t easily understand my code? Pfft … then you mustn’t be very 1337.

I’m embarrassed that I ever used to think that way. Writing software is hard. So hard, in fact, that I’d wager a majority of programmers are on software projects that will fail. Some of that failure may be out of our direct influence, but we can at least control the complexity of our own work, and make it easy to understand, test, and (gulp) modify by our fellow peers.

The following code may not convince my teammates that I’m particularly smart or gifted …

for (ObserverList::iterator itr = first; itr != last; ++itr)

{

    Observer * observer = *itr;

    observer->LightMoved(light);

}

… but they will at least quickly understand what it does, and perhaps be grateful for that much.

{ 3 comments }