Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Jackv949 · Jul 13, 2015 at 06:21 AM · prefablightingloadinglevelefficiency

How can I store and load levels efficiently?

I am making a towers defence game, and I need to know how I can load levels that have much the same data. For example, every level will need a game manager, tower placement script, etc.

I have tried storing level-specific stuff (geometry, paths) as a prefab, and then loading that prefab into a pre-made scene when the game starts, and this works okay. The level is loaded and everything works fine, except for one thing. Lighting data isn't saved with the prefab. Everything in a level prefab is static, so the level ends up looking quite different in editor because unity has calculated lighting data, but when instantiated from a prefab none of the lighting data is present.

alt text

The difference is subtle in this example, but you can imagine the difference it would make in more complicated scenes.

Is there some other, better way of loading levels like this without sacrificing pre-calculated lighting and all that stuff?

level.png (129.0 kB)
Comment
Add comment · Show 7
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 bubzy · Jul 13, 2015 at 06:37 AM 0
Share

you could make a new scene for each level

avatar image Jackv949 · Jul 13, 2015 at 06:39 AM 0
Share

The issue is that I'm trying to avoid that most of all, since the only thing that would change between scenes would be the level itself, and each scene would have a load of prefabs that are required to run the game.

avatar image bubzy · Jul 13, 2015 at 06:42 AM 0
Share

then you could construct the level from an array, in a grid style, that would require you building a load of arrays (at least one for each level) beyond that im not entirely sure, havent dealt a lot with multiple levels :/

avatar image troien · Jul 13, 2015 at 08:30 AM 2
Share

Depending on your setup you might be able to make 1 scene that contains all the items that each level needs. Call DontDestroyOnLoad for these items and load this scene once on startup. Then place every level inside a seperate scene. Then you could simply load each level using Application.LoadLevel while not destroying the shared assets that every level needs.

Ins$$anonymous$$d of DontDestroyOnLoad you could also when loading a level: load the 1 scene using LoadLevel, followed by the level scene using LoadLevelAdditive ins$$anonymous$$d of LoadLevel. Although currently Unity hasen't really implemented this with Global Illu$$anonymous$$ation yet, see the roadmap. This is planned for Unity 5.2 Sept. 8, 2015 (Lighting: $$anonymous$$ulti Scene Lightmap Baking)

avatar image eclecticLoad · Jul 13, 2015 at 08:41 AM 1
Share

I wanted to suggest 1 scene with all the objects/scripts that you need in every level while loading the level specific things in LoadLevelAdditive too. It worked out quite well for me.

Show more comments

2 Replies

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

Answer by LRG · Jul 13, 2015 at 08:21 AM

It is actually possible (though not very intuitive) to calculate and use lightmaps for prefabs.

Basically, you have to take your prefab, place it in an empty scene with lights, and calculate the lightmap for it. Then, you need to copy the near and far lightmaps that you have generated somewhere else (otherwise they will be overwritten when you try to calculate the next lightmap).

Then, when you load your scene, you will need to manually apply the lightmap you have calculated to your prefab. You can do that by setting the lightmapIndex for the prefab's MeshRenderer:

 renderer = gameObject.GetComponent<MeshRenderer>();
 renderer.lightmapIndex = lightmapIndex;
 renderer.lightmapScaleOffset = scaleOffset;

This index refers to a list of available lightmaps that is stored in the LightmapSettings static class. You will also need to build this list manually. It has a lightmaps property that contains a LightmapData[] array. If you have two prefabs that you want to lightmap, for example, then you will need to build your list with those two, and assign the right index to them.

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 Jackv949 · Jul 13, 2015 at 08:30 AM 0
Share

Yeah, you're right. That isn't very intuitive xD

Thanks for your help. $$anonymous$$aybe I could just have levels in their own scene and load the game management prefabs ins$$anonymous$$d...

avatar image
0

Answer by michaelb212 · Jul 13, 2015 at 12:45 PM

You can define a simple c# class that describes your level content, serialize/desirialize it to JSON file format with Newtonsoft.json.JsonConvert and use File.ReadAllText to load/save it to disk

Somewhere in your code have a method that loads up an instance of your level Describer class and Instantiates all the GameObjects accordingly.

This requires quite a bit of engineering work, but will teach you a lot if you want to spend the time on it.

I would start by reading about JSON, serialization, NuGet and Newtonsoft.json It will probably take a few days of work for an absolute beginner.

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 Jackv949 · Jul 13, 2015 at 12:53 PM 0
Share

Thanks for your answer, I actually had considered this, but it presents the same lighting issue that I have with using prefabs.. :/

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

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

How do I include baked lighting in my prefabs? 5 Answers

Loading in game does not work properly 0 Answers

Proper way to use scenes 1 Answer

Loading back and forth between levels, creates clone of player 1 Answer

scene loader issue 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