2D Toolkit Forum

2D Toolkit => Support => Topic started by: radu on December 19, 2014, 06:55:28 pm

Title: Script to rename sprites in collection
Post by: radu on December 19, 2014, 06:55:28 pm
I have an editor script that retrieves a sprite collection to do some mass renaming. The renaming seems to work but it's not reflected on the tk2d editors.

I checked the sprite collection data file on disk and the names are changed.

I'm guessing I need to run something to force a refresh/rebuild...

Here's my code:

Code: [Select]
[MenuItem("Blah/Mass Rename/Idle Sprites")]
    public static void MassRenameIdleSprites()
    {
        try
        {
            var spriteCollection = AssetDatabase.LoadAssetAtPath("Assets/Sprites/MainSpriteCollection.prefab", typeof(tk2dSpriteCollection)) as tk2dSpriteCollection;
            var spriteCollectionData = spriteCollection.spriteCollection;

            for (int i = 0; i < spriteCollectionData.Count; i++)
            {               
                spriteCollectionData.spriteDefinitions[i].name = spriteCollectionData.spriteDefinitions[i].name + "AB";
            }
        }
        catch (Exception e)
        {
            Debug.LogException(e);
        }
    }
Title: Re: Script to rename sprites in collection
Post by: unikronsoftware on December 19, 2014, 10:24:09 pm
You should rename the sprites in the SpriteCollection, save it (EditorUtility.SetDirty), then call tk2dSpriteCollectionBuilder.Rebuild(collection). You can't just change the data objet, otherwise the names will revert every time you rebuild the collection.