-
Notifications
You must be signed in to change notification settings - Fork 450
/
Copy pathReplaceCharacterInGameObjectNames.cs
69 lines (55 loc) · 2.33 KB
/
ReplaceCharacterInGameObjectNames.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
// editor tool to replace string from selected GameObject names
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
namespace UnityLibrary.Tools
{
public class ReplaceCharacterInGameObjectNames : EditorWindow
{
private string searchString = "|";
private string replaceString = "@";
[MenuItem("Tools/Replace Characters in GameObject Names")]
public static void ShowWindow()
{
GetWindow<ReplaceCharacterInGameObjectNames>("Replace Characters");
}
private void OnGUI()
{
GUILayout.Label("Replace Characters in Selected GameObject Names", EditorStyles.boldLabel);
searchString = EditorGUILayout.TextField("Search String", searchString);
replaceString = EditorGUILayout.TextField("Replace String", replaceString);
int selectedObjectCount = Selection.gameObjects.Length;
GUILayout.Label($"Selected GameObjects: {selectedObjectCount}", EditorStyles.label);
if (GUILayout.Button("Replace"))
{
ReplaceCharacters();
}
}
private void ReplaceCharacters()
{
GameObject[] selectedObjects = Selection.gameObjects;
if (selectedObjects.Length == 0)
{
Debug.LogWarning("No GameObjects selected.");
return;
}
// Start a new undo group
Undo.IncrementCurrentGroup();
Undo.SetCurrentGroupName("Replace Character in GameObject Names");
int undoGroup = Undo.GetCurrentGroup();
foreach (GameObject obj in selectedObjects)
{
if (obj.name.Contains(searchString))
{
Undo.RecordObject(obj, "Replace Character in GameObject Name");
obj.name = obj.name.Replace(searchString, replaceString);
EditorUtility.SetDirty(obj);
}
}
// End the undo group
Undo.CollapseUndoOperations(undoGroup);
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
Debug.Log($"Replaced '{searchString}' with '{replaceString}' in the names of selected GameObjects.");
}
}
}