I’m working on a pixel art game that uses a lot of sliced sprite sheets, and I’m trying to tool up in order to minimize how often animation clips and other references to sprites break when using the autoslicing feature.
I’m starting simple, trying to do a tool to rename a sprite sheet and its slices without breaking all references to the sprites. But even this seems difficult. I have the slices renaming, but SpriteRenderers and animations that reference the old slices break (Missing Sprite).
I’m currently marking my importer settings dirty, modifying the SpriteMetaData, and reimporting using TextureImporter.SaveAndReimport (). I’ve also tried using AssetDatabase.ImportAsset (path, ImportAssetOptions.ForcedUpdate), and every combination thereof but any method that saves the file seems to break the references.
I’ve noticed that the fileIDToRecycleName in the meta file is different when I manually rename the sprites versus through script. If I use the script I get additional fileIDs, but if I rename it manually it changes the names associated with each fileID.
Can anyone explain how I can modify the SpriteMetaData without breaking references? I’m also curious what the difference is between the two reimporting methods above. Many people do both SaveAndReimport and ForcedUpdate, but that seems unnecessary, based on the documentation.
After your suggestion I looked into using RenameAsset to rename the Sprites, but it seems like it won’t work for sprites since they are all at the same path (which is the path of the Texture they are “parented” to). If I do RenameAsset on their path, it will just rename the root Texture, which I don’t want (yet).
I’ve been digging some more and think the fileIDToRecycleName is basically like a GUID for the sprite, and the issue is related to the extra IDs instead of renaming the existing names. The extra IDs are probably added when I assign the new spritesheet to the importer (line 36). It probably compares the new sprite names against the RecycleNames and if it doesn’t find it it creates a new ID. I’m thinking it’s basically a Unity bug since there’s no way to force it to also rename the RecycleName that I can find.
A workaround (that might have issues - I have no idea) is to edit the .meta file manually (assuming you have it forced to text). You can replace each sprite name with whatever you want, and it seems to keep references in the editor in tact. Just be sure to edit both the fileIDToRecycleName (if there is any) and the SpriteMetaData.
There is internal class PatchImportSettingRecycleID, PatchMultiple(SerializedObject serializedObject, int classID, string[ ] oldNames, string[ ] newNames) method.
Based on that method I wrote my methods for replacing old names to new names.