问题

使用 Unity 命令行方式打包时,出现错误:Unity.exe 已停止工作 对话框。

环境

  • Windows 7
  • Unity 5.6.6f2

日志

打开 UnityEditor.log 在最后可以看到出错的信息:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Broken text PPtr in file(Assets/Prefabs/xxxxx.prefab). Local file identifier (1709307660265302) doesn't exist!
UnityEditor.AssetDatabase:Refresh(ImportAssetOptions)
UnityEditor.AssetDatabase:Refresh() (at C:\buildslave\unity\build\artifacts\generated\common\editor\AssetDatabaseBindings.gen.cs:266)
Builder:Build() (at Assets\Editor\Builder.cs:280)

[c:\buildslave\unity\build\runtime\serialize\transferfunctions\TextSerializeTraitsImpl.h line 90]
(Filename: Assets/Editor/Builder.cs Line: 280)

CheckConsistency: GameObject does not reference component MonoBehaviour. Fixing.
UnityEditor.AssetDatabase:Refresh(ImportAssetOptions)
UnityEditor.AssetDatabase:Refresh() (at C:\buildslave\unity\build\artifacts\generated\common\editor\AssetDatabaseBindings.gen.cs:266)
Builder:Build() (at Assets\Editor\Builder.cs:280)

[C:\buildslave\unity\build\Runtime/BaseClasses/GameObject.cpp line 1259]
(Filename: Assets/Editor/Builder.cs Line: 280)

========== OUTPUTING STACK TRACE ==================

0x00000001411EC912 (Unity) Transform::ApplySerializedToRuntimeData
0x00000001411F60CF (Unity) Transform::AwakeFromLoad
0x00000001411F6AD4 (Unity) UI::RectTransform::AwakeFromLoad
0x000000014091A023 (Unity) GameObject::FinalizeAddComponentInternal
0x000000014091C252 (Unity) Unity::Component::CheckConsistency
0x00000001411EC1C3 (Unity) Transform::CheckConsistency
0x00000001410914B0 (Unity) AwakeFromLoadQueue::InvokePersistentManagerAwake
0x00000001410949F9 (Unity) AwakeFromLoadQueue::PersistentManagerAwakeFromLoad
0x0000000141094BE6 (Unity) PersistentManager::IntegrateAllThreadedObjects
0x0000000141099420 (Unity) PersistentManager::LoadAndIntegrateAllPreallocatedObjects
0x000000014109B9DE (Unity) PersistentManager::ReadObject
0x000000014002F357 (Unity) PPtr<Object>::operator Object * __ptr64
0x00000001415FB4FE (Unity) NativeFormatImporter::EndImport
0x0000000140157811 (Unity) AssetDatabase::ImportAsset
0x0000000140162F34 (Unity) AssetDatabase::UpdateAsset
0x0000000140165AF4 (Unity) AssetInterface::ProcessAssetsImplementation
0x0000000140168BB7 (Unity) AssetInterface::StopAssetEditing
0x00000001401709FD (Unity) AssetInterface::Refresh
0x000000000BD51D25 (Mono JIT Code) (wrapper managed-to-native) UnityEditor.AssetDatabase:Refresh (UnityEditor.ImportAssetOptions)
0x000000000BD51C26 (Mono JIT Code) [C:\buildslave\unity\build\artifacts\generated\common\editor\AssetDatabaseBindings.gen.cs:266] UnityEditor.AssetDatabase:Refresh ()
0x00000000628A2C73 (Mono JIT Code) [C:\Project\Assets\Editor\Builder.cs:280] Builder:Build ()
0x000000000E390A8E (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void (object,intptr,intptr,intptr)
0x000007FEEC965CDF (mono) [c:\buildslave\mono\build\mono\mini\mini.c:4937] mono_jit_runtime_invoke
0x000007FEEC8B8499 (mono) [c:\buildslave\mono\build\mono\metadata\object.c:2623] mono_runtime_invoke
0x000000014107DC85 (Unity) scripting_method_invoke
0x0000000141073541 (Unity) ScriptingInvocation::Invoke
0x0000000141747F92 (Unity) Application::ParseARGVCommands
0x000000014174D4C7 (Unity) Application::FinishLoadingProject
0x000000014181B09B (Unity) WinMain
0x0000000141B06734 (Unity) strnlen
0x0000000076F9652D (kernel32) BaseThreadInitThunk
0x00000000771CC521 (ntdll) RtlUserThreadStart

========== END OF STACKTRACE ===========

分析与解决方案

Prefab 合并策略

通过日志可以发现是 Prefab 内的数据有错误导致。项目内使用 Force Text 选项序列化 Unity 生成的资源,在使用版本控制软件在分支合并时错误地将其认为文本文件,然后进行按行合并导致出错。

应该设置使用的版本控制软件将 Prefab 场景等 Unity 生成的文件设置为合并时不要自动处理,两边有修改时直接设置为冲突状态,让人手工解决冲突。

如果使用的版本控制软件是 Git,那么可以参考:Unity 项目中的 Git 属性 - 狂飙

应用已停止工作

出现 Unity.exe 已停止工作 提示时,Windows 并未终止 Unity 程序,导致打包脚本认为 Unity 程序依然在运行,打包未失败。

但是实际上已经完全失败了,上层脚本无法获取到当前进程状态,因而一直卡死在这个状态中。

需要修改注册表,将操作系统默认的行为改为不显示错误对话框,直接退出应用程序。

打包机应该设置为此行为,以防其他程序执行时卡死造成打包失败。

修改注册表,将其保存为 DontShowWindowsErrorReportingUI.reg 双击导入即可:

1
2
3
4
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\Windows Error Reporting]
"DontShowUI"=dword:00000001