Ayfel / PrefabLightmapping

Script for saving lightmapping data to prefabs. Used through the Assets tab in Unity. Place your prefbas in the scene with this script at the root. Set up your lighting and in the editor go to Assets->Bake Prefab Lightmaps. After is processed you can now spawn your prefabs in different scenes and they will use the lightmapping from the original scene.
618 stars 88 forks source link

"IndexOutOfRangeException: Index was outside the bounds of the array." on MaterialPostprocessor. #54

Closed Project-NSX closed 2 years ago

Project-NSX commented 2 years ago

Hello! Your script has helped me immensely, thank you very much. I do have an issue that occurs whenever a scene is saved with a the light baked onto a prefab though. The error I get is: IndexOutOfRangeException: Index was outside the bounds of the array. UnityEditor.Rendering.Universal.MaterialPostprocessor.OnPostprocessAllAssets (System.String[] importedAssets, System.String[] deletedAssets, System.String[] movedAssets, System.String[] movedFromAssetPaths) (at Library/PackageCache/com.unity.render-pipelines.universal@10.6.0/Editor/AssetPostProcessors/MaterialPostprocessor.cs:172) System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <695d1cc93cca45069c528c15c9fdd749>:0) Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation. System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <695d1cc93cca45069c528c15c9fdd749>:0) UnityEditor.AssetPostprocessingInternal.InvokeMethod (System.Reflection.MethodInfo method, System.Object[] args) (at <83a38f5397d94e36a22efa3369c82390>:0) UnityEditor.AssetPostprocessingInternal.PostprocessAllAssets (System.String[] importedAssets, System.String[] addedAssets, System.String[] deletedAssets, System.String[] movedAssets, System.String[] movedFromPathAssets) (at <83a38f5397d94e36a22efa3369c82390>:0) UnityEditor.EditorApplication:Internal_CallGlobalEventHandler()

I have tried deleting the script from the prefab, clearing the lightmap data, placing the script back onto the prefab and re-baking the lighting onto the prefab. Sadly this did not help and the issue persists. The project is a URP project and the prefabs I am baking to are prefab variants. Does anyone have any idea about a cause or solution to this issue?

Thanks!

Project-NSX commented 2 years ago

After some testing replacing the materials on the prefab's models fixes this issue. Even if the material is exactly the same as the previous one. Any help with this would be greatly appreciated. I'd rather not replace the materials on the many elements of the map.

Ayfel commented 2 years ago

Oh strange, from the error it seems to be from the prefab saving, maybe if they are prefab variants?

I am a bit in the dark here, can you share pictures or a package or something? This error comes from the saving system in Unity so something could be corrupted but its hard to tell what

Project-NSX commented 2 years ago

Oh sorry, I forgot to mention. This issue happens with normal prefabs as well as prefab variants. I stripped things right back to iron out exactly where the issue is.

Here's a package with the issue still present in it. https://we.tl/t-gu2qML4OTy This package was made in 2020.3.20f1 and requires a URP project. Baking the lighting onto the prefab and then saving the scene (or trying to build, or put the prefab in a scene and saving that scene) shows this error.

I imagine the issue present in this package is on all of the materials that are in the package that provides the roads. Thanks very much.

Edit: having tested some more with the project I created the package from it doesn't consistently produce the error anymore, which may make reproducing it an issue.

Ayfel commented 2 years ago

It seems to be an issue with the version of the materials you created, I wonder if you created these materials in a previous old version of the RenderPipeline because they are showing to be version -1 and the new Renderpipeline version checker fails with it. Definitely seems like an issue with those specific materials

Project-NSX commented 2 years ago

Ahh, this makes a lot of sense. The materials were part of an assets package that we bought and brought into the project. To solve the issue I opened the materials in notepad++ and changed "version: -1" to "version: 4"

Thanks very much for the help and for creating the script!