123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
-
- // =================================
- // Namespaces.
- // =================================
- using UnityEngine;
- using UnityEditor;
- using System.Linq;
- using System.Collections;
- using System.Collections.Generic;
- // =================================
- // Define namespace.
- // =================================
- namespace MirzaBeig
- {
- namespace MultiAssetRenamer
- {
- // =================================
- // Classes.
- // =================================
- public class MultiAssetRenamer : EditorWindow
- {
- // =================================
- // Nested classes and structures.
- // =================================
- // ...
- // =================================
- // Variables.
- // =================================
- // The paths of the selected assets.
- List<string> selectedAssetPaths = new List<string>();
- // Scrolling view position for selected assets.
- Vector2 scrollPosition;
- // Find and replace.
- string findString = "find";
- string replaceString = "replace";
- string addToStartString = "addToStart";
- string addToEndString = "addToEnd";
- int removeFromStartCount = 0;
- int removeFromEndCount = 0;
- // For labeling and tooltips.
- GUIContent guiContentLabel;
- // =================================
- // Functions.
- // =================================
- // ...
- [MenuItem("Window/Mirza Beig/Multi-Asset Renamer")]
- static void showEditor()
- {
- EditorWindow.GetWindow<MultiAssetRenamer>(false, "Mirza Beig - Multi-Asset Renamer");
- }
- // ...
- void updateSelectedAssetPaths()
- {
- // Clear for updated selection.
- selectedAssetPaths.Clear();
- // Populate asset paths selection.
- for (int i = 0; i < Selection.assetGUIDs.Length; i++)
- {
- selectedAssetPaths.Add(AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[i]));
- }
- Repaint();
- }
- // ...
- void OnSelectionChange()
- {
- updateSelectedAssetPaths();
- }
- // ...
- string getAssetNameFromPath(string path)
- {
- // Get name from after folder path.
- int lastIndexOfFolderSlash = path.LastIndexOf('/') + 1;
- string assetName = path.Substring(
- path.LastIndexOf('/') + 1, path.Length - lastIndexOfFolderSlash);
- // Get name from before asset type (*.prefab, *.png, etc...).
- // Folders also count as assets in Unity. Check if path has an extension.
- if (assetName.Contains('.'))
- {
- assetName = assetName.Substring(0, assetName.LastIndexOf('.'));
- }
- return assetName;
- }
- // ...
- void OnGUI()
- {
- // Find and replace settings.
- EditorGUILayout.Separator();
- EditorGUILayout.LabelField("- Find & Replace Settings:", EditorStyles.boldLabel);
- EditorGUILayout.Separator();
- guiContentLabel = new GUIContent("Find String", "Find string in selected assets.");
- findString = EditorGUILayout.TextField(guiContentLabel, findString);
- guiContentLabel = new GUIContent("Replace String", "Replace string in selected assets.");
- replaceString = EditorGUILayout.TextField(guiContentLabel, replaceString);
- EditorGUILayout.Separator();
- // Button to replace string in selected assets.
- guiContentLabel = new GUIContent("Find and Replace",
- "Find and replace string in selected assets.");
- if (GUILayout.Button(guiContentLabel))
- {
- for (int i = 0; i < selectedAssetPaths.Count; i++)
- {
- string assetName =
- getAssetNameFromPath(selectedAssetPaths[i]);
- if (assetName.Contains(findString))
- {
- AssetDatabase.RenameAsset(selectedAssetPaths[i], assetName.Replace(findString, replaceString));
- }
- }
- updateSelectedAssetPaths();
- }
- // Add to start settings.
- EditorGUILayout.Separator();
- EditorGUILayout.LabelField("- Add to Start Settings:", EditorStyles.boldLabel);
- EditorGUILayout.Separator();
- guiContentLabel = new GUIContent("Add to Start String", "Add to start.");
- addToStartString = EditorGUILayout.TextField(guiContentLabel, addToStartString);
- EditorGUILayout.Separator();
- // Button to add string in selected assets.
- guiContentLabel = new GUIContent("Add to Start",
- "Add string to start in selected assets.");
- if (GUILayout.Button(guiContentLabel))
- {
- for (int i = 0; i < selectedAssetPaths.Count; i++)
- {
- string assetName =
- getAssetNameFromPath(selectedAssetPaths[i]);
- AssetDatabase.RenameAsset(selectedAssetPaths[i], assetName.Insert(0, addToStartString));
- }
- updateSelectedAssetPaths();
- }
- // Add to end settings.
- EditorGUILayout.Separator();
- EditorGUILayout.LabelField("- Add to End Settings:", EditorStyles.boldLabel);
- EditorGUILayout.Separator();
- guiContentLabel = new GUIContent("Add to End String", "Add to end.");
- addToEndString = EditorGUILayout.TextField(guiContentLabel, addToEndString);
- EditorGUILayout.Separator();
- // Button to add string in selected assets.
- guiContentLabel = new GUIContent("Add to End",
- "Add string to end in selected assets.");
- if (GUILayout.Button(guiContentLabel))
- {
- for (int i = 0; i < selectedAssetPaths.Count; i++)
- {
- string assetName =
- getAssetNameFromPath(selectedAssetPaths[i]);
- AssetDatabase.RenameAsset(selectedAssetPaths[i], assetName + addToEndString);
- }
- updateSelectedAssetPaths();
- }
- // Remove from Start settings.
- EditorGUILayout.Separator();
- EditorGUILayout.LabelField("- Remove from Start Settings:", EditorStyles.boldLabel);
- EditorGUILayout.Separator();
- guiContentLabel = new GUIContent("Remove From Start Count", "Remove from Start.");
- removeFromStartCount = EditorGUILayout.IntField(guiContentLabel, removeFromStartCount);
- EditorGUILayout.Separator();
- // Button to remove string at end from selected assets.
- guiContentLabel = new GUIContent("Remove From Start",
- "Remove these many characters from the start.");
- if (GUILayout.Button(guiContentLabel))
- {
- for (int i = 0; i < selectedAssetPaths.Count; i++)
- {
- string assetName =
- getAssetNameFromPath(selectedAssetPaths[i]);
- AssetDatabase.RenameAsset(selectedAssetPaths[i], assetName.Substring(removeFromStartCount, assetName.Length - removeFromStartCount));
- }
- updateSelectedAssetPaths();
- }
- EditorGUILayout.Separator();
- // Remove from end settings (truncate).
- EditorGUILayout.Separator();
- EditorGUILayout.LabelField("- Remove from End Settings:", EditorStyles.boldLabel);
- EditorGUILayout.Separator();
- guiContentLabel = new GUIContent("Remove From End Count", "Remove from end.");
- removeFromEndCount = EditorGUILayout.IntField(guiContentLabel, removeFromEndCount);
- EditorGUILayout.Separator();
- // Button to remove string at end from selected assets.
- guiContentLabel = new GUIContent("Remove From End",
- "Remove these many characters from the end.");
- if (GUILayout.Button(guiContentLabel))
- {
- for (int i = 0; i < selectedAssetPaths.Count; i++)
- {
- string assetName =
- getAssetNameFromPath(selectedAssetPaths[i]);
- AssetDatabase.RenameAsset(selectedAssetPaths[i], assetName.Substring(0, assetName.Length - removeFromEndCount));
- }
- updateSelectedAssetPaths();
- }
- EditorGUILayout.Separator();
- // Selected objects.
- guiContentLabel = new GUIContent("- Selected:",
- "Valid folder and typed assets selected in the PROJECT view.");
- EditorGUILayout.LabelField(guiContentLabel, EditorStyles.boldLabel);
- EditorGUILayout.Separator();
- scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
- {
- if (selectedAssetPaths.Count == 0)
- {
- EditorGUILayout.LabelField("Please select some asset(s) in the project view.", EditorStyles.miniBoldLabel);
- }
- else
- {
- for (int i = 0; i < selectedAssetPaths.Count; i++)
- {
- EditorGUILayout.BeginHorizontal();
- {
- //EditorGUILayout.LabelField("> " + selectedAssetPaths[i], EditorStyles.miniLabel);
- EditorGUILayout.LabelField("> " + getAssetNameFromPath(selectedAssetPaths[i]), EditorStyles.miniLabel);
- }
- EditorGUILayout.EndHorizontal();
- }
- }
- }
- EditorGUILayout.EndScrollView();
- }
- // ...
- void OnFocus()
- {
- updateSelectedAssetPaths();
- }
- // =================================
- // End functions.
- // =================================
- }
- // =================================
- // End namespace.
- // =================================
- }
- }
- // =================================
- // --END-- //
- // =================================
|