- Home /
Is there anyway to batch renaming via Editor [not on Runtime] multiple game objects in the hierarchy ?
I feel like this is a common need for many. Is there any proper way to rename multiple objects according a linear sequence or anything close to that.
Let' say for example I drop 100 objects in my scene in a certain order in the hierarchy, how could I rename them as obj1-obj2-obj3 and so on...
Thank you
Answer by RaptorRush · Aug 10, 2020 at 09:11 PM
Yes, it’s possible using a custom editor window. Like this:
using UnityEngine;
using UnityEditor;
public class RenameChildren : EditorWindow {
private static readonly Vector2Int size = new Vector2Int(250, 100);
private string childrenPrefix;
private int startIndex;
[MenuItem("GameObject/Rename children")] public static void ShowWindow() {
EditorWindow window = GetWindow<RenameChildren>();
window.minSize = size;
window.maxSize = size;
}
private void OnGUI() {
childrenPrefix = EditorGUILayout.TextField("Children prefix", childrenPrefix);
startIndex = EditorGUILayout.IntField("Start index", startIndex);
if (GUILayout.Button("Rename children")) {
GameObject[] selectedObjects = Selection.gameObjects;
for (int objectI = 0; objectI < selectedObjects.Length; objectI++) {
Transform selectedObjectT = selectedObjects[objectI].transform;
for (int childI = 0, i = startIndex; childI < selectedObjectT.childCount; childI++) selectedObjectT.GetChild(childI).name = $"{childrenPrefix}{i++}";
}
}
}
}
To use it:
Save the script in your project.
Go to GameObject/Rename children.
Select the parent of the objects to rename.
Fill the fields and press the button.
Answer by AmitSMG · May 17, 2021 at 09:58 AM
Thanks @RaptorRush this works perfectly and exactly what i was looking for :)
Answer by thedooptr · Jun 04, 2021 at 10:12 PM
I couldn't do it can you help me it gives an error
Hi!
You named your class 'Mizaresimleri'
You are passing 'RenameChildren' as an argument to the 'GetWindow<>' command
There's no 'RenameChildren' class in your case.
The command should look as follows:
GetWindow<Mizaresimleri>
Answer by raiden · Feb 06 at 07:46 PM
I thought I would add a way to rename selected objects in case you are not naming the child objects.
using UnityEditor;
using UnityEngine;
public class RenameSelected : EditorWindow
{
// public fields
public GameObject[] objects;
// private fields
private static readonly Vector2Int size = new Vector2Int(250, 100);
private string _gameObjectPrefix;
private int _startIndex;
private SerializedObject _serializedObject;
[MenuItem("GameObject/Rename Selected")]
public static void ShowWindow()
{
EditorWindow window = GetWindow<RenameSelected>();
window.minSize = size;
window.maxSize = size;
}
private void OnEnable()
{
ScriptableObject target = this;
_serializedObject = new SerializedObject(target);
}
private void OnGUI()
{
_gameObjectPrefix = EditorGUILayout.TextField("Selected Prefix", _gameObjectPrefix);
_startIndex = EditorGUILayout.IntField("Start Index", _startIndex);
_serializedObject.Update();
SerializedProperty serializedProperty = _serializedObject.FindProperty("objects");
EditorGUILayout.PropertyField(serializedProperty, true);
if (GUILayout.Button("Rename Objects"))
{
for (int objectI = 0, i = _startIndex; objectI < serializedProperty.arraySize; objectI++)
{
serializedProperty.GetArrayElementAtIndex(objectI).objectReferenceValue.name = $"{_gameObjectPrefix}{i++}";
}
}
_serializedObject.ApplyModifiedProperties();
}
}
This will create an array, in the editor window, simply select the order of the GameObjects, then set your prefix and start index, and hit the Rename Objects button.
-Larry