Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Flightkick · Jun 19, 2015 at 03:27 PM · runtimescenesbuildsettings

Retrieve list of available scenes from within compiled game

Hi,

Unity offers the ability to select scenes to build in the build settings, these scenes are also assigned an integer in the order they where added. This way you can load scenes by using:

 Application.LoadLevel ("0");

or by using the name i.e.:

 Application.LoadLevel ("scenename");

Now I'm trying to create a minigame launcher. All minigames are stored in a directory 'Minigames', the minigames have their own scene. They are build together with the launcher by enabling the scenes in the build settings.

I would like to retrieve a list of all the other scenes that have been build on runtime so I can display a list of all available minigames (scenes) to launch.

I've searched other topics but most of them recommend using the class EditorApplication, since I want to access this information on runtime it won't be possible to use that class.

Does anyone have an idea how to do this?

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Flightkick · Jun 20, 2015 at 01:25 PM

Okay so I've finally found a way to do this.

First I use the [PostProcessScene] attribute somewhere in a static method, I use a bool to ensure the logic will only be executed once. This piece of code is also surrounded by conditional tags i.e.

 #if UNITY_EDITOR

Now in this method I scan my Minigame directory for subdirectories (each subdirectory contains a minigame), then I try to find a json file containing the relative path to the .untiy scene. All minigames need to specify their main scene in this json file, this is because I only want to know the main scenes of the minigame, you don’t have to do this if you want information for all scenes. I combine the .unity relative path with the directory I'm scanning in so I have the absolute path to the unity project defined in the json configuration file

Then use the EditorBuildSettings class to retrieve the scene id's and paths, I loop through them and check if the paths equal the paths retrieved from the json files and if the scenes are enabled in the buildsettings. If they match up I've got a correctly configured minigame.

I've created an object which holds all information about the minigame (name, scene id, path to scene, etc.), a collection of these objects will be serialized to the resources folder as a json file (but with a .txt extension). Since this happens when the first scene is being post-processed this means the json file written to the resources folder will also be included when the build starts.

When the game actually starts I only have to load the json file from the resources as a TextAsset (this is why it’s given a .txt extension). Then I deserialize the file back into a list containing the minigame information. So now I do have a list of the compiled scenes (filtered to only main scenes). Now I can access all scene information by my minigame information class.

Comment
Add comment · 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
2

Answer by Arcanor · Jun 20, 2015 at 03:32 PM

You could also do something like this, if you don't want to keep track of things prior to making your build.

 using UnityEngine;
 using System.IO;
 
 ...
 
 string folderName = Application.dataPath + "/Scenes";
 var dirInfo = new DirectoryInfo(folderName);
 var allFileInfos = dirInfo.GetFiles("*.unity", SearchOption.AllDirectories);
 foreach (var fileInfo in allFileInfos)
 {
     Debug.Log("Found a scene file: " + @fileInfo.FullName);
 }
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 Flightkick · Jun 20, 2015 at 06:50 PM 0
Share

Given the question I asked initially yes that would be a nice way to approach this. However, as I was implementing this I also noticed that I had to keep track of the scenes prior to building the project. Therefor I am checking both the $$anonymous$$igame config json and the buildsettings (I forgot to mention the change in my requirement).

Thanks for your addition to my answer.

avatar image
0

Answer by azad3h · May 03, 2017 at 12:34 PM

I had the same problem and ended up using a ScriptableObject to save the data using an Editor Script, following the instructions in this blog. I had to modify the solution in the blog also, as the contents of the instance of the ScriptableObject wouldn't save on the file system but only within the unity editor's cache. If you use this solution, make sure to modify the editor script as follows:

 // First, try to load the list if already exists
 // ... related code ...
 
 // If doesn't exist, create it!
 // ... related code ...  
 
 AssetDatabase.StartAssetEditing();
 // Fill the array or whatever attribute of the ScriptableObject
 AssetDatabase.StopAssetEditing();
 
 // Writes all unsaved asset changes to disk
 EditorUtility.SetDirty(list);
 AssetDatabase.SaveAssets();

BTW, I tried the answer by @Arcanor too. It works within the unity editor but not in the deployed build (on Android). Is there some additional setting required for it to work in the deployed build? It would have been a cleaner solution if it worked.

Comment
Add comment · 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
0

Answer by aweFalafelApps · Oct 04, 2019 at 03:45 AM

Old I know, but it matched my search, so I'm posting my solution. This seems to be possible at runtime now using SceneUtility.GetScenePathByBuildIndex

What I did:

 var sceneNames = new List<string>();
 var regex = new Regex(@"([^/]*/)*([\w\d\-]*)\.unity");
 for (int i = 0; i < SceneManager.sceneCountInBuildSettings; i++)
 {
     var path = SceneUtility.GetScenePathByBuildIndex(i);
     var name = regex.Replace(path, "$2");
     sceneNames.Add(name);
 }

Regex Credit

You can also use System.IO.Path.GetFileNameWithoutExtension instead of the regex.

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

How to save current scene of the program and upload the scene to web? 1 Answer

Export objects to a .3DS file at runtime 1 Answer

Using scene created with SceneManager.CreateScene during runtime 0 Answers

How to edit 3D Text at runtime? 1 Answer

Is there a tool to measure a distance in runtime? 4 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