• 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 Dunlain · Feb 21, 2013 at 02:47 PM · terrainsceneprocedural generation

Is it possible to create scenes at runtime with the Unity engine?

My friend and I are working on a game and wish to create a game with procedurally generated landscape (terrain, buildings, flora and fauna). Assuming we created all the Assets in advance and simply gave them to a script to place them along with the randomly generated terrain is this possible with any version of Unity? Also the major part of my question is can this be done while the game is being executed, sort of Minecraft style world generation.

Comment
zeh
fhmiv
sbuzonas

People who like this

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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by zeh · Feb 21, 2013 at 04:48 PM

Sort of. You wouldn't necessarily be creating scenes, but running the same scene with scripts that create your procedural content as needed. You would probably be mostly instantiating Prefabs at runtime and creating meshes dynamically. You would still need a few objects (camera, scripts, etc) placed on your scene but it would otherwise be an empty scene where everything would be instantiated while the game runs. This could be done at the Awake() state of your scripts but of course also be re-ran at a later time to replace the current content of the scene.

Comment
numberkruncher
fhmiv

People who like this

2 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 Dunlain · Feb 21, 2013 at 05:40 PM 0
Share

Hmm, thanks! I have a ways to go before I get to that point (still working on terrain generation). Those links were pretty useful!

avatar image numberkruncher · Feb 21, 2013 at 05:51 PM 0
Share

I would avoid instantiating prefabs directly. I would use a pooling system (like the fantastic PoolManager) to improve performance by recycling previously instantiated prefabs where possible.

Sometimes it is useful to reload the empty scene to clear everything.

avatar image zeh · Feb 21, 2013 at 06:07 PM 0
Share

@Dunlain: cool. Just to be clear, for terrain generation you should use the Mesh class (my link above on dynamic mesh generation has some great resources on the topic). I've recently implemented this myself and it's quite simple once you get the hang of it. If you want natural but random terrain, you'll probably need some perlin noise to create the height of your vertices correctly too.

avatar image

Answer by sbuzonas · Sep 19, 2017 at 03:00 PM

You can use SceneManagement.SceneManager.CreateScene.

 using UnityEngine.SceneManagement;
 using UnityEngine;
 
 public class SceneFactory : MonoBehaviour {
 
     Scene _scene = SceneManager.CreateScene("Procedural Scene");
 
 }

Comment
Bunny83

People who like this

1 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 Jacebook · Mar 13, 2018 at 09:13 AM 0
Share

Is this "Scene _scene = ..." method available, if the name of the scene were to be in a variable, or does it have to be absolutely declared like shown above? I'm creating a game that has multiple levels that can use the same scene, but it would need a new title, so I would have the scene name created like the following:

 string Level = "Level";
 int LevelNum = 1;
 
 string CurLevel = Level + int.ToString;
 
 Scene _scene = SceneManager.CreateScene(CurLevel);

or something similar to that (I'm new to C#, so my syntax needs correcting!). Would this work?

avatar image Ayelis Jacebook · Mar 27, 2018 at 06:16 PM 0
Share

I'm sure you meant to write "LevelNum.ToString()" but yes, this code works. void Start () { string Level = "Level"; for(int LevelNum = 1; LevelNum<=10; LevelNum++){ string CurLevel = Level + LevelNum.ToString(); Scene _scene = SceneManager.CreateScene(CurLevel); } } //creates ten levels

avatar image JAY_DIZZEL · May 01, 2021 at 11:15 PM 0
Share

so far i do know this too but how can you save the created scene in runtime?

avatar image gigos22 JAY_DIZZEL · May 02, 2021 at 05:27 AM 0
Share

You can save a template of the scene as a binary file.

Then each time you are creating a scene an run time you can use this template to load things back in.

*By template I mean a regular class (not monobehaviour), with properties that can be serialized.

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

17 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

Related Questions

Instantiate prefabs next to each other? 1 Answer

Movement in my terrain ? 2 Answers

Extracting GameObject from scene package 1 Answer

Terrain Stitching 0 Answers

Terrain is entirely black in editor 5 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