Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Custom Sprite split (SpriteMetaData) by JSON - Issues

Discussion in 'Editor & General Support' started by epifun, Sep 6, 2021.

  1. epifun

    epifun

    Joined:
    Oct 27, 2020
    Posts:
    6
    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:
    Code (CSharp):
    1.        
    2.         [MenuItem("FF Tools/Import Json Slices")]
    3.         public static void Slice()
    4.         {
    5.             var texture = Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets).First();
    6.             var path = AssetDatabase.GetAssetPath(texture);
    7.             var sjson = AssetDatabase.LoadAssetAtPath<TextAsset>(path.Replace(".png", ".json"));
    8.      
    9.             var ti = AssetImporter.GetAtPath(path) as TextureImporter;
    10.             if (ti == null || ti.spriteImportMode != SpriteImportMode.Multiple)
    11.                 return;
    12.  
    13.             var spritesheet = new List<SpriteMetaData>();
    14.  
    15.             /* ... json data preparation here  */
    16.            
    17.             foreach (var slice in slices.Values)
    18.             {
    19.                 var name = slice["name"].Value;
    20.                 var bounds = slice["keys"][0]["bounds"];
    21.                 var pivot = slice["keys"][0]["pivot"];
    22.                 var rect = BoundsToRect(w, h, bounds);
    23.  
    24.                 var item = new SpriteMetaData
    25.                 {
    26.                     pivot = new Vector2(0.5f, 0.5f),
    27.                     alignment = (int)SpriteAlignment.Custom,
    28.                     name = $"{layerName}-{name}",
    29.                     rect = rect,
    30.                     border = Vector4.zero
    31.                 };
    32.                
    33.                 spritesheet.Add(item);
    34.             }
    35.  
    36.             ti.spritesheet = spritesheet.ToArray();
    37.             ti.SaveAndReimport();
    38.         }


    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!
     
    Last edited: Sep 6, 2021
  2. epifun

    epifun

    Joined:
    Oct 27, 2020
    Posts:
    6
    Looks like fixed in 2021.2.0b11
     
  3. epifun

    epifun

    Joined:
    Oct 27, 2020
    Posts:
    6
    Looks like error is back in 2021.2.0b16
     
  4. Dreoh

    Dreoh

    Joined:
    Aug 15, 2012
    Posts:
    15
    I am encountering a very similar scenario.
     
  5. hippogames

    hippogames

    Joined:
    Feb 5, 2015
    Posts:
    228
  6. sfider

    sfider

    Joined:
    Jan 10, 2019
    Posts:
    9
    @hippogames Do you have some workaround for missed sprite references when updating from 2020 to 2021?
     
  7. hippogames

    hippogames

    Joined:
    Feb 5, 2015
    Posts:
    228
    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://forum.unity.com/threads/copy-spritesheet-slices-and-pivots-solved.301340/
     
  8. sfider

    sfider

    Joined:
    Jan 10, 2019
    Posts:
    9
    'kay, I was able to fix this issue in my project using this pair of scripts: https://gist.github.com/sfider/41d2af47db9a4da7d8dfcf96d99b9d1f

    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.
     
  9. hyagogow

    hyagogow

    Joined:
    Apr 25, 2014
    Posts:
    26
    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.
     
    ms502040 likes this.