【Unity】ビルドエラー(IOExceptionとBuildMethodException)の原因と対応手順

ネコニウム研究所

PCを利用したモノづくりに関連する情報を掲載するブログ

【Unity】ビルドエラー(IOExceptionとBuildMethodException)の原因と対応手順

2022-5-17 |

Unityでエディター上でのデバッグでは問題なく実行できるが、ビルド時にエラー(IOExceptionとBuildMethodException)が発生するのをなんとかしたい!

概要

この記事では、Unityでエディター上でのデバッグでは問題なく実行できるが、ビルド時にエラー(IOExceptionとBuildMethodException)が発生する原因(多分)と対応手順を掲載する。

仕様書

環境

  • Unity 2021.3.0f1

症状

ビルド時にエラー下記の4つのエラーが出力されて、ビルドに失敗してしまう。
例としてUnityのプロジェクトはC:\projectに配置されてるものとする。

まず1つ目、プロジェクト内の\Library\BuildPlayerData\Editorにアクセスできないらしい。

IOException: Access to the path '\\?\C:\project\Library\BuildPlayerData\Editor' is denied.
System.IO.FileSystem.RemoveDirectoryInternal (System.String fullPath, System.Boolean topLevel, System.Boolean allowDirectoryNotEmpty) (at <6073cf49ed704e958b8a66d540dea948>:0)
System.IO.FileSystem.RemoveDirectoryRecursive (System.String fullPath, Interop+Kernel32+WIN32_FIND_DATA& findData, System.Boolean topLevel) (at <6073cf49ed704e958b8a66d540dea948>:0)
System.IO.FileSystem.RemoveDirectory (System.String fullPath, System.Boolean recursive) (at <6073cf49ed704e958b8a66d540dea948>:0)
System.IO.Directory.Delete (System.String path, System.Boolean recursive) (at <6073cf49ed704e958b8a66d540dea948>:0)
UnityEditor.VisualStudioIntegration.DirectoryIOProvider.Delete (System.String path, System.Boolean recursive) (at <780782bc035845f9909cebbd4c983ae3>:0)
UnityEditor.Build.Player.BuildPlayerDataGenerator.CreateCleanFolder (System.Boolean isEditor) (at <780782bc035845f9909cebbd4c983ae3>:0)
UnityEditor.Build.Player.BuildPlayerDataGenerator.GenerateForAssemblies (System.String[] assemblies, System.String[] searchPaths, UnityEditor.BuildTarget buildTarget, System.Boolean isEditor) (at <780782bc035845f9909cebbd4c983ae3>:0)
UnityEditor.Build.Player.BuildPlayerDataGeneratorNativeInterface.GenerateForAssemblies (System.String[] assemblies, System.String[] searchPaths, UnityEditor.BuildTarget buildTarget, System.Boolean isEditor) (at <780782bc035845f9909cebbd4c983ae3>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

2つ目、謎。エディターとプレイヤーの互換性が無い?

Error building player because script class layout might be incompatible between the editor and the player.

3つ目、謎。UnityEngine.GUIUtility:ProcessEventがエラー?

Build completed with a result of 'Failed' in 0 seconds (163 ms)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

4つ目、大謎。UnityEditor.BuildPlayerWindow+BuildMethodExceptionという例外が発生してる。

UnityEditor.BuildPlayerWindow+BuildMethodException: 2 errors
  at UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer (UnityEditor.BuildPlayerOptions options) [0x002da] in <780782bc035845f9909cebbd4c983ae3>:0 
  at UnityEditor.BuildPlayerWindow.CallBuildMethods (System.Boolean askForBuildLocation, UnityEditor.BuildOptions defaultBuildOptions) [0x00080] in <780782bc035845f9909cebbd4c983ae3>:0 
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

なかなか解決せず、エディターの再起動を試した際に、エディターの起動直後に下記のエラーが出力されることに気付いた。

アクセスできずに消せないディレクトリがあるようだ。

Win32Exception: アクセスが拒否されました。

Rethrow as IOException: Cannot delete directory Library/Bee/artifacts/1900b0aE.dag/post-processed.
NiceIO.NPath+WindowsFileSystem.Directory_Delete (NiceIO.NPath path, System.Boolean recursive) (at <780782bc035845f9909cebbd4c983ae3>:0)
NiceIO.NPath+WindowsFileSystem.Directory_Delete (NiceIO.NPath path, System.Boolean recursive) (at <780782bc035845f9909cebbd4c983ae3>:0)
Rethrow as IOException: Cannot delete directory Library/Bee/artifacts/1900b0aE.dag.
NiceIO.NPath+WindowsFileSystem.Directory_Delete (NiceIO.NPath path, System.Boolean recursive) (at <780782bc035845f9909cebbd4c983ae3>:0)
NiceIO.NPath+WindowsFileSystem.Directory_Delete (NiceIO.NPath path, System.Boolean recursive) (at <780782bc035845f9909cebbd4c983ae3>:0)
Rethrow as IOException: Cannot delete directory Library/Bee/artifacts.
NiceIO.NPath+WindowsFileSystem.Directory_Delete (NiceIO.NPath path, System.Boolean recursive) (at <780782bc035845f9909cebbd4c983ae3>:0)
NiceIO.NPath+WindowsFileSystem.Directory_Delete (NiceIO.NPath path, System.Boolean recursive) (at <780782bc035845f9909cebbd4c983ae3>:0)
Rethrow as IOException: Cannot delete directory Library/Bee.
NiceIO.NPath+WindowsFileSystem.Directory_Delete (NiceIO.NPath path, System.Boolean recursive) (at <780782bc035845f9909cebbd4c983ae3>:0)
NiceIO.NPath.Delete (NiceIO.DeleteMode deleteMode) (at <780782bc035845f9909cebbd4c983ae3>:0)
UnityEditor.Scripting.ScriptCompilation.UnityBeeDriver.RecreateDagDirectoryIfNeeded (NiceIO.NPath dagDirectory) (at <780782bc035845f9909cebbd4c983ae3>:0)
UnityEditor.Scripting.ScriptCompilation.UnityBeeDriver.Make (Bee.BeeDriver.RunnableProgram buildProgram, System.String dagName, System.String dagDirectory, System.Boolean useScriptUpdater, System.String projectDirectory, Bee.BeeDriver.ProgressAPI progressAPI) (at <780782bc035845f9909cebbd4c983ae3>:0)
UnityEditor.Scripting.ScriptCompilation.UnityBeeDriver.Make (Bee.BeeDriver.RunnableProgram buildProgram, UnityEditor.Scripting.ScriptCompilation.EditorCompilation editorCompilation, System.String dagName, System.String dagDirectory, System.Boolean useScriptUpdater) (at <780782bc035845f9909cebbd4c983ae3>:0)
UnityEditor.Scripting.ScriptCompilation.EditorCompilation.CompileScriptsWithSettings (UnityEditor.Scripting.ScriptCompilation.ScriptAssemblySettings scriptAssemblySettings) (at <780782bc035845f9909cebbd4c983ae3>:0)
UnityEditor.Scripting.ScriptCompilation.EditorCompilation.CompileScripts (UnityEditor.Scripting.ScriptCompilation.EditorScriptCompilationOptions editorScriptCompilationOptions, UnityEditor.BuildTargetGroup platformGroup, UnityEditor.BuildTarget platform, System.Int32 subtarget, System.String[] extraScriptingDefines) (at <780782bc035845f9909cebbd4c983ae3>:0)
UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface+<>c__DisplayClass21_0.<CompileScripts>b__0 () (at <780782bc035845f9909cebbd4c983ae3>:0)
UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface.EmitExceptionAsError[T] (System.Func`1[TResult] func, T returnValue) (at <780782bc035845f9909cebbd4c983ae3>:0)
UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface:CompileScripts(EditorScriptCompilationOptions, BuildTargetGroup, BuildTarget, Int32, String[])

手順書

下記の3つディレクトリを削除したところ、無事にビルドできるようになった。

C:\project\Library\BuildPlayerData\Editor
C:\project\Library\BuildPlayerData\Player
C:\project\Library\Bee

削除したディレクトリは、ビルド時に再作成される。

原因(と思われるもの)

バックアップが面倒くさくて、Google Driveの同期ディレクトリにプロジェクトをまるごと保存してたのが、良くないみたい。

同期の処理でアクセス権がぐちゃぐちゃになってしまったと思われ。

まとめ(感想文)

Gitとかでバージョン管理しつつ、バックアップするのが良いかもね!