Any easy way to rearrange scene names
I have all my scenes names named level1,level2,.....level30.
During my editor workflow I need to insert some new scene in between others. For example trying to add level 10 if one already exists, I would want to add it as 10 and bump the other level >= 10 up a number. but can't figure out an easy way to have all the other scenes bump up a number without painfully renaming them all.
I know I can't be the only person to have this issue. Does anyone know of an easy solution?
Answer by Hellium · Jul 20, 2019 at 01:04 PM
Here is a very simple custom editor I made. It may not be perfect and lacks on some features I guess, but it should fit your needs.
Create a new script called SceneRenamer.cs
and put this file into an Editor
folder. Then, copy paste the following code.
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System;
public class SceneRenamer : EditorWindow
{
private class SceneListElement
{
public bool Rename;
public string ScenePath;
public SceneAsset SceneAsset;
public bool EnabledInBuildSettings;
}
private const string tempPrefix = "TEMP_SCENERENAMER_";
private List<SceneListElement> scenes;
private ReorderableList reorderableList;
private string sceneName = "Scene";
private string sceneFormat = "000";
private int sceneSuffixIndex = 0;
private GUIStyle renamedSceneStyle;
/// <summary>
/// Shows the window using a menu item
/// </summary>
[MenuItem("Window/SceneRenamer")]
public static void ShowWindow()
{
SceneRenamer window = GetWindow<SceneRenamer>( "SceneRenamer" );
window.Init();
}
/// <summary>
/// Initializes the window
/// </summary>
private void Init()
{
// Get scenes from build settings
// And create a list containing the data structure we need
EditorBuildSettingsScene[] buildSettingsScenes = EditorBuildSettings.scenes;
scenes = buildSettingsScenes.Select( s =>
{
return new SceneListElement()
{
Rename = true,
ScenePath = s.path,
SceneAsset = AssetDatabase.LoadAssetAtPath( s.path, typeof( SceneAsset ) ) as SceneAsset,
EnabledInBuildSettings = s.enabled
};
}).ToList();
// Create reordable list for custom window
reorderableList = new ReorderableList( scenes, scenes.GetType().GetGenericArguments()[0], true, false, false, false );
reorderableList.drawHeaderCallback = DrawReordableListHeader;
reorderableList.drawElementCallback = DrawReordableLisElement;
EditorBuildSettings.sceneListChanged -= Init;
EditorBuildSettings.sceneListChanged += Init;
}
private void OnDestroy()
{
EditorBuildSettings.sceneListChanged -= Init;
}
/// <summary>
/// Draws the GUI in the window
/// </summary>
private void OnGUI()
{
if ( reorderableList == null )
Init();
if ( renamedSceneStyle == null )
renamedSceneStyle = new GUIStyle( GUI.skin.label ) { alignment = TextAnchor.MiddleRight };
sceneSuffixIndex = 0;
reorderableList.DoLayoutList();
if ( Screen.width > 300 )
{
EditorGUIUtility.labelWidth = 75;
EditorGUILayout.BeginHorizontal();
}
sceneName = EditorGUILayout.TextField( "Base name", sceneName );
sceneFormat = CleanFormatString( EditorGUILayout.TextField( "Suffix", sceneFormat ) );
if ( Screen.width > 300 )
EditorGUILayout.EndHorizontal();
if ( GUILayout.Button( "Rename" ) )
{
RenameScenes();
ReorderScenes();
}
}
private string CleanFormatString( string input )
{
return System.Text.RegularExpressions.Regex.Replace( input, "[^0#]", string.Empty );
}
/// <summary>
/// Reorder the scenes in the build settings
/// </summary>
private void ReorderScenes()
{
EditorBuildSettings.scenes = scenes.Select( s => new EditorBuildSettingsScene( s.ScenePath, s.EnabledInBuildSettings ) ).ToArray();
}
/// <summary>
/// Renames the scene
/// </summary>
/// <param name="temp">If <c>true</c>, set a temporary name for the scenes</param>
private void RenameScenes( bool temp = true )
{
AssetDatabase.Refresh();
for ( int index = 0, sceneIndex = 0 ; index < reorderableList.list.Count ; ++index )
{
SceneListElement scene = reorderableList.list[index] as SceneListElement;
if ( !scene.Rename ) continue;
RenameScene( scene, ++sceneIndex, temp );
}
AssetDatabase.SaveAssets();
if ( temp )
RenameScenes( false );
}
/// <summary>
/// Renames a single scene
/// </summary>
/// <param name="scene">The scene to rename</param>
/// <param name="sceneIndex">The index of the scene</param>
/// <param name="temp">If <c>true</c>, set a temporary name for the scene</param>
private void RenameScene( SceneListElement scene, int sceneIndex, bool temp)
{
string newName = GetNewName( scene, sceneIndex, temp );
string guid = AssetDatabase.AssetPathToGUID( scene.ScenePath );
string result = AssetDatabase.RenameAsset( scene.ScenePath, newName );
if ( !string.IsNullOrEmpty( result ) )
{
Debug.LogError( result );
}
else
{
scene.SceneAsset.name = newName;
scene.ScenePath = AssetDatabase.GUIDToAssetPath( guid );
}
}
private string GetNewName( SceneListElement scene, int sceneIndex, bool temp )
{
return string.Format( "{0}{1:" + sceneFormat + "}", ( temp ? tempPrefix : string.Empty ) + sceneName, sceneIndex );
}
/// <summary>
/// Draws the header of the reordable list
/// </summary>
/// <param name="rect"></param>
public void DrawReordableListHeader( Rect rect )
{
GUI.Label( rect, "Scenes in build" );
}
/// <summary>
/// Draws an element of the reoardable list
/// </summary>
/// <param name="rect">The rect the element must be drawn in</param>
/// <param name="index">The index of the element in the list</param>
/// <param name="isActive">Indicates whether the element is active (hovered) or not</param>
/// <param name="isFocused">Indicates whether the element is focused or not</param>
public void DrawReordableLisElement( Rect rect, int index, bool isActive, bool isFocused )
{
SceneListElement scene = reorderableList.list[index] as SceneListElement;
Rect checkRect = rect;
checkRect.width = rect.height;
scene.Rename = GUI.Toggle( checkRect, scene.Rename, null as string );
Rect labelRect = rect;
labelRect.x += checkRect.width;
labelRect.width -= checkRect.width;
GUI.Label( labelRect, scene.SceneAsset.name );
if( scene.Rename )
GUI.Label( labelRect, GetNewName( scene, ++sceneSuffixIndex, false ), renamedSceneStyle );
}
}
In the top menu of Unity, open the window using the Window > SceneRenamer
option.
In the window, you will see all your scenes currently in your build settings. The two fields below the list will let you bulk rename all the lists using a base name and a numeral suffix. The format of the suffix must be composed of #
s followed by 0
s. A #
means the number is optional, 0
means the character is mandatory.
You can uncheck the the scenes you don't want to be renamed, and reorder the scenes as you want. When hitting Rename
, the scenes will be renamed and reorganized in your Build Settings
window.
It's actually a quite versatile approach. Two things would probably be useful when you work with many scenes:
An invert selection button
A start index value
In addition it might be useful to be able to enter a regex to filter the input names.
Anyways, such tools without explicit dependencies are a really useful addition to the editor. The simplest things are often the most useful. You may want to post a copy on the wiki
Yes, I agree, as I have indicated, it is a very small custom editor I made and several features, including the ones you have mentioned would be very nice. After very few researches, I haven't found a free tool to bulk rename scenes so I took the liberty to develop a very small one to answer the immediate needs of ChubbyBump.
Your answer
Follow this Question
Related Questions
How to copy and paste animation events from one clip to another if those clips are included in FBXs? 1 Answer
OnValidate For Other Components? 0 Answers
InvalidOperationException: Operation is not valid due to the current state of the object System 1 Answer
Unity5-serializedObject.FindProperty("listname") for ReorderableList doesn't work... 2 Answers
Applying values from an animationClip to bones of a skinnedMesh via code? 0 Answers