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!
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 ?