Search Unity

Bug Weird Exception in Editor/SpritePostProcess.cs

Discussion in 'Editor & General Support' started by lorewap3, Apr 13, 2021.

  1. lorewap3

    lorewap3

    Joined:
    Jun 24, 2020
    Posts:
    58
    I can't say I'm sure this is a bug, but the behavior is very strange and I'm not sure how to fix it. I'm using the code from this asset to generate a tile atlas texture dynamically.
    https://assetstore.unity.com/packag...tileset-generator-automatic-rule-tiles-149809

    Everything works fine. The texture/sprites are all created fine and seem to have no other issue than the fact that I get this error every time:
    Code (CSharp):
    1. InvalidOperationException: Sequence contains no matching element
    2. System.Linq.Enumerable.First[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] predicate) (at <351e49e2a5bf4fd6beabb458ce2255f3>:0)
    3. UnityEditor.U2D.Animation.SpritePostProcess.PostProcessBoneData (UnityEditor.U2D.Sprites.ISpriteEditorDataProvider spriteDataProvider, System.Single definitionScale, UnityEngine.Sprite[] sprites) (at Library/PackageCache/com.unity.2d.animation@3.2.5/Editor/SpritePostProcess.cs:107)
    4. UnityEditor.U2D.Animation.SpritePostProcess.OnPostprocessSprites (UnityEngine.Texture2D texture, UnityEngine.Sprite[] sprites) (at Library/PackageCache/com.unity.2d.animation@3.2.5/Editor/SpritePostProcess.cs:29)
    5. System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
    6. Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    7. System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
    8. System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
    9. UnityEditor.AssetPostprocessingInternal.InvokeMethodIfAvailable (System.Object target, System.String methodName, System.Object[] args) (at <5984c823338e4ca69b7a0ca01115425e>:0)
    10. UnityEditor.AssetPostprocessingInternal.CallPostProcessMethods (System.String methodName, System.Object[] args) (at <5984c823338e4ca69b7a0ca01115425e>:0)
    11. UnityEditor.AssetPostprocessingInternal.PostprocessSprites (UnityEngine.Texture2D tex, System.String pathName, UnityEngine.Sprite[] sprites) (at <5984c823338e4ca69b7a0ca01115425e>:0)
    12. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
    13.  
    But there's no bones at all in the texture I'm importing. TileGen came with a SpriteProcessor to handle the import process, so it doesn't need to call the built-in one at all.... but I'm not sure how to turn that off.

    The offending line in SpritePostProcess.cs is this:
    SpriteRect spriteRect = spriteDataProvider.GetSpriteRects().First(s => { return s.spriteID == guid; });


    I tried adding a try/catch around it since I really don't care about the error, yet of course it just re-imports the code when you re-open Unity.

    Here's a SS of the SpriteEditor. All of the sprites have Rects, Names, etc. I don't know what it's complaining about. I also get the error everytime I apply the settings in the Sprite Editor.

    Any assistance would be very much appreciated!

    upload_2021-4-12_19-47-38.png
     
  2. Johste

    Johste

    Joined:
    Jul 12, 2014
    Posts:
    18
    I have the same issue. Did you find a solution in the end?
     
  3. lorewap3

    lorewap3

    Joined:
    Jun 24, 2020
    Posts:
    58
    @Johste

    I'm afraid not :(. I ended up just adding code to automatically clear the console afterwards. A ridiculous band-aid I know, but it hasn't been worth the time to debug further.
     
  4. Jubessin

    Jubessin

    Joined:
    Mar 31, 2020
    Posts:
    1
    After struggling through this same problem for a bit, I was able to find a solution to it.

    It seems that when sprites are sliced, each spriteRect is given a spriteID that is then stored within an ISpriteEditorDataProvider object.

    However when the slicing is done via custom code (e.g., through an AssetPostprocesser), the spriteID assigned to each spriteRect differs from the one stored in the ISpriteEditorDataProvider.

    For anyone else who ends up experiencing this issue, I was able to solve it by doing this:

    Code (CSharp):
    1. private void OnPostprocessSprites(Texture2D texture, Sprite[] sprites)
    2. {
    3.     var dpf = new SpriteDataProviderFactories();
    4.  
    5.     dpf.Init();
    6.  
    7.     ISpriteEditorDataProvider dp = dpf.GetSpriteEditorDataProviderFromObject(AssetImporter.GetAtPath(assetPath));
    8.  
    9.     if (dp != null)
    10.     {
    11.         dp.InitSpriteEditorDataProvider();
    12.  
    13.         var rects = dp.GetSpriteRects();
    14.  
    15.         for (int i = 0; i < rects.Length && i < sprites.Length; ++i)
    16.         {
    17.             sprites[i].SetSpriteID(rects[i].spriteID);
    18.         }
    19.  
    20.         dp.Apply();
    21.     }
    22. }

    You'll also need to include these using directives:

    using UnityEditor.U2D;
    using UnityEditor.U2D.Sprites;