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 Babilinski · Jul 07, 2011 at 09:56 PM · scenesdrag-and-dropcachepublic variable

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.

Comment
Add comment · Show 5
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
avatar image SilverTabby · Jul 07, 2011 at 10:02 PM 0
Share

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.

avatar image Dreamblur · Jul 07, 2011 at 10:10 PM 0
Share

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).

avatar image Babilinski · Jul 07, 2011 at 10:11 PM 0
Share

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.

avatar image Dreamblur · Jul 07, 2011 at 10:14 PM 1
Share
  public string strLevel;
  .
  .
  .
  Application.LoadLevel(strLevel);
avatar image Babilinski · Jul 07, 2011 at 10:16 PM 0
Share

thank you

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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 );
 }
Comment
Add comment · Show 1 · Share
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
avatar image Matthew A · Mar 18, 2013 at 09:32 PM 1
Share

This is certainly useful, but seems wrong in a few ways.

  1. The actual suffix appended to scene object names is " (SceneAsset)" not default asset. ($$anonymous$$ight have changed since this was written).

  2. You are using TrimEnd. I do not think it does what you think it does.

  3. You really have to validate that the asset is a scene.

  4. Cope with null values (just removing the scene would be useful).

  5. 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


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