Custom Sprite split (SpriteMetaData) by JSON - Issues

Hello!

I’m trying to create a custom script to split sprite by JSON data, but after the second usage (to update sprite asset) I receive warning like:

Identifier uniqueness violation: ‘Name:Hell-Limbus-Security-Desk, Type:Sprite, FileId:0’. Multiple Objects with the same FileId are generated by this Importer. There is no guarantee that subsequent imports of this asset will properly re-link to these targets. UnityEditor.AssetImporter:SaveAndReimport ()

And some previously added sprites are became Missed.

Here is my code:

```csharp

  •    [MenuItem("FF Tools/Import Json Slices")]
      public static void Slice()
      {
          var texture = Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets).First();
          var path = AssetDatabase.GetAssetPath(texture);
          var sjson = AssetDatabase.LoadAssetAtPath<TextAsset>(path.Replace(".png", ".json"));
    
          var ti = AssetImporter.GetAtPath(path) as TextureImporter;
          if (ti == null || ti.spriteImportMode != SpriteImportMode.Multiple)
              return;
    
          var spritesheet = new List<SpriteMetaData>();
    
          /* ... json data preparation here  */
         
          foreach (var slice in slices.Values)
          {
              var name = slice["name"].Value;
              var bounds = slice["keys"][0]["bounds"];
              var pivot = slice["keys"][0]["pivot"];
              var rect = BoundsToRect(w, h, bounds);
    
              var item = new SpriteMetaData
              {
                  pivot = new Vector2(0.5f, 0.5f),
                  alignment = (int)SpriteAlignment.Custom,
                  name = $"{layerName}-{name}",
                  rect = rect,
                  border = Vector4.zero
              };
             
              spritesheet.Add(item);
          }
    
          ti.spritesheet = spritesheet.ToArray();
          ti.SaveAndReimport();
      }*
    

```

Also I noticed that for some reason in .meta file TextureImporter: internalIDToNameTable: - field not filed with a new added sprites names/ids.

Unity 2021.2.0b9

Appreciate any help! Thanks!

Looks like fixed in 2021.2.0b11

Looks like error is back in 2021.2.0b16

I am encountering a very similar scenario.

Similar behaviour when renaming sprites in multiple textures. Moreover, when updating Unity from 2020 to 2021 and vice versa you’ll get missed sprite references.
Also I’ve found this reply saying you’re a bad guy when you use TextureImporter

@hippogames Do you have some workaround for missed sprite references when updating from 2020 to 2021?

Hi! In my case the reason was in this script. It copies internal IDs when copying sprite sheet meta, thus they get duplicated. Different Unity versions have different resolvers for such situations. I had to delete all sprite meta and reimport sprite sheets manually.
https://discussions.unity.com/t/568921

'kay, I was able to fix this issue in my project using this pair of scripts: Fix Sprite references in Unity 2020.3 · GitHub

There were two types of sprite references and only one type was properly handled by 2021.3. Don’t know how I wound up with the second type of references, maybe leftovers after 2019? I was just able to translate the wrong references to the right ones.

The new Unity versions have changed the way it allows to create new sprites.

In my tests, you can still change sprites data using SpriteMetaData and setting them directly into TextureImporter.spritesheet but you cannot create new ones using this approach.

To create/remove sprites, use the new Sprite Editor Data Provider API. Here are some code samples.

1 Like