Skip to content

Instantly share code, notes, and snippets.

@maxahn
Last active April 5, 2024 05:24
Show Gist options
  • Save maxahn/3b4c78d276d5f409b69da6e8610b2ef2 to your computer and use it in GitHub Desktop.
Save maxahn/3b4c78d276d5f409b69da6e8610b2ef2 to your computer and use it in GitHub Desktop.
Merging Unity Scenes with Smart Merge and Perforce

Unity has a built in tool called UnityYAMLMerge that can be used to automatically merge scene files and prefabs. The tool by itself is limited in that it cannot resolve conflicts when collaborators edit the same game object in a scene though it works fine when adding, deleting, and editing different objects. To resolve the merge conflicts Smart Merge can't handle, the user must install and enable a fallback tool to manually fix the conflict themselves.

  1. Add the following lines to your local .git/config file.
[merge] 
     tool = unityyamlmerge 
[mergetool "unityyamlmerge"] 
     trustExitCode = false 
     keepTemporaries = true 
     keepBackup = false 
     path = C:/Program Files/Unity/Editor/Data/Tools/UnityYAMLMerge.exe 
     cmd = \"C:/Program Files/Unity/Editor/Data/Tools/UnityYAMLMerge.exe\" merge -p "$BASE" "$REMOTE" "$LOCAL" "$MERGED"`

Note: The path to your UnityYAMLMerge.exe file may differ (If it's not in Program Files, it may be in Program Files (x86), etc).

  1. Download and install the Perforce Helix Visual Client. There are other merge tools but this one is nice since it's free (for teams less than 5 at least).

  2. Open up the mergespecfile.txt which can be found in the Unity install Directory (C:\Program Files\Unity\Editor\Data\Tools). There will be two lines that aren't commented out that look something like this:

unity use ...
prefab use ...

Change it to the following:

unity use "C:\Program Files\Perforce\p4merge.exe" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
prefab use "C:\Program Files\Perforce\p4merge.exe" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
Change the directory if you installed Perforce elsewhere. 

Note: If you run into issues where you don't have permissions to change the mergespecfile.txt, you can right click, go to Properties, Security tab, and then click on the Edit... button to change permissions. From there you can check off the allow boxes for Full control and Modify for your user. This will allow you to change and save the file.

  1. Go to Unity's Editor Settings (Edit > Project Settings > Editor), and change the Asset Serialization's Mode to Force Text.

  2. Now when a merge conflict arises within a scene, you can use the command:

git mergetools

And it will launch the Perforce Helix Visual Client and you can choose which changes you want before saving and closing the client.

  1. Voila, the scene should now be merged!

Sources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment