Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ChubbyBump · Jul 20, 2019 at 04:09 AM · editorsceneeditor-scriptingprogramming

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer Wiki

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 0s. 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.

alt text


scenesrenamer.png (13.6 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jul 20, 2019 at 01:24 PM 0
Share

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

avatar image Hellium Bunny83 · Jul 20, 2019 at 01:37 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

247 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges