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

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 :slight_smile:

[MenuItem("CONTEXT/TextureImporter/Update Sprite Name", priority = 999)]
        private static void UpdateSpriteName(MenuCommand command)
        {
            TextureImporter textureImporter = (TextureImporter)command.context;
            string path = AssetDatabase.GetAssetPath(textureImporter);
            string fileName = Path.GetFileName(path).Replace(".png", "");
            Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(path).OfType<Sprite>().ToArray();

            int index = 0;
            foreach (Sprite sprite in sprites)
            {
                sprite.name = $"{fileName}_{index++}";

                EditorUtility.SetDirty(sprite);
                AssetDatabase.Refresh();
                AssetDatabase.SaveAssets();
            }
        }

You’re better off asking on the 2D forum.

I’ll move your post for you.

1 Like

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:

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

Has this metafile.

8473676--1126166--Screen Shot 2022-09-28 at 12.35.36 PM.png

1 Like

Hi @Kurt-Dekker , thank you for you message :slight_smile:

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 :sunglasses:

[MenuItem("CONTEXT/TextureImporter/Update Sprite Name", priority = 999)]
        private static void UpdateSpriteName(MenuCommand command)
        {
            TextureImporter textureImporter = (TextureImporter)command.context;
            string path = AssetDatabase.GetAssetPath(textureImporter);
            string fileName = Path.GetFileName(path).Replace(".png", "");

            SpriteMetaData[] spritesheet = textureImporter.spritesheet;
            for (int i = 0; i < spritesheet.Length; i++)
            {
                spritesheet[i].name = $"{fileName}_{i}";
            }
            textureImporter.spritesheet = spritesheet;

            EditorUtility.SetDirty(textureImporter);
            AssetDatabase.Refresh();
            AssetDatabase.SaveAssets();
            textureImporter.SaveAndReimport();
        }
2 Likes

Thank you. This really save my time.