Search Unity

Question Update name of sprites from a Texture2D with Sprite Mode Multiple by code?

Discussion in '2D' started by Mahjoub_GameDev, Sep 28, 2022.

  1. Mahjoub_GameDev

    Mahjoub_GameDev

    Joined:
    Dec 5, 2017
    Posts:
    3
    Hi everyone!

    Since sometimes we have to rename a sprite texture2D, it could be nice to also update sprites names which have already been generated from this texture.
    At least it was my attempt with the script below, however I was not able to make it working so far.

    Please, does anyone have some clue on how we could manage that by code?
    It will be super helpful!

    Thank you in advance :)

    Code (CSharp):
    1. [MenuItem("CONTEXT/TextureImporter/Update Sprite Name", priority = 999)]
    2.         private static void UpdateSpriteName(MenuCommand command)
    3.         {
    4.             TextureImporter textureImporter = (TextureImporter)command.context;
    5.             string path = AssetDatabase.GetAssetPath(textureImporter);
    6.             string fileName = Path.GetFileName(path).Replace(".png", "");
    7.             Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(path).OfType<Sprite>().ToArray();
    8.  
    9.             int index = 0;
    10.             foreach (Sprite sprite in sprites)
    11.             {
    12.                 sprite.name = $"{fileName}_{index++}";
    13.  
    14.                 EditorUtility.SetDirty(sprite);
    15.                 AssetDatabase.Refresh();
    16.                 AssetDatabase.SaveAssets();
    17.             }
    18.         }
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    You're better off asking on the 2D forum.

    I'll move your post for you.
     
    Mahjoub_GameDev likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    I think your attempt above is noble and wonderful but I think the reason it fails is that Unity only allows you to manipulate this type of data by writing a custom asset importer, in this case a custom importer for textures.

    How such a thing might interoperate with the existing sprite editor, etc., I have no idea.

    ANOTHER option is to go into the
    .meta
    file and manipulate the names directly there. I would do that with Unity closed so that it imports correctly upon reopen. Make sure it doesn't break any linkages.

    This 2-sprite image:

    Screen Shot 2022-09-28 at 12.35.26 PM.png

    Has this metafile.

    Screen Shot 2022-09-28 at 12.35.36 PM.png
     
    Mahjoub_GameDev likes this.
  4. Mahjoub_GameDev

    Mahjoub_GameDev

    Joined:
    Dec 5, 2017
    Posts:
    3
    Hi @Kurt-Dekker, thank you for you message :)

    Actually after looking a bit more into it, it seems that the following script is working all fine inside Unity. Quite happy to have that now :cool:

    Code (CSharp):
    1. [MenuItem("CONTEXT/TextureImporter/Update Sprite Name", priority = 999)]
    2.         private static void UpdateSpriteName(MenuCommand command)
    3.         {
    4.             TextureImporter textureImporter = (TextureImporter)command.context;
    5.             string path = AssetDatabase.GetAssetPath(textureImporter);
    6.             string fileName = Path.GetFileName(path).Replace(".png", "");
    7.  
    8.             SpriteMetaData[] spritesheet = textureImporter.spritesheet;
    9.             for (int i = 0; i < spritesheet.Length; i++)
    10.             {
    11.                 spritesheet[i].name = $"{fileName}_{i}";
    12.             }
    13.             textureImporter.spritesheet = spritesheet;
    14.  
    15.             EditorUtility.SetDirty(textureImporter);
    16.             AssetDatabase.Refresh();
    17.             AssetDatabase.SaveAssets();
    18.             textureImporter.SaveAndReimport();
    19.         }
     
    baptistee and bewox like this.
  5. bewox

    bewox

    Joined:
    Apr 13, 2018
    Posts:
    18
    Thank you. This really save my time.