The joys of submodule-ing in Unity

So Robert Yang posted a decent guide on how to ‘sub-project’ your Unity projects so they can share a common code-base easily. This does what it needs to do pretty well, you can easily add to and maintain your own framework, and build individual games on top of it. This, as he warns, comes with some complications. You have to juggle different build/player/editor/project settings, each game’s /Resources folder will be included with every build, and you need to manage an ever-growing script base. I manage my shared code a different way, though the magic of Git submodules.

Screen Shot 2013-04-10 at 9.52.51 PM

(Note: This method needs a non-Unity version control, so you need Unity Pro to make it work without freaking out. If you’re flying solo you might be able to get by with Unity Indie, but you’ve been warned)

Essentially, I do the complete opposite of Robert’s subproject system. I’ve built up a small set of useful ‘plugins’, mostly code but also models, shaders and materials. I keep each of these separate, and add them to specific games where needed. Then, if I update a plugin while working on a game, I can push the useful changes back to the ‘master’ plugin and get the updates in all the games that use them. You can do this outside of version control (with Unity packages), but you need to be disciplined about keeping the master updated, or changes within each game will become too unwieldy to merge together again. Git submodules let you pull and push changes back and forth between the master easily, while handling changes and merges automatically (mostly).

You’re all using some sort of version control, right? You already know you should, so I’m not going to go over the many many reasons why. I use and recommend Git. Other version control systems may have something similar to Git submodules (notably SVN’s sub-projects), but I’m not familiar with their utility.

Git submodules aren’t without their flaws. Most of all, they’re delicate little flowers that break at the drop of a mistyped terminal command. They work in a confusing way, and it can be difficult to understand just what’s happening. That said, if you use them you have to use Git, which makes it hard to really fuck up a project if things go wrong.

I think I need to put a detailed guide up at some point, on this and other Unity + Git best-practices… Until then, shoot if you have any questions!

3 thoughts on “The joys of submodule-ing in Unity”

  1. I just made a forum post about the best way to go for sharing a common code-base using Git. How easy is this to set up and maintain? Do you still plan on doing a tutorial on this at some point for those new to Git?

    1. Yes, after some tricky setup, they’re very easy to maintain, and only get complicated if something goes wrong. I’m finishing up the last two parts of my git tutorial now, the next covers submodules, hopefully done by the weekend.

      1. Great! I actually missed your git tutorial the first time around somehow. Going to go through it now to scrub up on the basics so i am ready for the crucial part when you are done! Thanks a million!

Comments are closed.