-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenamerWindow.cs
552 lines (506 loc) · 23.9 KB
/
RenamerWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
using UnityEditor;
using UnityEngine;
using System.IO;
namespace AhabTools
{
public class RenamerWindow : EditorWindow
{
#region Variables
private enum IterationType
{
SELECTED_OBJECTS,
SELECTED_FOLDER,
FOLDER_RECURSIVE
}
private IterationType iterationType;
private string originalNamePart = "";
private string newNamePart = "";
private string prefix = "";
private string suffix = "";
public bool renamerFoldout;
public bool apendixFoldout;
private GUIStyle foldoutHeaderStyle;
private GUIStyle foldoutContentStyle;
#endregion
[MenuItem("Tools/Ahab Tools/A Simple Renamer")]
public static void ShowWindow()
{
GetWindow<RenamerWindow>("A Simple Renamer v1.0.1");
}
void OnGUI()
{
#region Hyperlink
GUIStyle hyperlinkStyle = new GUIStyle();
hyperlinkStyle.normal.textColor = Color.gray;
hyperlinkStyle.fontStyle = FontStyle.Italic;
Rect linkRect = EditorGUILayout.GetControlRect();
if (GUI.Button(linkRect, "Click here to visit the GitHub repository of this tool to check the latest version.", hyperlinkStyle))
{
Application.OpenURL("https://github.com/ahabdeveloper/Unity3D-Simple-Renamer");
}
#endregion
GUILayout.Space(5);
#region How to use infobox
EditorGUILayout.HelpBox("" +
"How To use:\n" +
"- Choose a method depending on if you want to rename only the current " +
"selected objects in the Project Tab,\n" +
"all the files meeting the conditions inside the current selected Folder also in " +
"the Project Tab, \n" +
"or all files and folders (including all levels) meeting the conditions under the current selected folder in the Project Tab.\n" +
"- Run either the renaming method or the sufix/prefix method.", MessageType.Info);
#endregion
#region Operation Type
// Toggle buttons for iteration type
GUILayout.BeginHorizontal();
if (GUILayout.Toggle(iterationType == IterationType.SELECTED_OBJECTS, "Selected Objects", "Button"))
{
iterationType = IterationType.SELECTED_OBJECTS;
}
if (GUILayout.Toggle(iterationType == IterationType.SELECTED_FOLDER, "Selected Folder", "Button"))
{
iterationType = IterationType.SELECTED_FOLDER;
}
if (GUILayout.Toggle(iterationType == IterationType.FOLDER_RECURSIVE, "Folder Recursive", "Button"))
{
iterationType = IterationType.FOLDER_RECURSIVE;
}
GUILayout.EndHorizontal();
#endregion
GUILayout.Space(10);
#region Foldout Styles
if (foldoutHeaderStyle == null)
{
foldoutHeaderStyle = new GUIStyle("ShurikenModuleTitle")
{
border = new RectOffset(2, 2, 2, 2),
fixedHeight = 22,
contentOffset = new Vector2(20f, -2f)
};
foldoutHeaderStyle.contentOffset = new Vector2(20f, -2f);
foldoutHeaderStyle.fixedWidth = 0;
}
if (foldoutContentStyle == null)
{
foldoutContentStyle = new GUIStyle(GUI.skin.box)
{
padding = new RectOffset(1, 1, 1, 1),
margin = new RectOffset(1, 1, 1, 1)
};
}
#endregion
#region Foldout drawing 1
Rect backgroundRect9 = GUILayoutUtility.GetRect(1f, 22f, GUILayout.ExpandWidth(true));
GUI.Box(backgroundRect9, GUIContent.none, foldoutHeaderStyle);
renamerFoldout = EditorGUI.Foldout(backgroundRect9, renamerFoldout, "Renaming Options", true, foldoutHeaderStyle);
#endregion
if (renamerFoldout)
{
EditorGUI.indentLevel++;
EditorGUI.DrawRect(EditorGUILayout.BeginVertical(EditorStyles.helpBox), new Color(0.18f, 0.18f, 0.18f, 1f));
GUILayout.BeginHorizontal();
GUILayout.Label("Original Name Part:", GUILayout.Width(150));
originalNamePart = GUILayout.TextField(originalNamePart);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("New Name Part:", GUILayout.Width(150));
newNamePart = GUILayout.TextField(newNamePart);
GUILayout.EndHorizontal();
GUIContent buttonContent1 = new GUIContent("Rename Files", "Changes cannot be undone with Control+Z. Careful.");
if (GUILayout.Button(buttonContent1, GUILayout.Height(40)))
{
switch (iterationType)
{
case IterationType.SELECTED_OBJECTS:
RenameSelectedObjects();
break;
case IterationType.SELECTED_FOLDER:
RenameFilesInSelectedFolder();
break;
case IterationType.FOLDER_RECURSIVE:
RenameFolderRecursive();
break;
default:
break;
}
}
EditorGUILayout.EndVertical();
EditorGUI.indentLevel--;
}
#region Foldout drawing 2
Rect backgroundRect1 = GUILayoutUtility.GetRect(1f, 22f, GUILayout.ExpandWidth(true));
GUI.Box(backgroundRect1, GUIContent.none, foldoutHeaderStyle);
apendixFoldout = EditorGUI.Foldout(backgroundRect1, apendixFoldout, "Suffix/Prefix Options", true, foldoutHeaderStyle);
#endregion
if (apendixFoldout)
{
EditorGUI.indentLevel++;
EditorGUI.DrawRect(EditorGUILayout.BeginVertical(EditorStyles.helpBox), new Color(0.18f, 0.18f, 0.18f, 1f));
GUILayout.BeginHorizontal();
GUILayout.Label("Prefix:", GUILayout.Width(150));
prefix = GUILayout.TextField(prefix);
GUILayout.EndHorizontal();
GUIContent buttonContent2 = new GUIContent("Add Prefix", "Changes cannot be undone with Control+Z. Careful.");
if (GUILayout.Button(buttonContent2, GUILayout.Height(40)))
{
switch (iterationType)
{
case IterationType.SELECTED_OBJECTS:
AddPrefixToSelectedObjects();
break;
case IterationType.SELECTED_FOLDER:
AddPrefixToSelectedFolder();
break;
case IterationType.FOLDER_RECURSIVE:
AddPrefixRecursive();
break;
default:
break;
}
}
GUILayout.BeginHorizontal();
GUILayout.Label("Suffix:", GUILayout.Width(150));
suffix = GUILayout.TextField(suffix);
GUILayout.EndHorizontal();
GUIContent buttonContent3 = new GUIContent("Add Sufix", "Changes cannot be undone with Control+Z. Careful.");
if (GUILayout.Button(buttonContent3, GUILayout.Height(40)))
{
switch (iterationType)
{
case IterationType.SELECTED_OBJECTS:
AddSuffixToSelectedObjects();
break;
case IterationType.SELECTED_FOLDER:
AddSuffixToSelectedFolder();
break;
case IterationType.FOLDER_RECURSIVE:
AddSuffixRecursive();
break;
default:
break;
}
}
EditorGUILayout.EndVertical();
EditorGUI.indentLevel--;
}
}
#region Auxiliar methods
private void AddSuffixRecursive()
{
string path = GetSelectedFolderPath();
if (path == null)
{
Debug.LogWarning("No folder is currently selected or the selected item is not a folder.");
return;
}
if (string.IsNullOrEmpty(suffix))
{
Debug.LogWarning("Suffix is empty. Please enter a suffix to add.");
return;
}
AddSuffixToDirectoryContents(path);
AssetDatabase.Refresh(); // Refresh the Asset Database to show the new file names and directories
}
private void AddSuffixToDirectoryContents(string directoryPath)
{
AssetDatabase.StartAssetEditing(); // Begin grouping asset database changes
// Add suffix to files in the directory
string[] fileEntries = Directory.GetFiles(directoryPath);
foreach (string filePath in fileEntries)
{
if (!filePath.EndsWith(".meta"))
{
string fileName = Path.GetFileName(filePath);
string extension = Path.GetExtension(filePath);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
string newName = fileNameWithoutExtension + suffix + extension;
AssetDatabase.RenameAsset(filePath, newName);
Debug.Log($"Added suffix to {fileName}, new name {newName}");
}
}
// Recursively add suffix to subdirectories and their contents
string[] subdirectoryEntries = Directory.GetDirectories(directoryPath);
foreach (string subdirectory in subdirectoryEntries)
{
AddSuffixToDirectoryContents(subdirectory); // First add suffix to files and subdirectories inside this one
// Now check if this subdirectory itself should have the suffix added
string dirName = Path.GetFileName(subdirectory);
string newDirName = dirName + suffix;
string newDirPath = Path.Combine(Path.GetDirectoryName(subdirectory), newDirName);
AssetDatabase.MoveAsset(subdirectory, newDirPath);
Debug.Log($"Added suffix to directory {dirName}, new directory name {newDirName}");
}
AssetDatabase.StopAssetEditing(); // End grouping asset database changes
}
private void AddPrefixRecursive()
{
string path = GetSelectedFolderPath();
if (path == null)
{
Debug.LogWarning("No folder is currently selected or the selected item is not a folder.");
return;
}
if (string.IsNullOrEmpty(prefix))
{
Debug.LogWarning("Prefix is empty. Please enter a prefix to add.");
return;
}
AddPrefixToDirectoryContents(path);
AssetDatabase.Refresh(); // Refresh the Asset Database to show the new file names and directories
}
private void AddPrefixToDirectoryContents(string directoryPath)
{
AssetDatabase.StartAssetEditing(); // Begin grouping asset database changes
// Add prefix to files in the directory
string[] fileEntries = Directory.GetFiles(directoryPath);
foreach (string filePath in fileEntries)
{
if (!filePath.EndsWith(".meta"))
{
string fileName = Path.GetFileName(filePath);
string newName = prefix + fileName;
AssetDatabase.RenameAsset(filePath, newName);
Debug.Log($"Added prefix to {fileName}, new name {newName}");
}
}
// Recursively add prefix to subdirectories and their contents
string[] subdirectoryEntries = Directory.GetDirectories(directoryPath);
foreach (string subdirectory in subdirectoryEntries)
{
AddPrefixToDirectoryContents(subdirectory); // First add prefix to files and subdirectories inside this one
// Now check if this subdirectory itself should have the prefix added
string dirName = Path.GetFileName(subdirectory);
string newDirName = prefix + dirName;
string newDirPath = Path.Combine(Path.GetDirectoryName(subdirectory), newDirName);
AssetDatabase.MoveAsset(subdirectory, newDirPath);
Debug.Log($"Added prefix to directory {dirName}, new directory name {newDirName}");
}
AssetDatabase.StopAssetEditing(); // End grouping asset database changes
}
private void AddSuffixToSelectedObjects()
{
if (Selection.objects == null || Selection.objects.Length == 0)
{
Debug.LogWarning("No objects are currently selected.");
return;
}
AssetDatabase.StartAssetEditing();
foreach (var selectedObject in Selection.objects)
{
string path = AssetDatabase.GetAssetPath(selectedObject);
if (!string.IsNullOrEmpty(path) && !path.EndsWith(".meta"))
{
string extension = Path.GetExtension(path);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
string newName = fileNameWithoutExtension + suffix + extension;
string newPath = Path.Combine(Path.GetDirectoryName(path), newName);
AssetDatabase.RenameAsset(path, newName);
Debug.Log($"Added suffix to {fileNameWithoutExtension}, new name {newName}");
}
}
AssetDatabase.StopAssetEditing();
AssetDatabase.Refresh();
}
private void AddSuffixToSelectedFolder()
{
string path = GetSelectedFolderPath();
if (path != null && !string.IsNullOrEmpty(suffix))
{
string[] fileEntries = Directory.GetFiles(path);
AssetDatabase.StartAssetEditing();
foreach (string filePath in fileEntries)
{
if (!filePath.EndsWith(".meta"))
{
string extension = Path.GetExtension(filePath);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
string newName = fileNameWithoutExtension + suffix + extension;
string newPath = Path.Combine(Path.GetDirectoryName(filePath), newName);
AssetDatabase.RenameAsset(filePath, newName);
Debug.Log($"Added suffix to {fileNameWithoutExtension}, new name {newName}");
}
}
AssetDatabase.StopAssetEditing();
AssetDatabase.Refresh();
}
else if (string.IsNullOrEmpty(suffix))
{
Debug.LogWarning("Suffix is empty. Please enter a suffix to add.");
}
}
private void AddPrefixToSelectedObjects()
{
if (Selection.objects == null || Selection.objects.Length == 0)
{
Debug.LogWarning("No objects are currently selected.");
return;
}
AssetDatabase.StartAssetEditing();
foreach (var selectedObject in Selection.objects)
{
string path = AssetDatabase.GetAssetPath(selectedObject);
if (!string.IsNullOrEmpty(path) && !path.EndsWith(".meta"))
{
string fileName = Path.GetFileName(path);
string newName = prefix + fileName;
string newPath = Path.Combine(Path.GetDirectoryName(path), newName);
AssetDatabase.RenameAsset(path, newName);
Debug.Log($"Added prefix to {fileName}, new name {newName}");
}
}
AssetDatabase.StopAssetEditing();
AssetDatabase.Refresh();
}
private void AddPrefixToSelectedFolder()
{
string path = GetSelectedFolderPath();
if (path != null && !string.IsNullOrEmpty(prefix))
{
string[] fileEntries = Directory.GetFiles(path);
AssetDatabase.StartAssetEditing();
foreach (string filePath in fileEntries)
{
if (!filePath.EndsWith(".meta"))
{
string fileName = Path.GetFileName(filePath);
string newName = prefix + fileName;
string newPath = Path.Combine(Path.GetDirectoryName(filePath), newName);
AssetDatabase.RenameAsset(filePath, newName);
Debug.Log($"Added prefix to {fileName}, new name {newName}");
}
}
AssetDatabase.StopAssetEditing();
AssetDatabase.Refresh();
}
else if (string.IsNullOrEmpty(prefix))
{
Debug.LogWarning("Prefix is empty. Please enter a prefix to add.");
}
}
private void RenameSelectedObjects()
{
if (Selection.objects == null || Selection.objects.Length == 0)
{
Debug.LogWarning("No objects are currently selected.");
return;
}
AssetDatabase.StartAssetEditing();
foreach (var selectedObject in Selection.objects)
{
string path = AssetDatabase.GetAssetPath(selectedObject);
if (!string.IsNullOrEmpty(path) && !path.EndsWith(".meta"))
{
string fileName = Path.GetFileName(path);
if (fileName.Contains(originalNamePart))
{
string newName = fileName.Replace(originalNamePart, newNamePart);
string newPath = Path.Combine(Path.GetDirectoryName(path), newName);
AssetDatabase.RenameAsset(path, newName);
Debug.Log($"Renamed {fileName} to {newName}");
}
}
}
AssetDatabase.StopAssetEditing();
AssetDatabase.Refresh();
}
private void RenameFolderRecursive()
{
string path = GetSelectedFolderPath();
if (path == null)
{
Debug.LogWarning("No folder is currently selected or the selected item is not a folder.");
return;
}
if (string.IsNullOrEmpty(originalNamePart))
{
Debug.LogWarning("Original name part is empty. Please enter the part of the file names you want to replace.");
return;
}
RenameDirectoryContents(path);
AssetDatabase.Refresh(); // Refresh the Asset Database to show the new file names and directories
}
private void RenameDirectoryContents(string directoryPath)
{
AssetDatabase.StartAssetEditing(); // Begin grouping asset database changes
// Rename files in the directory
string[] fileEntries = Directory.GetFiles(directoryPath);
foreach (string filePath in fileEntries)
{
if (!filePath.EndsWith(".meta"))
{
string fileName = Path.GetFileName(filePath);
if (fileName.Contains(originalNamePart))
{
string newName = fileName.Replace(originalNamePart, newNamePart);
AssetDatabase.RenameAsset(filePath, newName);
Debug.Log($"Renamed {fileName} to {newName}");
}
}
}
// Recursively rename subdirectories and their contents
string[] subdirectoryEntries = Directory.GetDirectories(directoryPath);
foreach (string subdirectory in subdirectoryEntries)
{
RenameDirectoryContents(subdirectory); // First rename files and subdirectories inside this one
// Now check if this subdirectory itself should be renamed
string dirName = Path.GetFileName(subdirectory);
if (dirName.Contains(originalNamePart))
{
string newDirName = dirName.Replace(originalNamePart, newNamePart);
string newDirPath = Path.Combine(Path.GetDirectoryName(subdirectory), newDirName);
AssetDatabase.MoveAsset(subdirectory, newDirPath);
Debug.Log($"Renamed directory {dirName} to {newDirName}");
}
}
AssetDatabase.StopAssetEditing(); // End grouping asset database changes
}
private void RenameFilesInSelectedFolder()
{
string path = GetSelectedFolderPath();
if (path != null && !string.IsNullOrEmpty(originalNamePart))
{
string[] fileEntries = Directory.GetFiles(path);
AssetDatabase.StartAssetEditing(); // Begin grouping asset database changes
foreach (string filePath in fileEntries)
{
if (!filePath.EndsWith(".meta"))
{
string fileName = Path.GetFileName(filePath);
if (fileName.Contains(originalNamePart))
{
string newName = fileName.Replace(originalNamePart, newNamePart);
string newPath = Path.Combine(Path.GetDirectoryName(filePath), newName);
AssetDatabase.RenameAsset(filePath, newName);
Debug.Log($"Renamed {fileName} to {newName}");
}
}
}
AssetDatabase.StopAssetEditing(); // End grouping asset database changes
AssetDatabase.Refresh(); // Refresh the Asset Database to show the new file names
}
else if (string.IsNullOrEmpty(originalNamePart))
{
Debug.LogWarning("Original name part is empty. Please enter the part of the file names you want to replace.");
}
}
private string GetSelectedFolderPath()
{
if (Selection.activeObject == null)
{
Debug.LogWarning("No folder or object is currently selected.");
return null;
}
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (AssetDatabase.IsValidFolder(path))
{
return path;
}
else
{
Debug.LogWarning("The selected item is not a folder.");
return null;
}
}
#endregion
}
}