Unity_Best_Practices

Best practices guide for Unity : Versioning, unit testing, TDD and Clean Architecture

View project on GitHub

Versioning : Git & Unity

You want to use git for your Unity project, but sometimes, Scenes or Prefabs crash !
In fact, two people editing the same file at the same time cause a conflict and by default, git don’t solve this problem.
You open your scene and you see your scene completely empty or your prefab all broken. A solution exists for that and is presented below.

Create a .gitignore

It is important to add a .gitignore file to your project. It will allow git to ignore some temporary files generated by Unity and your IDE. In some projects, it’s useful to add folders of your project to not save them in your online repository. For example, an asset of the asset store can weigh heavily in the repository, which slows considerably the team when it needs to update or retrieve the repository).

see .gitignore Unity

Store file in YAML

Image of Unity Project Settings

Change the asset serialization mode to “Force Text” (see picture above). This option converts the binaries (like Scene or Prefab) to YAML (“YAML Ain’t Markup Language”).

This allows Git to better manage conflict, but sometimes it may not be enough.

By default, Unity saves the files in binary format in order to have a project as light as possible. So it’s an optimization to use if you don’t want to version your project. Unity does not mind switching to YAML and advice for anyone uses versioning software such as Git.

Remember to make visible your .meta files (Version Control : “Visible Meta Files”). This will allow git to version your .meta files and save the settings of your objects ( all object you drag & drop in Unity is saved in a .meta file).

Merge with Smart Merge

Since Unity 5.0, an UnityYAMLMerge tool is included with its installation.
To use it, Copy/Paste the following lines in the .git/config file.

[merge]
tool = unityyamlmerge
[mergetool "unityyamlmerge"]
cmd = 'C:\\Program Files\\Unity\\Editor\\Data\\Tools\\UnityYAMLMerge.exe' merge -p "$BASE" "$REMOTE" "$LOCAL" "$MERGED"
trustExitCode = false
keepTempories = true
keepBackup = false

Change the “C:\ …” link to fit your Unity location. If you work in a team, it’s better that everyone uses the same link.
:warning: Warning : don’t forget to use ‘\\’ and no ‘\’ or ‘/’.

This merge tool can be used with other tools (Perforce, SVN, Mercurial, SourceTree), see documentation

This tool is very performing and allowed to work in team without the risk of losing scene or Prefab files.

How it’s works ?

Use git as usual and when your merge command crash, use the smartmerge command: “git mergetool”.

In this following example, two branches are creating “Master” and “Another”. Each branch modify “sceneTest” and commit. After, we try to merge and it crashes. And now, Our “sceneTest” is empty. Then we tap “git mergetool” and we find our scene with the twice modification.

Schema of Smart Merge

Limits of Smartmerge :

  • Modify the same object in a scene
  • Modify the same property of prefab

In this cases, you will be able to choose the best version (your scene or colleague scene).

Manage Large files

Versioning large files (.png, .fbx, …) can be hard. GitHub don’t accept files that weigh more than 100 MB and git keep all versions of all your files. Versioning large files, make clone and recuperation for another user longer.

Git imposed to each user to have as much free space as used space (For example, if your project is 1 GB, Git need 2 GB).
If your project is compatible with Git LFS, use it ! (Git LFS can be used with GitHub / Bitbucket / Gitlab) Git LFS is an open source git extension.

:warning: Warning, this solution works only for file less than 2 GB.

How git LFS works ?

Image of Git LFS

Git LFS track chosen files by name, extensions, folder location, …
Chosen files will be stocked in a LFS server automatically and git keep only links to their files. This system don’t versioning track files, but in most projects, assets 3D or 2D are already versioned by designers.

For install git LFS, write the command :

$ git lfs install

Git LFS is installed automatically and return this message :

>Updated git hooks.
>Git LFS initialized.

Use the following command to track your file (here all .png file)

$ git lfs track "*.extension" 
>Tracking "*.png"

Add a .gitattributes to your project

$ git add .gitattributes

If you use GitHub, the notification “stored with Git LFS” appears in your tracked file.

Image of Github Stored with Git LFS

To understand how Git LFS works, you can watch the following Bitbucket video:

video Bitbucket Git LFS

Another solution for large file in git project : Git annex, Git fat , Git media , Git Bigstore, Git sizer, … But they have little or no documentation and they don’t really adapt to Unity project’s needs.