• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Hobo42 · Oct 23, 2013 at 05:02 AM · arrayscenefolderloading filelevelload

Load scenes from a Scenes folder to an array.

Hello, I'm new to the forums and to Unity, I am having a what I think would be a simple problem.

I have a LevelSelector class that handles the loading of various levels. In the class I would like to have an array of GameScenes / levels that are all found in a certain folder under Scenes\\GameLevels

Currently my code is able to load certain levels, but it's hard coded by name so if I ever add any more levels, I would have to constantly go back and add or remove the couple lines of code that loads that level.

Here is my code so far:

 using UnityEngine;
 using System.Collections;
 
 
 public class LevelSelector : ButtonManagerBehavior
 {
     /// <summary>
     /// Number of levels in the Application
     /// </summary>
     static int levelCount = Application.levelCount;
     
     /// <summary>
     /// Array that holds the levels from GameLevels
     /// </summary>
     GameScene[] levelArray = new GameScene[levelCount];
     
     
     /// <summary>
     /// Activate the button that will be pressed.
     /// </summary>
     /// <param name='buttonName'>
     /// Name of the button in the scene being pressed.
     /// </param>
     public override void ButtonPressed (string buttonName)
     {
         /// Switch statement that loads the approiate level based on the level name passed in.
         switch(buttonName)
         {
             /// Load level03 scene
         case "level3Button":
             Application.LoadLevel("level03");
             break;
             /// Load level04 scene
         case "level4Button":
             Application.LoadLevel("level04");
             break;
             /// Load level05 scene
         case "level5Button":
             Application.LoadLevel("level05");
             break;
             /// Go back to the main menu
         case "backToMenu":
             Application.LoadLevel("MainMenuGUITest");
             break;
         default:
             /// Attempt to load level scene with the same name as the buttonName param passed in
             base.ButtonPressed(buttonName);
             break;
         }
 
     }
 }


As you can see I haven't gotten very far. The switch statement is there to let me load the levels by their name, but I would like to have the levels in the GameLevels folder in the array levelArray.

Any help or insight will be very much appreciated! Thank you!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tarlius · Oct 23, 2013 at 06:15 AM

You can load the scenes via an index in place of the name, but I think is dependent on the order they are included in the build menu, so I'm not sure if that will scratch your itch.

Another approach would be to make a class that listens for a button press and has the level name to load serialized in it, and attach an instance of it to each button. Ie, something like:

 public class LevelLoaderButton : MonoBehaviour {
     [SerializeField] string levelName;
 
     public void OnPressed() {
         Application.LoadLevel(levelName);
     }
 }


Comment
Add comment · Show 3 · 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 Hobo42 · Oct 23, 2013 at 06:47 AM 0
Share

I believe I am already doing that in my switch statement where I am calling the levels by name. I should have mentioned that I loaded those 3 levels in the switch statement into the build menu too.

I am looking more for a way to load only the scenes in a certain folder in an array.

But thank you anyways!

avatar image Tarlius · Oct 23, 2013 at 07:10 AM 0
Share

If you do it this way you won't have to edit the code at least.

I don't think you can scan the folder at runtime because of how scenes get included in the game, but you could set up an editor script to scan the folder and build the UI in the editor. Not ideal, but perhaps better than doing it by hand?

avatar image Hobo42 · Oct 23, 2013 at 07:23 AM 0
Share

Hmmm, okay. I'll have to think of a way around this I guess.

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

15 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

Related Questions

Deactivate game modes until tutorial is finished? 1 Answer

Add prefabs from a folder to array as GameObject 2 Answers

How do i load entire folders into arrays? 2 Answers

Is there a way to drag and drop a folder into an array? 1 Answer

Is there a way to load all objects in multiple folders within a Resource folder into 1 array? 1 Answer


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