Skip to content

Instantly share code, notes, and snippets.

@edwardrowe
Last active March 22, 2023 01:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save edwardrowe/1b18a5c8fd180733a68f to your computer and use it in GitHub Desktop.
Save edwardrowe/1b18a5c8fd180733a68f to your computer and use it in GitHub Desktop.
Unity - RenameSpritesheet
/*The MIT License (MIT)
Copyright (c) 2016 Edward Rowe (@edwardlrowe)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using UnityEngine;
using UnityEditor;
namespace RedBlue
{
public class RenameSpritesheet : EditorWindow
{
string oldSpritePrefix;
string newSpritePrefix;
static Object selectedObject {
get {
var selectedObject = Selection.activeObject;
if (!AssetDatabase.Contains (selectedObject)) {
Debug.LogError ("Selected object not found in Asset Database.");
return null;
}
return selectedObject;
}
}
[MenuItem ("Assets/Rename Spritesheet")]
public static void ShowRenameSpritesheetWindow ()
{
EditorWindow.GetWindow<RenameSpritesheet> (true, "Rename Texture", true);
}
[MenuItem ("Assets/Rename Spritesheet", true)]
public static bool IsSelectionTexture ()
{
if (Selection.activeObject == null) {
return false;
}
if (Selection.objects.Length > 1) {
return false;
}
return Selection.activeObject.GetType () == typeof(Texture2D);
}
void OnEnable ()
{
oldSpritePrefix = System.IO.Path.GetFileNameWithoutExtension (selectedObject.name);
newSpritePrefix = oldSpritePrefix;
}
void OnGUI ()
{
EditorGUILayout.HelpBox ("This SpritesheetRename tool is used to rename a texture that's been autosliced. It replaces " +
"all instances of the old prefix with a new one, and renames the Texture to match.", MessageType.None);
oldSpritePrefix = EditorGUILayout.TextField ("Prefix to Replace", oldSpritePrefix);
newSpritePrefix = EditorGUILayout.TextField ("New Sprite Prefix", newSpritePrefix);
if (GUILayout.Button ("Rename")) {
RenameSelectedTexture (oldSpritePrefix, newSpritePrefix);
Close ();
}
}
static void RenameSelectedTexture (string oldName, string newName)
{
string path = AssetDatabase.GetAssetPath (selectedObject);
string metaFile = System.IO.File.ReadAllText (path + ".meta");
string ammendedMetaFile = ReplaceSpritePrefixInMetafile (metaFile, oldName, newName);
// If users have hidden meta files they will get an access exception.
// Need to unhide them briefly.
var pathToTextureMetaFile = path + ".meta";
var originalFileAttributes = System.IO.File.GetAttributes(pathToTextureMetaFile);
System.IO.File.SetAttributes(pathToTextureMetaFile, originalFileAttributes & ~System.IO.FileAttributes.Hidden);
System.IO.File.WriteAllText (pathToTextureMetaFile, ammendedMetaFile);
System.IO.File.SetAttributes(pathToTextureMetaFile, originalFileAttributes);
AssetDatabase.RenameAsset (path, newName);
AssetDatabase.Refresh ();
}
static string ReplaceSpritePrefixInMetafile (string metafileText, string prefixToReplace, string newPrefix)
{
string modifiedMetafile = ReplaceFileIDRecycleNames (metafileText, prefixToReplace, newPrefix);
modifiedMetafile = ReplaceSpriteMetaData (modifiedMetafile, prefixToReplace, newPrefix);
return modifiedMetafile;
}
static string ReplaceFileIDRecycleNames (string metafileText, string oldPrefix, string newPrefix)
{
string fileIDPattern = "([\\d]{8}: )" + oldPrefix + "(\r?\n)";
var fileIDRegex = new System.Text.RegularExpressions.Regex (fileIDPattern);
string replacementText = "$1" + newPrefix + "$2";
return fileIDRegex.Replace (metafileText, replacementText);
}
static string ReplaceSpriteMetaData (string metafileText, string oldPrefix, string newPrefix)
{
string spritenamePattern = "(- name: )" + oldPrefix + "(\r?\n)";
var spritenameRegex = new System.Text.RegularExpressions.Regex (spritenamePattern);
string replacementText = "$1" + newPrefix + "$2";
return spritenameRegex.Replace (metafileText, replacementText);
}
}
}
@wkvs
Copy link

wkvs commented May 16, 2017

https://gist.github.com/edwardrowe/1b18a5c8fd180733a68f#file-renamespritesheet-cs-L96-L98

string modifiedMetafile = ReplaceFileIDRecycleNames (metafileText, prefixToReplace, newPrefix);
modifiedMetafile = ReplaceSpriteMetaData (metafileText, prefixToReplace, newPrefix);
return modifiedMetafile;

I think some mistake is hidden there. Second line must have modifiedMetafile as first argument, isn't it?

@snarlynarwhal
Copy link

This does not work for me - it just renames the main texture asset, not the sliced sprites.

@edwardrowe
Copy link
Author

edwardrowe commented Feb 6, 2018

@wkvs you are totally right. I've been using a modified version of this script in my renaming tool (https://github.com/redbluegames/unity-mulligan-renamer), and I fixed it there after you pointed this out.

@snarlynarwhal I may have fixed the issues you were seeing as well, which I suspect could have been due to using Hidden meta files and not being able to Write them.

Thanks for your comments!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment