Skip to content

Instantly share code, notes, and snippets.

@QuantumCalzone
Created April 19, 2018 19:22
Show Gist options
  • Save QuantumCalzone/9f6a5b2e196a0a83a26d7ff3996b200a to your computer and use it in GitHub Desktop.
Save QuantumCalzone/9f6a5b2e196a0a83a26d7ff3996b200a to your computer and use it in GitHub Desktop.
A Unity Editor Window that does some simple asset renaming
using UnityEngine;
using UnityEditor;
public class Renamer : EditorWindow {
#region Variables
private GameObject[] targets = new GameObject[0];
private bool debugThis = false;
private string prependValue = "";
private string appendValue = "";
private string replaceTarget = "";
private string replaceValue = "";
#endregion
#region Starts
[MenuItem("Window/Renamer")]
private static void Init () {
Renamer window = (Renamer)GetWindow(typeof(Renamer));
window.titleContent = new GUIContent("Renamer");
window.Show();
}
#endregion
#region Interaction
private void Rename () {
for (int a = 0; a < Selection.objects.Length; a++) {
string assetPath = AssetDatabase.GetAssetPath(Selection.objects[a]);
if (!string.IsNullOrEmpty(assetPath)) {
string assetName = Selection.objects[a].name;
if (debugThis) {
Debug.Log("assetPath: " + assetPath);
Debug.Log("assetName: " + assetName);
}
if (!string.IsNullOrEmpty(prependValue)) assetName = prependValue + assetName;
if (!string.IsNullOrEmpty(appendValue)) assetName = assetName + appendValue;
if (!string.IsNullOrEmpty(replaceTarget)) assetName = assetName.Replace(replaceTarget, replaceValue);
if (debugThis) Debug.Log("new assetName: " + assetName);
string error = AssetDatabase.RenameAsset(assetPath, assetName);
if (!string.IsNullOrEmpty(error)) Debug.LogError(error);
}
}
}
#endregion
#region Process
private void OnGUI () {
this.Repaint();
GUILayout.Label("Select your target assets in the Project window");
GUILayout.Space(10);
EditorGUILayout.BeginHorizontal();
prependValue = EditorGUILayout.TextField("Prepend With:", prependValue);
EditorGUILayout.EndHorizontal();
GUILayout.Space(10);
EditorGUILayout.BeginHorizontal();
appendValue = EditorGUILayout.TextField("Append With:", appendValue);
EditorGUILayout.EndHorizontal();
GUILayout.Space(10);
replaceTarget = EditorGUILayout.TextField("Replace:", replaceTarget);
replaceValue = EditorGUILayout.TextField("With:", replaceValue);
GUILayout.Space(10);
debugThis = EditorGUILayout.ToggleLeft("Debug Changes", debugThis);
if (GUILayout.Button("Rename")) Rename();
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment