teleias

Grid Walls

Aug 6th, 2014
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. for(int x = 0; x < Size.x; x++){
  2.     for(int z = 0; z < Size.z; z++){
  3.         //We create a new Transform to manipulate later.
  4.         Transform newCell;
  5.         //A new CellPrefab is Instantiated at the correct location
  6.         // using "new Vector3(x,0,z)".
  7.         // Quaternion.Identity is a rotation that we don't need to
  8.         // worry about.
  9.         newCell = (Transform)Instantiate(CellPrefab, new Vector3(x, 0, z), Quaternion.identity);
  10.         //The newCell is now renamed "(x,0,z)" using String.Format
  11.         // "(+x+",0,"+z+")" would also work, but is less efficient.
  12.         newCell.name = string.Format("Cell@({0},0,{1})",x,z);
  13.         //For clean-ness, we parent all the newCells to the Grid gameObject.
  14.         newCell.parent = transform;
  15.         //We already set the position of the newCell, but the cell's attached
  16.         // script needs to know where it is also.
  17.         // We assign it here.
  18.         newCell.GetComponent<CellScript>().Position = new Vector3(x, 0, z);
  19.         //Grid[,] keeps track of all of the cells.
  20.         // We add the newCell to the appropriate location in the Grid array.
  21.         Grid[x,z] = newCell;
  22.     }
  23. }
  24.  
  25. //This is the part we will add.
  26. //You need to make another prefab (for the wall) object in the Unity Editor
  27. // Place it in a new global variable called WallPrefab (assigned in Unity Editor).
  28. for(int x = -1; x < Size.x+1; x++)
  29. {
  30.     for(int z = -1; z < Size.z+1; z++)
  31.     {
  32.         //We're actually only going to use x=-1 and x=Size.x+1 (or y=-1 and y=Size.y+1),
  33.         // because those are the far walls.
  34.         // Otherwise just continue;
  35.         // You can set this loop in other ways (this is a bit wasteful) but I think it looks neat.
  36.         if(x==-1||x==Size.x+1||z==-1||z==Size.z+1)
  37.         {
  38.             Transform newWall;
  39.             newWall = (Transform)Instantiate(WallPrefab, new Vector3(x, 0, z), Quaternion.identity);
  40.             newWall.name = string.Format("Wall@({0},0,{1})",x,z);
  41.             newWall.parent = transform;
  42.         }
  43.         else //if we're not looking at a boundary.
  44.         {
  45.             continue;
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment