うにてぃブログ

主にUnityとC#に関する記事を書いていきます

【Unity】JobSystems のエラー箇所を知る

デフォルトの設定では JobSystems 側でエラーログが発生したとしても以下のように細かい情報が表示されない

System.IndexOutOfRangeException: Index {0} is out of range of '{1}' Length.
This Exception was thrown from a job compiled with Burst, which has limited exception support.
 #3 Unity.Jobs.IJobParallelForExtensions.ParallelForJobStruct`1<TextureJob>.Execute
 #4 ExecuteJobCopyData(ManagedJobData*, void (*)(void*, void*, void*, void*, int), int)
 #5 ForwardJobForEachToManaged(ManagedJobData*, unsigned int)
 #6 JobQueue::Exec(JobInfo*, long long, int, bool)
 #7 JobQueue::ExecuteJobFromHighPriorityStack(bool, bool&)
 #8 JobQueue::ProcessJobs(JobQueue::ThreadInfo*, void*)
 #9 JobQueue::WorkLoop(void*)
 #10 Thread::RunThreadWrapper(void*)
 #11 _pthread_start
 #12 thread_start

表示させるためには、以下の処理でログの詳細を出すように設定する必要があります

こちらは保存されるようで一度この処理を呼び出してしまえば毎回呼び出す必要はありません

NativeLeakDetection.Mode = NativeLeakDetectionMode.Enabled;
NativeLeakDetection.Mode = NativeLeakDetectionMode.EnabledWithStackTrace;

上記を設定した状態で先程と同じエラーを確認してみると、ちゃんとエラー箇所が分かるようなログが表示されます

IndexOutOfRangeException: Index 4193938 is out of range of '4192256' Length.
Unity.Collections.NativeArray`1[T].FailOutOfRangeError (System.Int32 index) (at /Users/bokken/build/output/unity/unity/Runtime/Export/NativeArray/NativeArray.cs:289)
Unity.Collections.NativeArray`1[T].CheckElementWriteAccess (System.Int32 index) (at /Users/bokken/build/output/unity/unity/Runtime/Export/NativeArray/NativeArray.cs:148)
Unity.Collections.NativeArray`1[T].set_Item (System.Int32 index, T value) (at /Users/bokken/build/output/unity/unity/Runtime/Export/NativeArray/NativeArray.cs:167)
TextureJob.Execute (System.Int32 index) (at Assets/Script/TextureJob.cs:22)
Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct`1[T].Execute (T& jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at /Users/bokken/build/output/unity/unity/Runtime/Jobs/Managed/IJobParallelFor.cs:37)