- Home /
cashing in Scenes to a public vaiable
Is there a line of C# code that would let me drag and drop scenes into the inspector?
Okay i have right now have "Application.LoadLevel("MainScreen")"
I want to to make the word "MainScreen" a variable so people can just drag and drop the scene (that they want to be the main Screen)
basically so that they can change the sene without editing the code =D.
What exactly are you looking for? Are you trying to make it so that when you drag-drop a scene in edit mode, it opens? Or is there something else.
A little bit more details would be useful.
A scene is a file. You can expose a reference to a scene on an inspector by designating a string to represent it (its path). Why you would want to do that though, is a completely mystery, since you can't access anything inside an external scene file via scripting unless you know how a scene file is structured (i.e., you have access to Unity's source code).
Okay i have right now have "Application.LoadLevel("$$anonymous$$ainScreen")"
I want to to make the word "$$anonymous$$ainScreen" a variable so people can just drag and drop the scene (that they want to be the main Screen)
basically so that they can change the sene without editing the code =D.
public string strLevel;
.
.
.
Application.LoadLevel(strLevel);
Answer by Joshua · Jul 08, 2011 at 03:13 AM
While dreamblur tells you how to load a scene based on it's name, he doesn't tell you how to actually 'drag and drop' this scene into the inspector and have the inspector sort out the name. I can imagine there are cases where you're setting up a menu when you don't want to type twenty levelnames, but would much rather drag and drop them in.
To do that you need to write a custom inspector. This below assumes you have one script named LoadLevel, which will load a level based on what you drag and dropped, and a second script (in a folder named Editor!) called LoadLevel_Editor. What you will get is the same you get when, in a normal script, you type var someTransform:Transform, with a field where you can drag and drop the Transform in, but here it's a scene you drop in.
//script name is LoadLevel_Editor
@CustomEditor( LoadLevel ) class LoadLevel_Editor extends Editor
{
var type : String = "(DefaultAsset)"; //GetDragAndDropTitle returns a name + type, in this case LevelName(DefaultAsset), we want to remove the DefaultAsset part
var fullName : String;
override function OnInspectorGUI()
{
GUILayout.Label( fullName );
target.scene = EditorGUILayout.ObjectField( target.scene, Object );
if( target.scene != null )
{
fullName = levelName = ObjectNames.GetDragAndDropTitle( target.scene );
var charArray = type.ToCharArray();
target.levelName = levelName.TrimEnd( charArray );
}
}
}
//script name is LoadLevel
var levelName : String;
var scene : Object;
function LoadLevel()
{
Application.LoadLevel( levelName );
}
This is certainly useful, but seems wrong in a few ways.
The actual suffix appended to scene object names is " (SceneAsset)" not default asset. ($$anonymous$$ight have changed since this was written).
You are using TrimEnd. I do not think it does what you think it does.
You really have to validate that the asset is a scene.
Cope with null values (just removing the scene would be useful).
Scene names got from drag + drop can't be used to load the scene in the editor. You need the actual path.
A possibly improved version:
Object newScene = EditorGUILayout.ObjectField(scene, typeof(Object), false);
if (newScene == null)
{
scene = null;
sceneName = null;
}
else if (newScene != scene)
{
string fullName = ObjectNames.GetDragAndDropTitle(newScene);
string sceneSuffix = " (SceneAsset)";
if (fullName.EndsWith(sceneSuffix))
{
scene = newScene;
sceneName = fullName.Substring(0, fullName.Length - sceneSuffix.Length);
}
else
{
Debug.LogWarning("That asset is not a scene!");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612134918im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Where is scene data stored? 1 Answer
Transfer data between scenes + saving it C# 1 Answer
Built Unity Scene converting to Unity Scene 1 Answer
Unity 5.5.1f1 Save and load data 0 Answers
I need help with my audio slider 0 Answers