- Home /
How to get Scene Name in Edit Mode without #if UNITY_EDITOR
Hello!
So, I know that in order to get the Scene Name I usually have two options:
1 - Use UnityEditor.EditorApplication.currentScene, which requires me to be in an Editor class (file inside an Editor folder) or to use #if UNITY_EDITOR.
2 - Use Application.loadedLevelName, which works fine as long as I dont use it right after creating a new scene (the first option works in this case).
The thing is Im creating a script that needs to run in edit mode and which goes into a DLL, so the use of #if UNITY_EDITOR is crossed and I dont want to create a custom inspector for every class that needs to see the Scene Name just to get the Scene Name. How can I cover the case missing in the second option?
What else can I do to solve this?
Thanks!
Out of curiosity: why do you need current scene name from within a context of an individual object (its Editor in particular)?
Thats classified.
Joke!
In my software the user can save the model being worked with and since a lot of tests are involved I dont want it to override the pre-existing models and since the same model can be loaded in different scenes, a solution was to save the current changes in a Scene_Name/$$anonymous$$odel_Name folder, thus not overriding the previous models nor the models used in other scenes.
The Applicaiton class seems to be in the UnityEngine namespace, which means it's available both in Editor and Runtime. Does Applicaiton.loadedLevelName report a wrong value?
After some more tests I saw that Application.loadedLevelName just wont work in the case where a new scene was just created where UnityEditor.EditorApplication.currentScene works.
In that case I would recommend to not rely on Unity scene flow, but, I guess, from what it sounds like, they're deeply in the workflow...
Answer by NeverHopeless · Aug 17, 2015 at 03:31 PM
If i understood you correctly, we know for loading a scene we have to call Application.LoadLevel
Or similar functions of application class. In this function we have to pass the scene name to be load. Save the scene name in a singleton class say Session
and set before loading the new scene.
E.g,
public sealed class GameSession
{
private static volatile GameSession instance;
private static object syncRoot = new object ();
public string loadedLevelName;
private GameSession ()
{
}
public static GameSession Instance {
get {
if (instance == null) {
lock (syncRoot) {
if (instance == null)
instance = new GameSession ();
}
}
return instance;
}
}
}
and in some event say OnCollisionEnter2D
, load a new level:
void OnCollisionEnter2D(Collision2D other)
{
GameSession.loadedLevelName = "GameScene"; // Save in session
Application.LoadLevel("GameScene");
}
Now wherever you want you can use:
Debug.Log(GameSession.loadedLevelName);
Is this what you want ?
Your answer
Follow this Question
Related Questions
Add GUI elements to Scene View? 3 Answers
Z sorting of Handles 0 Answers
How can I make function run in the editor only when I press a button in the inspector? 1 Answer
Dynamic editor dll linking? 0 Answers
Multiple Cars not working 1 Answer