Version: 2020.3
将外部版本控制系统用于 Unity
多场景编辑

Smart merge

Use the UnityYAMLMerge tool to merge scene and prefab files in a semantically correct way. The tool can be accessed from the command line and is also available to third-party version control software.

Setting up smart merging in Unity

In the Version Control project settings (menu: Edit > Project Settings > Version Control), when you select a third-party version control tool in the Mode field, for example Perforce or PlasticSCM, the Smart Merge is displayed. The menu has the following options:

  • Off:仅使用偏好设置中设定的默认合并工具而不进行智能合并。
  • Premerge:启用智能合并,接受纯净的合并。不纯净的合并将创建文件的预先合并基础版本、他们的版本和我的版本。然后,对这些版本使用默认合并工具。
  • Ask:启用智能合并,但发生冲突时显示一个对话框让用户解决冲突(这是默认设置)。

Setting up UnityYAMLMerge for use with third-party tools

UnityYAMLMerge 工具随附于 Unity Editor;假设 Unity 安装在标准位置,则 UnityYAMLMerge 的路径将是:

C:\Program Files\Unity\Editor\Data\Tools\UnityYAMLMerge.exe

or

C:\Program Files (x86)\Unity\Editor\Data\Tools\UnityYAMLMerge.exe
  • 在 Windows 上;
/Applications/Unity/Unity.app/Contents/Tools/UnityYAMLMerge
  • 在 Mac OSX 上(使用 Finder 中的 Show Package Contents 命令访问此文件夹)。

UnityYAMLMerge 附带了一个默认的回退文件(称为 mergespecfile.txt,也在 Tools 文件夹中),用于指定如何处理未解决的冲突或未知文件。此外,还可将其用作不会根据文件扩展名自动选择合并工具的版本控制系统(例如 git)的主要合并工具。默认情况下,mergespecfile.txt 中已列出最常用的工具,但可以编辑此文件以添加新工具或更改选项。

You can run UnityYAMLMerge as a standalone tool from the command line (you can see full usage instructions by running it without any arguments). Setup instructions for common version control systems are given below.

P4V

  1. Go to Preferences > Merge.
  2. Select Other application.
  3. Click the Add button.
  4. In the extension field, type .unity.
  5. In the Application field, type the path to the UnityYAMLMerge tool (see above).
  6. In the Arguments field, type merge -p %b %1 %2 %r
  7. Click Save.

然后,按照相同的步骤添加 .prefab 扩展名。

Git

将以下文本添加到 .git.gitconfig 文件中:

    [merge]
        tool = unityyamlmerge

        [mergetool "unityyamlmerge"]
        trustExitCode = false
        cmd = '<path to UnityYAMLMerge>' merge -p "$BASE" "$REMOTE" "$LOCAL" "$MERGED"

Mercurial

将以下文本添加到 .hgrc 文件中:

    [merge-patterns]
        **.unity = unityyamlmerge
        **.prefab = unityyamlmerge

        [merge-tools]
        unityyamlmerge.executable = <path to UnityYAMLMerge>
        unityyamlmerge.args = merge -p --force $base $other $local $output
        unityyamlmerge.checkprompt = True
        unityyamlmerge.premerge = False
        unityyamlmerge.binary = False

SVN

将以下文本添加到 ~/.subversion/config 文件中:

    [helpers]
        merge-tool-cmd = <path to UnityYAMLMerge>

TortoiseGit

  1. Go to Preferences > Diff Viewer > Merge Tool and click the Advanced button.
  2. In the popup, type .unity in the extension field.
  3. In the External Program field type:
    <path to UnityYAMLMerge> merge -p %base %theirs %mine %merged

然后,按照相同的步骤添加 .prefab 扩展名。

PlasticSCM

  1. Go to Preferences > Merge Tools and click the Add button.
  2. Select External merge tool.
  3. Select Use with files that match the following pattern.
  4. Add the .unity extension.
  5. Enter the command:
    <path to UnityYAMLMerge> merge -p "@basefile" "@sourcefile"  "@destinationfile" "@output"

然后,按照相同的步骤添加 .prefab 扩展名。

SourceTree

  1. Go to Tools > Options > Diff.
  2. Select Custom in the Merge Tool dropdown.
  3. Type the path to UnityYAMLMerge in the Merge Command field.
  4. Type merge -p $BASE $REMOTE $LOCAL $MERGED in the Arguments field.

配置 mergerules.txt

要自定义 UnityYAMLMerge 合并文件的方式,请配置 mergerules.txt 文件。该文件位于您的 Unity 安装的 Editor/Data/Tools 文件夹中。 各种配置选项如下。

数组

arrays 配置部分告知 UnityYAMLMerge 将指定路径视为一个数组;或者作为带有键值的 “set”,或者作为没有键值的 “plain” 数组。所有数组的默认设置是采用混合模式并尝试与一些已知的启发式方法匹配。

  • <mode> is one of “set” or “plain”

示例(根据默认的 mergerules 文件)

[arrays]
set *.GameObject.m_Component *.fileID
set *.Prefab.m_Modification.m_Modifications target.fileID target.guid propertyPath
plain *.MeshRenderer.m_Materials
plain *.Renderer.m_Materials

Exclusions

exclusions 配置部分指示要从合并中排除哪些路径。如果双方都已修改,则它们将被视为冲突,并将显示给用户输入。

  • <mode> is one of “include”, “exclude”, “includeIfContains”, “excludeIfContains”

示例(根据默认的 mergerules 文件)

[exclusions]
exclude *.MeshRenderer.m_Materials.*
exclude *.SpriteRenderer.m_Materials
exclude *.SpriteRenderer.m_Color
include *.ParticleSystem.InitialModule
exclude *.ParticleSystem.*
exclude *.ParticleSystem.InitialModule.*
# excludeDepend *.MonoBehaviour m_Script ^m_
excludeIfContains *.MonoBehaviour.* x y z
excludeIfContains *.MonoBehaviour.* r g b

Comparisons

comparisons 部分考虑了浮点值之间可以忽略的差异,以忽略每个用户的设置。启用后,在浮点比较时会考虑相对误差。您可以按照以下方式配置比较:

  • “float” <float value to compare> (This sets epsilon to a default value close to machine epsilon)
  • “float” <float value to compare> <relative comparison value and absolute cutoff>
  • “float” <float value to compare> <relative comparison value> <absolute comparison cutoff>

相对比较值决定了 epsilon 以及比较将如何随着浮点数相对误差的大小而缩放。绝对比较截止值决定了在距离 0 点的哪个位置将浮点比较从绝对切换到相对(在图上)。

注意:比较值应该在 float epsilon(0.00000011921)和 1.0 之间

示例

[comparisons]
float *.Transform.m_LocalPosition.x 0.0000005
float *.Transform.m_LocalPosition.y 0.0000005
float *.Transform.m_LocalPosition.z 0.0000005
float *.Transform.m_LocalRotation.x 0.00005 0.001
float *.Transform.m_LocalRotation.y
float *.Transform.m_LocalRotation.z 0.00005 0.001
float *.Transform.m_LocalRotation.w
将外部版本控制系统用于 Unity
多场景编辑