Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for(int x = 0; x < Size.x; x++){
- for(int z = 0; z < Size.z; z++){
- //We create a new Transform to manipulate later.
- Transform newCell;
- //A new CellPrefab is Instantiated at the correct location
- // using "new Vector3(x,0,z)".
- // Quaternion.Identity is a rotation that we don't need to
- // worry about.
- newCell = (Transform)Instantiate(CellPrefab, new Vector3(x, 0, z), Quaternion.identity);
- //The newCell is now renamed "(x,0,z)" using String.Format
- // "(+x+",0,"+z+")" would also work, but is less efficient.
- newCell.name = string.Format("Cell@({0},0,{1})",x,z);
- //For clean-ness, we parent all the newCells to the Grid gameObject.
- newCell.parent = transform;
- //We already set the position of the newCell, but the cell's attached
- // script needs to know where it is also.
- // We assign it here.
- newCell.GetComponent<CellScript>().Position = new Vector3(x, 0, z);
- //Grid[,] keeps track of all of the cells.
- // We add the newCell to the appropriate location in the Grid array.
- Grid[x,z] = newCell;
- }
- }
- //This is the part we will add.
- //You need to make another prefab (for the wall) object in the Unity Editor
- // Place it in a new global variable called WallPrefab (assigned in Unity Editor).
- for(int x = -1; x < Size.x+1; x++)
- {
- for(int z = -1; z < Size.z+1; z++)
- {
- //We're actually only going to use x=-1 and x=Size.x+1 (or y=-1 and y=Size.y+1),
- // because those are the far walls.
- // Otherwise just continue;
- // You can set this loop in other ways (this is a bit wasteful) but I think it looks neat.
- if(x==-1||x==Size.x+1||z==-1||z==Size.z+1)
- {
- Transform newWall;
- newWall = (Transform)Instantiate(WallPrefab, new Vector3(x, 0, z), Quaternion.identity);
- newWall.name = string.Format("Wall@({0},0,{1})",x,z);
- newWall.parent = transform;
- }
- else //if we're not looking at a boundary.
- {
- continue;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment