- Home /
Get the currently open scene name or file name
I have an editor script that needs to know which one of my scenes is open in the editor at the moment. Preferably it would give me the name of the file, like "myScene.unity" or "Assets/path/myScene.unity".
Any ideas?
EDIT: My editor script needs to make persistent changes to the scene, therefore the play button will not be pressed while it runs.
But how do I get rid of the path part of the string so I just have myCurrentScene.unity rather than Assets/path/myCurrentScene.unity??
System.IO.Path.GetFileName(yourPath) - though this really should have been a seperate question
To remove the path part you can use string's .split function, which will return you string[]. The last string on the array will be the scene.unity
Any solution in Unity 5.3? The old solutions seem not working anymore.
Try using or looking docs about Scene.name
http://docs.unity3d.com/ScriptReference/Scene$$anonymous$$anagement.Scene-name.html
Answer by Molix · Apr 22, 2010 at 05:57 PM
Correct!!! Thank you. Realizing that EditorApplication exists has solved another problem of $$anonymous$$e.
In execution time it will bring the scene in which the script was loaded first, not the current loaded level / scene, unlike Application.loadedLevelName
, as Elliot already advised.
That's why you switch to using Application.loadedLevelName at runtime. It's an editor script, anyway- you shouldn't really be using it at runtime.
FYI Sometimes when using Application.LoadLevelAdditive() the variable Application.loadedLevelName does not match current opened scene.
Answer by e-bonneville · Apr 07, 2010 at 08:16 PM
Try (Application.loadedLevelName)
. You can find the (scanty) page on it here. It gives you the name of the last level loaded, which is probably the current level.
So close! That trick only works when the play button has been pressed. Excellent try though, I didn't find that with my searches.
Thats not true, you can also use it inside an Editor
or EditorWindow
. And its just the name of the scene ins$$anonymous$$d the path you will receive with currentScene
.
Oh well. BTW, the API for Unity is available at this link; http://unity3d.com/support/documentation/ScriptReference/
I prefer this custom search, which includes the script reference: http://www.google.com/cse/home?cx=002470491425767499270:iugs1ezlsfq
Answer by ABerlemont · Jan 20, 2016 at 04:04 PM
Since they added the SceneManager the right way to do that is
http://answers.unity3d.com/questions/1116932/what-is-the-new-scenemanagement-equivalent-for-app.html
Scene scene = SceneManager.GetActiveScene();
scene.name; // name of scene
they also have EditorScene$$anonymous$$anager.GetActiveScene() for usage in the editor
Answer by Dealzu-The-Wikid · Jul 15, 2014 at 10:17 PM
I finally with all the clues after reading for several hours on the forums found the solution to this.. On your first script where you call application.LoadLevel, it is correct this script will be destroyed and not run anymore code... On the player I made another script to determine which zone I was in and used onLevelWasLoaded function... Inside this function I figured out which scene I had just loaded with Application.loadedLevelName.. and THEN and ONLY THEN, despite advice from all over these forums, could I place the player's position for this zone. Here is a quick example.. Make sure your zones have proper spacing in them or this will not work! Also make sure that you don't forget to label the locations you want to teleport to in the inspector :)
//this code goes on your teleporter
#pragma strict
var MyLevel:String;
var MyPlayer:GameObject;
var Destination : Transform;
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.tag == "Player")
{
MyPlayer = other.gameObject;
DontDestroyOnLoad(MyPlayer);
Application.LoadLevel (MyLevel);
}
}
//this code goes on the player
#pragma strict
var portalDropoff : Vector3;
var portalDropoffCOL : Vector3;
function OnLevelWasLoaded()
{
portalDropoff = gameObject.Find("portalDropoff").transform.position;
if (Application.loadedLevelName == "Whatever the name of your zone 1 is as a string")
{
this.transform.position = portalDropoff;
}
if(Application.loadedLevelName == "whatever the name of the second zone is")
{
this.transform.position = portalDropoffCOL;
}
}