2
\$\begingroup\$

I am trying to change the name of the sub-sprites of a sprite sheet. I have searched and tried everything I can think of and cannot get it to work, which makes me think it can't be done at this point in time.

Let's say I have a sprite sheet that has already been split up into individual sprites inside of Unity.

Original Sprite Name

I am attempting to change the sub-sprite names programmatically.

I tried using

AssetDatabase.RenameAsset (...)

on the sprite sheet asset, which only changes the sprite sheet name and not sub-sprites.

I then thought to obtain a sub-sprite and attempt to change its name using the below code.

            if (AssetDatabase.IsSubAsset (subSprite))
            {
                AssetDatabase.RenameAsset (AssetDatabase.GetAssetPath (subSprite), "newSprite" + i.ToString ());
            }

However, this too only changes the sprite sheet name.

Altered Sprite Name

I'm not sure where else I can take this to achieve my desired outcome, apart from programmatically copying the original sprite sheet and using that to create a copy. But even then, I still am unable to alter the sub-sprite names.

I also tried changing subSprite.name, but this just changes the internal name and not the asset name.

Any ideas?

\$\endgroup\$
5
  • \$\begingroup\$ I take it this can't be done by the lack of replies lol? \$\endgroup\$
    – Savlon
    Jul 3, 2015 at 5:09
  • \$\begingroup\$ Did you verify with a debugger or log that the AssetDatabase.GetAssetPath call gives you the path you would expect? If not then my guess would be that your subsprites aren't really files on disk but rather just metadata about the section of the parent sprite. \$\endgroup\$
    – Honeybunch
    Jul 7, 2015 at 22:21
  • \$\begingroup\$ @Honeybunch you may be onto something there. I'm wondering if I can modify the metadata then... \$\endgroup\$
    – Savlon
    Jul 7, 2015 at 23:41
  • 1
    \$\begingroup\$ Try checking out Unity's .meta files. Last I checked they didn't have anything super important in them but it would be worth a shot. Maybe there are some other files that Unity creates that deal with the sprite sheet what you could edit. I don't think that they actually splice the image into different images though; pretty sure it's all just coordinate data somewhere. That somewhere is probably also where the names are. \$\endgroup\$
    – Honeybunch
    Jul 7, 2015 at 23:46
  • \$\begingroup\$ Have you tried using subSprite.name to change the name internally then saving the object as an asset with the ReplaceAsset function, (function name might be incorrect), to overwrite the asset with the new one? \$\endgroup\$ Jul 8, 2015 at 1:17

3 Answers 3

4
\$\begingroup\$

Ok so based on @Rouze comment regarding the sprite slice renamer, I've managed to mock something up quickly that does the job.

Here is the quick mockup I did if anyone else is looking for a quick and dirty way to change the subsprite names. In my research for this subject, it appears I was referring to them as Sub-Sprites when the more known name is Sprite Slices... Probably why I couldn't find anything on this subject.

using UnityEngine;
using UnityEditor;
using System.Collections;

public class SubSpriteRenamer : MonoBehaviour 
{
    public Texture2D texture2D;
    public string newName;

    private string path;
    private TextureImporter textureImporter;

    void Start () 
    {
        path = AssetDatabase.GetAssetPath (texture2D);
        textureImporter = AssetImporter.GetAtPath (path) as TextureImporter;
        SpriteMetaData[] sliceMetaData = textureImporter.spritesheet;

        int index = 0;
        foreach (SpriteMetaData individualSliceData in sliceMetaData)
        {
            sliceMetaData[index].name = string.Format (newName + "_{0}", index);
            print (sliceMetaData[index].name);

            index++;
        }

        textureImporter.spritesheet = sliceMetaData;
        EditorUtility.SetDirty (textureImporter);
        textureImporter.SaveAndReimport ();

        AssetDatabase.ImportAsset (path, ImportAssetOptions.ForceUpdate);
    }

    void Update () 
    {

    }
}
\$\endgroup\$
1
\$\begingroup\$

I had the same problem - a quick fix is to open the meta file in a good editor and do a find / replace on the name fragment. In my case I needed AbilityIcon_0X changed to UpgradeNodeIcon_0X. So I did these steps:

  • Find "AbilityIcon_"
  • Replace all with "UpgradeNodeIcon_".

A scripted solution if there is a batch is always great. If you just need to do a handful this might be easiest

\$\endgroup\$
0
+50
\$\begingroup\$

I found this sprite slice renamer online. It's the closest thing I could find but the source is provided and might be of some use.

\$\endgroup\$
4
  • \$\begingroup\$ OP Isn't asking to rename game object but rather how to rename sub-sprites. \$\endgroup\$
    – Honeybunch
    Jul 7, 2015 at 23:06
  • \$\begingroup\$ Yeh @Honeybunch is correct. I'm looking to rename the sub-sprite names. \$\endgroup\$
    – Savlon
    Jul 7, 2015 at 23:39
  • 2
    \$\begingroup\$ Ah yes, I see now. My bad. I found this sprite slice renamer online. It's the closest thing I could find but the source is provided and might be of some use. \$\endgroup\$
    – driima
    Jul 8, 2015 at 0:20
  • \$\begingroup\$ @Rouze change your submitted answer to the above comment \$\endgroup\$
    – Savlon
    Jul 13, 2015 at 22:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .