Advertisement
Guest User

Untitled

a guest
Oct 18th, 2011
1,601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class WorldGameObject : MonoBehaviour {
  5.    
  6.     private WorldData world = new WorldData();                  // Holds all the data about our world
  7.    
  8.     public Transform chunkPrefab;                               // A template of a chunk
  9.     public Transform[,,] chunkGO;                               // A list of our chunks coords
  10.  
  11.     void Start () {
  12.         // So we start by initializing a grid for our chunks
  13.         // In our world data script we run a function that
  14.         // Creates a 3 dimensional array and sets the coords
  15.         // For each Chunk
  16.         world.InitChunkGrid();
  17.        
  18.         // After initializing our grid for our chunks
  19.         // We can now go ahead and instantiat our chunks
  20.         // Referencing their Transform
  21.         CreateChunks();
  22.     }
  23.    
  24.     private void CreateChunks()
  25.     {
  26.         // This will only contain the locations of our chunks
  27.         // The actually chunk data gets stored in our ChunkData script
  28.         chunkGO = new Transform[world.ChunksX,world.ChunksY,world.ChunksZ];
  29.        
  30.         // 3 for loops, x,y,z each increment will represent a chunk
  31.         // We only want to go as high as the amount of chunks in our
  32.         // World so we set the loop amount as ChunksX,ChunksY,ChunksZ
  33.         for (int x = 0; x < world.ChunksX; x++)
  34.         {
  35.             for (int y = 0; y < world.ChunksY; y++)
  36.             {
  37.                 for (int z = 0; z < world.ChunksZ; z++)
  38.                 {
  39.                     // We get the chunk from the array located at x,y,z
  40.                     // And store it as the current chunk we're working on
  41.                     ChunkData curChunk = world.chunks[x, y, z];
  42.                    
  43.                     // Here we create a Vector3 of where the chunk will be placed
  44.                     // the x position on our vector3 is the current chunk's x coord multiplied
  45.                     // By the chunk width for example if curChunk is 2 we multiply it by our ChunkWidth,
  46.                     // Which in this case would be 32 because our chunks are 32x32x128 meaning
  47.                     // the chunks x position would be 64, we don't need to worry about Y because
  48.                     // Our chunk height is 1, then we do the same on the Z axis
  49.                     Vector3 chunkInstPos = new Vector3(curChunk.X * world.ChunkWidth, 0, curChunk.Y * world.ChunkHeight);
  50.                    
  51.                     // Now that we have where to we want put our chunk lets create a chunk instance
  52.                     Transform chunkInst = Instantiate(chunkPrefab, chunkInstPos, Quaternion.identity) as Transform;
  53.                    
  54.                     // After creating an instance of a chunk lets set the parent to our World GameObject
  55.                     chunkInst.parent = transform;
  56.                    
  57.                     // Lets rename the chunk to something more managable
  58.                     // In our ChunkData script is a function called ToString
  59.                     // This basically returns a string of the chunks position
  60.                     // It will return something like Chunk ( 1,0,1 )
  61.                     chunkInst.name = curChunk.ToString();
  62.                    
  63.                     // Now our chunk exists lets make a reference of it's
  64.                     // Position by adding it to our array
  65.                     chunkGO[x, y, z] = chunkInst;
  66.                 }
  67.             }
  68.         }
  69.     }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement