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
0
Question by Razputin · May 19, 2014 at 03:56 PM · buildrandomlevelsettings

How to save a level that was randomly generated, so it can be returned to later.

I have kind of an idea of what I want to do here, but I don't know how to implement it. Basically the over world of my game is created using this CubeGrid script.

 public class CubeGrid : MonoBehaviour {
     
     //these are now GameObjects so you can use Prefabs to define what they are
     public GameObject border;
     public GameObject grassLand;
     public GameObject house;
     public GameObject city;
     public GameObject graveyard;
     public GameObject desert;
     public GameObject tree;
     public GameObject wolf;
     public GameObject bandit;
     
     public int randNum = 0;
     public int xCubes;
     public int zCubes;
     public int desertCreated = 0;
 
 
     public bool fighting = false;
     public bool enterWolf = false;
     static bool spawned = false;
     void Start () 
     {
         if(spawned == false)
         {
             GenerateLandscapeAndObjects(xCubes,zCubes);
             GenerateBorder(xCubes,zCubes);
         }
         else
         {
             DestroyImmediate(gameObject);
         }
     }
     
     void Awake()
     {
 
     }
     
 
     private void GenerateLandscapeAndObjects(int xMax, int zMax) {
         
         //create a GameObject to contain the landscape and objects
         //to keep things neat in the Hierarchy tab if you decide
         //to start generating huge landscapes later on
         GameObject landscape = new GameObject("Landscape and Objects");
 
 
         for ( int z = 0; z < zMax; z++ )
         {
             for ( int x = 0; x < xMax; x++ )
             {
                 float rnd = Random.value;
                 if ( rnd < 0.01 )
                 {
                     DoInstantiate(city, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
                 }
         //        else if (rnd < 0.3 && desertCreated < 19 && z > 0)
             //    {
                 //    DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
                 //    x++;
                 //    DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);                
                 //    x++;
                 //    DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
                 //    x++;
                 //    DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
                 //    x++;
                 //    DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
                 //    x++;
                     //DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
                 //    x++;
                 //    DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
                     //x++;
                 //    DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);    
                 //    x++;
                 //    DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
                 //    x++;
                 //    DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
                     //x++;
                 //    DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
                 //    x++;
                 //    DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
                 //    x++;
                 //    DoInstantiate(desert, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
                 //    desertCreated++;
 
                 //}
                 else if (rnd < 0.07)
                 {
                     DoInstantiate(tree, new Vector3(x,0,z), Quaternion.identity,landscape.transform);
                 }
                 else if ( rnd < 0.15 )
                 {
                     DoInstantiate(house, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
                 }
                 else if (rnd < 0.18)
                 {
                     DoInstantiate(graveyard, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
                 }
                 else 
                 {
                     DoInstantiate(grassLand, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
 
 
                     randNum = Random.Range(0,50);
                     if(randNum <= 3){
                         DoInstantiate(wolf,new Vector3(x,0,z), Quaternion.identity,landscape.transform);
                     }
                     else if(randNum ==4){
                         DoInstantiate(bandit, new Vector3(x,0.1f,z), Quaternion.identity,landscape.transform);
                     }
                 }
             }
         }
     }
   
     
     private void GenerateBorder(int xMax, int zMax) {
         
         //create a GameObject to contain the border
         //to keep things neat in the Hierarchy tab if you decide
         //to start generating huge landscapes later on
         GameObject borderGameObject = new GameObject("Landscape and Objects");
         //create border blocks along x axes
         for ( int x = 0; x < xMax; x++ )
         {
             DoInstantiate(border, new Vector3(x,1,0), Quaternion.identity,borderGameObject.transform);
             DoInstantiate(border, new Vector3(x,1,zMax), Quaternion.identity,borderGameObject.transform);
         }
         
         //create border blocks along z axes
         for ( int z = 0; z < zMax; z++ )
         {
             DoInstantiate(border, new Vector3(0,1,z), Quaternion.identity,borderGameObject.transform);
             DoInstantiate(border, new Vector3(xMax,1,z), Quaternion.identity,borderGameObject.transform);
         }
 
     }
     
     //A helper function to let you organise GameObjects easily in 
     //the hierarchy by setting the transform.parent of the instantiated prefab.
     private void DoInstantiate(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent) {
         Transform temp = ((GameObject)Instantiate(prefab,position,rotation)).transform;
         temp.parent = parent;
     }
 }
 
 

I would like to save the results I get from this as their own level in the build settings. So the first time the game starts it would go down something like this

MapCreated = false; Loadlevel MapCreator MapCreated = true; Then save that map in the build settings(Which Idk how to do) as like OverWorld so that whenever I have to return to the overworld I could load the OverWorld level that was created. Currently I always return to the MapCreator level (Which means everytime I go back to the overworld its recreated randomly and is a brand new world).

Hopefully i've explained what i'm trying to do in a good enough way for you to understand.

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
2
Best Answer

Answer by robertbu · May 19, 2014 at 04:15 PM

What I would do is generate and save the 'seed' used to generate the level. Then when you go back, just use the seed for the random generator to reproduce the same level using your code above. To make this happen, I'd use .NET Random class rather than Unity's Random class. For .Net you have to create an instance of the class. Note if you add 'using System; you will get conflicts between the two classes, but you can get access doing:

  System.Random rand1 = new System.Random(seed);

The seed itself you can generate using Unity's Random.Range or by using DateTime. Once you have a level you want to save, use PlayerPrefs to save the seed and then reuse it the next time.

You'll need to rewrite you use of Random in the above code. Instead of Random.Range() for example, you would do:

 int r = rand1.Next(10, 60);
Comment
Add comment · Show 2 · 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 Razputin · May 19, 2014 at 04:57 PM 0
Share

I've haven't used seeds before so I'll probably have a hard time figuring this out. But i'm going to give it a try because that does seem like a good idea, thank you!

avatar image Razputin · May 19, 2014 at 04:59 PM 1
Share

Actually... that was rather easy... O.O I just got it working in like a second haha thank you so much!

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

20 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

Related Questions

How to save a scene that you randomly generated as a new level in your build settings? 1 Answer

How to save a scene that you randomly generated as a new level in your build settings? 1 Answer

Post-Build Custom Level Loading 2 Answers

Distribute terrain in zones 3 Answers

Dialog Box in Build Settings 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