# How to programmatically change sprite sheet sub-sprite name

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.

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.

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?

• I take it this can't be done by the lack of replies lol? – Savlon Jul 3 '15 at 5:09
• 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. – Honeybunch Jul 7 '15 at 22:21
• @Honeybunch you may be onto something there. I'm wondering if I can modify the metadata then... – Savlon Jul 7 '15 at 23:41
• 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. – Honeybunch Jul 7 '15 at 23:46
• 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? – Jeremiah Leslie Jul 8 '15 at 1:17

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;

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

index++;
}

EditorUtility.SetDirty (textureImporter);
textureImporter.SaveAndReimport ();

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

void Update ()
{

}
}


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.

• OP Isn't asking to rename game object but rather how to rename sub-sprites. – Honeybunch Jul 7 '15 at 23:06
• Yeh @Honeybunch is correct. I'm looking to rename the sub-sprite names. – Savlon Jul 7 '15 at 23:39
• 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. – driima Jul 8 '15 at 0:20
• @Rouze change your submitted answer to the above comment – Savlon Jul 13 '15 at 22:29