• 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
Question by thrmotta · Aug 17, 2015 at 02:46 PM · c#sceneeditor-scriptingdllc# dll

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, w$$anonymous$$ch requires me to be in an Editor class (file inside an Editor folder) or to use #if UNITY_EDITOR.

2 - Use Application.loadedLevelName, w$$anonymous$$ch works fine as long as I dont use it right after creating a new scene (the first option works in t$$anonymous$$s case).

The t$$anonymous$$ng is Im creating a script that needs to run in edit mode and w$$anonymous$$ch 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 t$$anonymous$$s?

Thanks!

Comment

People who like this

0 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 stepan-stulov · Aug 17, 2015 at 02:52 PM 0
Share

Out of curiosity: why do you need current scene name from within a context of an individual object (its Editor in particular)?

avatar image thrmotta · Aug 17, 2015 at 03:01 PM 0
Share

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/Model_Name folder, thus not overriding the previous models nor the models used in other scenes.

avatar image stepan-stulov · Aug 17, 2015 at 03:03 PM 0
Share

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?

avatar image thrmotta · Aug 17, 2015 at 03:08 PM 0
Share

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.

avatar image stepan-stulov · Aug 17, 2015 at 03:15 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image

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 t$$anonymous$$s 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 t$$anonymous$$s what you want ?

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

26 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Dynamic editor dll linking? 0 Answers

Add GUI elements to Scene View? 3 Answers

Z sorting of Handles 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