• 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 /
  • Help Room /
avatar image
1
Question by Gablock · Mar 16, 2017 at 08:43 PM · 2d

How to have a lot of levels without a lot of scenes ?

Hi I'm currently working on a 2D 2-4 players game and i need to make +100 levels, but making one level per scene is not very optimized and need a loading time. So how can i do plz ?

(sry for my bad english)

Comment
Add comment · Show 2
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 Matt1000 · Mar 16, 2017 at 08:58 PM 0
Share

Could you please describe your levels, i might know a way but i need to know how levels are composed.

avatar image Gablock Matt1000 · Mar 16, 2017 at 09:00 PM 0
Share

There is blocks, spawners, players, differents weapons, background color, speed of players, gravity level etc.

Gravity level, background color and other things like that are just variables in a level manager. Other things like players, spawners (spawn objects and weapons) and other physics things have there own script. Blocks are just a collider with a sprite a layer and a tag.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Matt1000 · Mar 16, 2017 at 09:55 PM

Fine, then this is what you should do... its a little bit heavy but it is probably the best way to do it. In order to do it you'll need to create classes (non-monobehaviours) for most objects. Some examples:

 public class Block {
     public block (Vector2 pos, Vector2 _size, string tag, string layer) {
         position = pos;
         size = _size;
         tagName = tag;
         layerName = layer;
     }
     
     public Vector2 position;
     public Vector2 size;
     public string tagName; // it's the fastest way i found to save tags and layers,
     public string layerName;
 }

 public class Manager {
     public Manager (Color color, float gravity) {
         gravitySpeed = gravity;
         backGroundColor = color;
     }
 
     public float gravitySpeed;
     public Color backGroundColor;
 }

And finally you need one more class for each level:

 public class Level {
     public level (Block[] blocks, Manager manager, Players[] players, Etc.) {
         allBlocks = blocks;
         levelManager = manager;
         allPlayers = players;
     }
 
     public Block[] allBlocks;
     public Manager levelManager;
     public Player[] allPlayers;
     //All data for each level
 }

With all that you simply create a scriptableObject class. More info on scriptableObjects. It is to save and mantain all that data through open and closing of unit and the game. And in that file you need to have something like this:

 [CreateAssetMenu ()] //This will let you to create the file.
 public class LevelSaver : ScriptableObject {
     public Level[] allLevels; //or...
     public List<Level> allLevels //I'd recommend this one here.
 
     public void Awake () {
         SetLevels ();
     }
 
     void SetLevels () {
         allLevels.Add (new Level ( new Block[] { new Block (/*data*/), new Block (/*data*/)}, new Manager (/*Manager data*/), new Player (/*Player data*/)));
 
 //I know it looks messy but remember you are saving a whole level here...
 // That Add () method only works for lists though.
     }
 }

Since you have the [CreateAssetMenu ()] you can create this file through Assets/Create/LevelSaver, but remember to have only one. Everytime you change the scripts delete the previous file and create a new one.

then simply put this file in the Resources foulder and you can access it with Resources.FindObjectsOfTypeAll<LevelSaver> (). Then simply write levelSaver.allLevels [1].levelManager.gravitySpeed and should work. Hope this is useful, i know it seems like a lot of wirk but this things come in handy in the end. Good luck! ;D

A little info on CreateAssetMenu Attribute

Comment
Add comment · Show 4 · 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 Gablock · Mar 17, 2017 at 03:18 PM 0
Share

Thanks a lot :)

But i don't understand three things :

First, how to saves scripted objects like weapons and spawners ? How do the scripts detects all blocks and other things like tags or do i need to set them manually ? How does the objects spawn ? Thanks

avatar image Matt1000 Gablock · Mar 17, 2017 at 07:18 PM 0
Share

Fisrt, how are they saved. This is thanks to your scriptableObject. they are assets that contain a certain data and therefore will subsist (they wont be deleted). For example, when you instanciate a prefab, after you stop the scene the clone is deleted. However your .prefab asset will always exist scince they are saved.

The only problem with custom classes is that you need to make them [Serializable][1] using the [System.Serializable]attribute on top of the class (you write it before starting the class). This is because Unity wont save all types of data, even in your scriptableObjects. Just primitive and some other ones, such as int, float, Vector2 and GameObject.

And how to spawn them... that's kind of weird but not difficult. You just need an empty object on your Game Scene with a levelGenerator.cs script. It would be something like this:

public class LevelGenerator : $$anonymous$$onoBehaviour {

 public LevelSaver levelSaver;
 int selectedLevel;

 public void Awake () {
     selectedLevel = Random.Range (0, levelSaver.allLevels.Count); //For lists
     selectedLevel = Random.Range (0, levelSaver.allLevels.Length); //For arrays
     //This is the level that will be spawned
     CreateLevel (selectedLevel);
 }

 void CreateLevel (int levelIndex) {
     Level currentLevel = levelSaver.allLevels [levelIndex];
     foreach (Block block in currentLevel.allBlocks) {
         GameObject b = new GameObject ();
         b.name = "Block " + allBlocks.IndexOf (block).ToString ();
         b.AddComponent<BoxCollider2D> ();
         b.GetComponent<BoxCollider2D> ().size = block.size;
         b.transform.position = block.position;
         b.tag = block.tag;
         b.layer = block.layer;
         //That will create a Block... you could
         //Also work this through prefabs, but i
         //dont know if its possible in your case
     }
     foreach (Player player in currentLevel.allPlayers) {
         //Same for player things
     }
         manger.current$$anonymous$$anager = currentLevel.level$$anonymous$$anager;
 }

} In the case of the manager, what I'd do is your add a $$anonymous$$anager variable to your level$$anonymous$$anager monobehaviour and set it from the levelGenerator. then all your variable should be set in the start ():

 public class Level$$anonymous$$anager : $$anonymous$$onobehaviour {
     //$$anonymous$$any variables
     public $$anonymous$$anager current$$anonymous$$anager;
 
     public Start () {
         gravity = current$$anonymous$$anager.gravitySpeed;
         //$$anonymous$$ore variable setting
     }
 }
avatar image Matt1000 Gablock · Mar 17, 2017 at 07:18 PM 0
Share

That is how you spawn each object. $$anonymous$$ind that you dont need alll data to create each object. And some data can be automatically set in the constructor of an object. What i mean:

 //Block constructor
 public Block (Vector2 _size, Vector2 pos, string textureName) { 
     size = _size;
     position = pos;
     tag = "Block"
     layer = "BlockLayer"
     Texture2D = Resources.Load<Textture2D> (textureName);
 }

Here the layer and tag are always the same so you dont need to set it all the time and the Texture is saved just by passing the name. Finally, i dind't understand your second question...

[1]: https://docs.unity3d.com/$$anonymous$$anual/script-Serialization.html

avatar image Gablock Matt1000 · Mar 17, 2017 at 09:15 PM 0
Share

Ok thx a lot :)

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

133 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Weird Unity 2D Graphical Issue 1 Answer

Camera Movement Question - How to follow player in Y axis only above certain threshold? 0 Answers

How to check if player is rapidly rotating the joystick? 2 Answers

Joystick freezes when new scene is loaded,Joystick freezes when loading a new scene 0 Answers

How to get the width of filled image ? 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