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

Smart Merge

Unity 包含一个名为 UnityYAMLMerge 的工具,能够以语义正确的方式合并场景和预制件文件。可从命令行中访问该工具,也可将其用于第三方版本控制软件。

在 Unity 中设置智能合并

Editor 窗口(菜单:__Edit > Project Settings__,然后选择 Editor 类别)中,可以选择第三方版本控制工具(例如 Perforce 或 PlasticSCM)。启用其中一个工具后,将在 Version Control 标题下看到 Smart Merge 菜单。该菜单具有四个选项:

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

设置 UnityYAMLMerge 与第三方工具结合使用

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 中已列出最常用的工具,但可以编辑此文件以添加新工具或更改选项。

可从命令行中将 UnityYAMLMerge 作为独立工具运行(若要查看该工具的完整使用说明,可在运行时不带任何参数)。下面给出了常见版本控制系统的设置说明。

P4V

  • 选择 Preferences > Merge。
  • 选择 Other application
  • 单击 Add 按钮。
  • 在 Extension 字段中,输入 .unity
  • 在 Application 字段中,输入 UnityYAMLMerge 工具的路径(见下文)。
  • 在 Arguments 字段中,输入 merge -p %b %1 %2 %r
  • 单击 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

  • 选择 Preferences > Diff Viewer > Merge Tool,然后单击 Advanced 按钮。
  • 在弹出框的 Extension 字段中,输入 .unity
  • 在 External Program 字段中,输入:
    <path to UnityYAMLMerge> merge -p %base %theirs %mine %merged

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

PlasticSCM

  • 选择 Preferences > Merge Tools,然后单击 Add 按钮。
  • 选择 External 合并工具。
  • 选择 Use with files that match the following pattern
  • 添加 .unity 扩展名。
  • 输入以下命令:
    <path to UnityYAMLMerge> merge -p "@basefile" "@sourcefile"  "@destinationfile" "@output"

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

SourceTree

  • 选择 Tools > Options > Diff。
  • 在 Merge Tool 下拉选单中选择 Custom
  • Merge Command 文本字段中,输入 UnityYAMLMerge 的路径。
  • Arguments 文本字段中,输入 merge -p $BASE $REMOTE $LOCAL $MERGED

Configuring mergerules.txt

To customize how UnityYAMLMerge merges files, configure the mergerules.txt file. This is available in the Editor/Data/Tools folder of your Unity installation. The various configuration options are as follows.

数组

The arrays configuration section tells UnityYAMLMerge to treat the specified path as an array; either as a “set” with a key value, or as a “plain” array without key values. The default for all arrays is to do a hybrid mode and try to match with some known heuristics.

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

Example (as per default mergerules file)

[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

The exclusions configuration section indicates which paths to exclude from merging. If both sides have been modified, they are then treated as a conflict and will show up for user input.

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

Example (as per default mergerules file)

[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

The comparisons section takes into account negligible differences between float values to ignore per user settings. When enabled, floating point comparison is done relatively to account for relative error. You can configure comparison in the following ways:

  • “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>

The relative comparison value determines epsilon and how comparison will scale with the size of the floats’ relative error. The absolute comparison cutoff determines at which point float comparison switches from absolute to relative from zero (on a graph).

Note: Comparison values should be between float epsilon (~0.00000011921) and 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
多场景编辑