MultiAssetRenamer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. using UnityEditor;
  7. using System.Linq;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. // =================================
  11. // Define namespace.
  12. // =================================
  13. namespace MirzaBeig
  14. {
  15. namespace MultiAssetRenamer
  16. {
  17. // =================================
  18. // Classes.
  19. // =================================
  20. public class MultiAssetRenamer : EditorWindow
  21. {
  22. // =================================
  23. // Nested classes and structures.
  24. // =================================
  25. // ...
  26. // =================================
  27. // Variables.
  28. // =================================
  29. // The paths of the selected assets.
  30. List<string> selectedAssetPaths = new List<string>();
  31. // Scrolling view position for selected assets.
  32. Vector2 scrollPosition;
  33. // Find and replace.
  34. string findString = "find";
  35. string replaceString = "replace";
  36. string addToStartString = "addToStart";
  37. string addToEndString = "addToEnd";
  38. int removeFromStartCount = 0;
  39. int removeFromEndCount = 0;
  40. // For labeling and tooltips.
  41. GUIContent guiContentLabel;
  42. // =================================
  43. // Functions.
  44. // =================================
  45. // ...
  46. [MenuItem("Window/Mirza Beig/Multi-Asset Renamer")]
  47. static void showEditor()
  48. {
  49. EditorWindow.GetWindow<MultiAssetRenamer>(false, "Mirza Beig - Multi-Asset Renamer");
  50. }
  51. // ...
  52. void updateSelectedAssetPaths()
  53. {
  54. // Clear for updated selection.
  55. selectedAssetPaths.Clear();
  56. // Populate asset paths selection.
  57. for (int i = 0; i < Selection.assetGUIDs.Length; i++)
  58. {
  59. selectedAssetPaths.Add(AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[i]));
  60. }
  61. Repaint();
  62. }
  63. // ...
  64. void OnSelectionChange()
  65. {
  66. updateSelectedAssetPaths();
  67. }
  68. // ...
  69. string getAssetNameFromPath(string path)
  70. {
  71. // Get name from after folder path.
  72. int lastIndexOfFolderSlash = path.LastIndexOf('/') + 1;
  73. string assetName = path.Substring(
  74. path.LastIndexOf('/') + 1, path.Length - lastIndexOfFolderSlash);
  75. // Get name from before asset type (*.prefab, *.png, etc...).
  76. // Folders also count as assets in Unity. Check if path has an extension.
  77. if (assetName.Contains('.'))
  78. {
  79. assetName = assetName.Substring(0, assetName.LastIndexOf('.'));
  80. }
  81. return assetName;
  82. }
  83. // ...
  84. void OnGUI()
  85. {
  86. // Find and replace settings.
  87. EditorGUILayout.Separator();
  88. EditorGUILayout.LabelField("- Find & Replace Settings:", EditorStyles.boldLabel);
  89. EditorGUILayout.Separator();
  90. guiContentLabel = new GUIContent("Find String", "Find string in selected assets.");
  91. findString = EditorGUILayout.TextField(guiContentLabel, findString);
  92. guiContentLabel = new GUIContent("Replace String", "Replace string in selected assets.");
  93. replaceString = EditorGUILayout.TextField(guiContentLabel, replaceString);
  94. EditorGUILayout.Separator();
  95. // Button to replace string in selected assets.
  96. guiContentLabel = new GUIContent("Find and Replace",
  97. "Find and replace string in selected assets.");
  98. if (GUILayout.Button(guiContentLabel))
  99. {
  100. for (int i = 0; i < selectedAssetPaths.Count; i++)
  101. {
  102. string assetName =
  103. getAssetNameFromPath(selectedAssetPaths[i]);
  104. if (assetName.Contains(findString))
  105. {
  106. AssetDatabase.RenameAsset(selectedAssetPaths[i], assetName.Replace(findString, replaceString));
  107. }
  108. }
  109. updateSelectedAssetPaths();
  110. }
  111. // Add to start settings.
  112. EditorGUILayout.Separator();
  113. EditorGUILayout.LabelField("- Add to Start Settings:", EditorStyles.boldLabel);
  114. EditorGUILayout.Separator();
  115. guiContentLabel = new GUIContent("Add to Start String", "Add to start.");
  116. addToStartString = EditorGUILayout.TextField(guiContentLabel, addToStartString);
  117. EditorGUILayout.Separator();
  118. // Button to add string in selected assets.
  119. guiContentLabel = new GUIContent("Add to Start",
  120. "Add string to start in selected assets.");
  121. if (GUILayout.Button(guiContentLabel))
  122. {
  123. for (int i = 0; i < selectedAssetPaths.Count; i++)
  124. {
  125. string assetName =
  126. getAssetNameFromPath(selectedAssetPaths[i]);
  127. AssetDatabase.RenameAsset(selectedAssetPaths[i], assetName.Insert(0, addToStartString));
  128. }
  129. updateSelectedAssetPaths();
  130. }
  131. // Add to end settings.
  132. EditorGUILayout.Separator();
  133. EditorGUILayout.LabelField("- Add to End Settings:", EditorStyles.boldLabel);
  134. EditorGUILayout.Separator();
  135. guiContentLabel = new GUIContent("Add to End String", "Add to end.");
  136. addToEndString = EditorGUILayout.TextField(guiContentLabel, addToEndString);
  137. EditorGUILayout.Separator();
  138. // Button to add string in selected assets.
  139. guiContentLabel = new GUIContent("Add to End",
  140. "Add string to end in selected assets.");
  141. if (GUILayout.Button(guiContentLabel))
  142. {
  143. for (int i = 0; i < selectedAssetPaths.Count; i++)
  144. {
  145. string assetName =
  146. getAssetNameFromPath(selectedAssetPaths[i]);
  147. AssetDatabase.RenameAsset(selectedAssetPaths[i], assetName + addToEndString);
  148. }
  149. updateSelectedAssetPaths();
  150. }
  151. // Remove from Start settings.
  152. EditorGUILayout.Separator();
  153. EditorGUILayout.LabelField("- Remove from Start Settings:", EditorStyles.boldLabel);
  154. EditorGUILayout.Separator();
  155. guiContentLabel = new GUIContent("Remove From Start Count", "Remove from Start.");
  156. removeFromStartCount = EditorGUILayout.IntField(guiContentLabel, removeFromStartCount);
  157. EditorGUILayout.Separator();
  158. // Button to remove string at end from selected assets.
  159. guiContentLabel = new GUIContent("Remove From Start",
  160. "Remove these many characters from the start.");
  161. if (GUILayout.Button(guiContentLabel))
  162. {
  163. for (int i = 0; i < selectedAssetPaths.Count; i++)
  164. {
  165. string assetName =
  166. getAssetNameFromPath(selectedAssetPaths[i]);
  167. AssetDatabase.RenameAsset(selectedAssetPaths[i], assetName.Substring(removeFromStartCount, assetName.Length - removeFromStartCount));
  168. }
  169. updateSelectedAssetPaths();
  170. }
  171. EditorGUILayout.Separator();
  172. // Remove from end settings (truncate).
  173. EditorGUILayout.Separator();
  174. EditorGUILayout.LabelField("- Remove from End Settings:", EditorStyles.boldLabel);
  175. EditorGUILayout.Separator();
  176. guiContentLabel = new GUIContent("Remove From End Count", "Remove from end.");
  177. removeFromEndCount = EditorGUILayout.IntField(guiContentLabel, removeFromEndCount);
  178. EditorGUILayout.Separator();
  179. // Button to remove string at end from selected assets.
  180. guiContentLabel = new GUIContent("Remove From End",
  181. "Remove these many characters from the end.");
  182. if (GUILayout.Button(guiContentLabel))
  183. {
  184. for (int i = 0; i < selectedAssetPaths.Count; i++)
  185. {
  186. string assetName =
  187. getAssetNameFromPath(selectedAssetPaths[i]);
  188. AssetDatabase.RenameAsset(selectedAssetPaths[i], assetName.Substring(0, assetName.Length - removeFromEndCount));
  189. }
  190. updateSelectedAssetPaths();
  191. }
  192. EditorGUILayout.Separator();
  193. // Selected objects.
  194. guiContentLabel = new GUIContent("- Selected:",
  195. "Valid folder and typed assets selected in the PROJECT view.");
  196. EditorGUILayout.LabelField(guiContentLabel, EditorStyles.boldLabel);
  197. EditorGUILayout.Separator();
  198. scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
  199. {
  200. if (selectedAssetPaths.Count == 0)
  201. {
  202. EditorGUILayout.LabelField("Please select some asset(s) in the project view.", EditorStyles.miniBoldLabel);
  203. }
  204. else
  205. {
  206. for (int i = 0; i < selectedAssetPaths.Count; i++)
  207. {
  208. EditorGUILayout.BeginHorizontal();
  209. {
  210. //EditorGUILayout.LabelField("> " + selectedAssetPaths[i], EditorStyles.miniLabel);
  211. EditorGUILayout.LabelField("> " + getAssetNameFromPath(selectedAssetPaths[i]), EditorStyles.miniLabel);
  212. }
  213. EditorGUILayout.EndHorizontal();
  214. }
  215. }
  216. }
  217. EditorGUILayout.EndScrollView();
  218. }
  219. // ...
  220. void OnFocus()
  221. {
  222. updateSelectedAssetPaths();
  223. }
  224. // =================================
  225. // End functions.
  226. // =================================
  227. }
  228. // =================================
  229. // End namespace.
  230. // =================================
  231. }
  232. }
  233. // =================================
  234. // --END-- //
  235. // =================================