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 /
avatar image
0
Question by AstroBeans9 · May 12, 2020 at 11:22 PM · sceneseditorgui

[SOLVED] Swapping Scene Names (or Asset Names)

This post is for future users that want to swap to scene names automatically through code.

(1) CREATE A C# SCRIPT and paste the following inside.

NOTE: Change the class name to the C# Script name.

 using UnityEngine;
 using UnityEditor;
 using UnityEngine.SceneManagement;
 using System;
 
 public class SceneNameEditor : EditorWindow
 {
     const string path = "Assets/Scenes/"; //CHANGE THIS TO THE FOLDER CONTAINING WHATEVER ASSET YOU WANT TO SWAP THE LAST "/" IS VERY IMPORTANT
                                             
     const string tempSceneName = "tempAssetSceneFromEditor"; //A TEMP NAME that you probably won't ever actually use lol
                                                              
     const string exten = ".unity"; //CHANGE THIS TO THE EXTENSION OF THE ASSET YOU MOVING (for scenes its .unity). THE "." is very important
                                   
     string levelNumber1 = "Level01";
     string levelNumber2 = "Level02";
     string levelNumberToInsert = "Level07";
     string levelNumberToGo = "Level09";
 
     [MenuItem("Window/Scene Changer")]
     public static void ShowWindow()
     {
         GetWindow<SceneNameEditor>("Scene Changer");
     }
 
     void OnGUI()
     {
         levelNumber1 = EditorGUILayout.TextField("Level Number (1)", levelNumber1);
         levelNumber2 = EditorGUILayout.TextField("Level Number (2)", levelNumber2);
 
 
         if (GUILayout.Button("SWAP!"))
         {
             levelNumber1 = EditorGUILayout.TextField("Level Number (1)", levelNumber1);
             levelNumber2 = EditorGUILayout.TextField("Level Number (2)", levelNumber2);
 
             SwapNames(levelNumber1, levelNumber2);
             AssetDatabase.SaveAssets();
         }
 
         levelNumberToInsert = EditorGUILayout.TextField("Level Insert", levelNumberToInsert);
         levelNumberToGo = EditorGUILayout.TextField("Level To Go", levelNumberToGo);
 
         if (GUILayout.Button("INSERT!"))
         {
             levelNumberToInsert = EditorGUILayout.TextField("Level Insert", levelNumberToInsert);
             levelNumberToGo = EditorGUILayout.TextField("Level To Go", levelNumberToGo);
 
             InsertInto(levelNumberToInsert,levelNumberToGo);
             AssetDatabase.SaveAssets();
         }
     }
 
 
     //You can tailor this to work with any asset, in this case, its using for swapping scene names.
     void SwapNames(string name1, string name2)
     {
         string tempName1 = name1;
         string tempName2 = name2;
         AssetDatabase.RenameAsset(path + name1 + exten, tempSceneName + exten); //curr origLevel 1
         AssetDatabase.RenameAsset(path + name2 + exten, tempName1 + exten); //curr origLevel 2 w/ Level01 name
         AssetDatabase.RenameAsset(path + tempSceneName + exten, tempName2 + exten);// curr origLevel 1 now to Level02
     }
 
     void InsertInto(string levelToMove, string posToGo)
     {
         if (isExists(posToGo))
         {
             InsertInto(posToGo, getNextLevel(posToGo));
         }
 
         AssetDatabase.RenameAsset(path + levelToMove + exten, posToGo);
 
     }
 
     string getNextLevel(string currLevel)
     {
         string sNumbs = "";
         int size = currLevel.Length;
 
         for (int i = 0; i < size; i++)
         {
             if (i <= 4)
             {
                 continue;
             }
 
             sNumbs += currLevel[i];
         }
 
         int numb = Convert.ToInt32(sNumbs);
         numb += 1;
 
         sNumbs = numb.ToString();
         if (numb < 10)
         {
             sNumbs = "0" + sNumbs;
         }
 
         
 
         return "Level" + sNumbs;
         
     }
 
     bool isExists(string assetName)
     {
         SceneAsset s = AssetDatabase.LoadAssetAtPath(path + assetName + exten, typeof(SceneAsset)) as SceneAsset;
         if (s == null)
         {
             return false;
         }
 
         return true;
     }
 }
 

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

0 Replies

· Add your reply
  • Sort: 

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

129 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

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Unity Editor Script Changes To Scene Not Recognized 1 Answer

Retaining Data Between Levels 2 Answers

how do i Disable and Enable buttons? 2 Answers

Unity 4.6 Button onClick not working in Scenes 1 Answer


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