We have two projects underway and want to share some classes between them. The shared source must be in a single location so when a programmer on Project1 updates the code those changes are immediately seen in Project2.
The important part is that we do not want to maintain multiple copies of the same code. i think we would agree that its simply bad practise to do so.
The only option appears to be putting the code in a class library and load it as a plugin into each project. But I don't believe this works when referencing Unity types like GameObject or transform.
In C/C++ land we could put the source in a directory and include it into any solution we wanted - doesn't seem to be so easy in Unity-land. Is their a better way to share code between unity projects?
EDIT: All projects, including the shared code, are already under source control. This is not a Source Control question, its a framework issue. (And obviously I need to write better questions!)
Lets say the directory structure looks like this...
Can Unity reference code from other projects? Can Unity reference code outside of its own Assets folder? If so, how?
You can share code (and assets like prefabs) effectively like this:
Use external versioning support. (I'll use subversion in this example, but most versioncontrol systems support this scenario).
Version your game at:
Version your shared code at:
Now, in the checkout of the game, create a folder called "SharedCode", and set its svn:external property to the svn url of your shared code svn location.
This way you can easily share your "common" code amongst different games. If your needs get a bit more complex, you can also branch your sharedcode project to something like:
if you want to "freeze" the version of the sharedcode that the monkeydonkey game uses. (for instance, when a game gets close to shipping, but you have another game in full production, it's likely that you don't want to the sharedcode updates to be picked up by your almost shipping game, due to risks of accidentally introducing a bug).
I find this to work pretty well myself, as you can easily commit changes to both your game, and the engine code.
answered May 14 '10 at 05:45 PM
Lucas Meijer 1 ♦♦
The link provided by @Cyclops may provide part of the answer. Sharing the code could be accomplished through a clever use of source control.
Your scripts (and potentially the whole project if you have pro) should be under source control already. Consider moving all shared code into a single root folder. Keep this folder out of your source control by ignoring it. Create a new repository just for this shared code.
With this model, you can develop in both projects making changes to project-specific and shared code. The only extra work comes when its time to check-in and sync your changes to the server. Instead of only doing it once, you'll do it for the project specific repository and the shared one.
When working on either project, always remember to check out the latest version of the shared code and you should have a single point for managing all that code.
answered May 07 '10 at 11:22 PM
Michael La Voie