Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Block generation

Discussion in 'Scripting' started by Sam_Pr, Sep 14, 2014.

  1. Sam_Pr

    Sam_Pr

    Joined:
    Aug 23, 2012
    Posts:
    66
    So, I created a really simple script to generate a flat surface out of cubes.

    Code (CSharp):
    1. public class testBlock : MonoBehaviour {
    2.  
    3.  
    4.     public int zLength;
    5.     public int xLength;
    6.  
    7.     void Start () {
    8.  
    9.         int xLoc;
    10.         int zLoc;
    11.  
    12.         for(zLoc = 0; zLoc < zLength; zLoc++) {  
    13.  
    14.             for(xLoc = 0;xLoc < xLength;xLoc++) {
    15.  
    16.                 GameObject block = GameObject.CreatePrimitive (PrimitiveType.Cube);
    17.                 block.transform.position = new Vector3 (xLoc, 0, zLoc);
    18.                 print(zLoc+" "+xLoc);
    19.  
    20.             }
    21.  
    22.  
    23.         }
    24.  
    25.     }
    26. }
    Of course this does what it's supposed to, it generates a flat of cubes, however when I use larger numbers like 100x100 it lags really hard even though it's only 1 block thick and 100x100. While this may be expected as you're making 10,000 cubes all at once my question is how do games like Minecraft, and the Minecraft clones created in unity handle this? Because they have hundreds of thousands of blocks loaded at once as apposed to my 10,000. Any ideas on how to make this work better are much appreciated!
     
  2. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    They usually do two things, first they procedurally generate just a handful of cubes, about 16x16x16, referred to as a chunk, then each chunk is handled by a chunk manager, giving about 32x32x32 chunks (usually) that gives u about 512x512x512m space. The chunk managers job is to handle loading and discarding chunks that come in and out of view. That way the world can seem to be seamless, as for the data that produces the world/chunks, theres a few methods, saving a fixed size world to file (via some editor) which is looped to make it virtually circular/seamless/infinite. Another method is having the data procedurally generated by an algorithm such as perlin noise, with some rules as to how to place different types of blocks, as well as space (caves, e.g.), can have the effect of actually producing infinite space. Another method is if it's via server, the server sends data for each chunk as and when the client needs the data, and the server uses a similar method to above. Hope this helps. And this is just what I know, by far not the be all end all answer.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401