Hello Guest

Author Topic: Script to rename sprites in collection  (Read 3658 times)

radu

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 67
    • View Profile
Script to rename sprites in collection
« 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);
        }
    }

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Script to rename sprites in collection
« Reply #1 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.