I have been working on multiple Unity 3D projects, and I often used the same setup for many of them. I needed to reuse my Infrastructure library between the projects. I wanted that when I update a tool in the infrastructure, all the projects receive that change as well. The solution that I used is Git Submodules.
How to split the main project and the submodule?
The submodule is a sub repo in the main repo, therefore I chose it to be a part of the Assets folder. This is how it is placed it in my main project.
The submodule is a regular git repo. What determines it's use as submodule is the main project git config.
How to config it?
When you open the Unity project, and create a repo for it, you can connect the submodule and the branch to track as following:
git submodule add -b master <remote_url> <destination_folder>
Also stage the submodule addition and commit it:
git commit -m "Added the submodule to the project."
You can pull from it in two ways:
1. git submodule update --init --recursive (the recursive flag is to have it update inner submodules)
2. pull from it manually as a secondary git repo.
How do I work with it?
You can update the submodule's code, but make sure that it does not have any dependency to the main project. You should use assembly definitions to enforce this. Your main project will have dependency to the submodule. It is best to do it through defined API that are closed to change, so when you make changes to the submodule implementations it will not break other existing projects that use it.
What's the catch?
There is a small overhead when working with the submodule. When you pull the main repo, the submodule is not going to update itself, so you would need to pull from multiple repos. To update the submodule by calling submodule update (or pull from it manually) git submodule update --remote --merge
This extra work however, is quite simple to automate. My method is using a custom git command that pulls from the main repo and makes the submodule updates behind the scenes each time I pull.
Commentaires