Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3.  
  4. public class TileGrid : MonoBehaviour
  5. {
  6.     Dimensions dimensions;
  7.     GameObject[,,] grid;
  8.     float padding;
  9.  
  10.     public void Build(Dimensions dimensions, GameObject tilePrefab, float padding)
  11.     {
  12.         this.dimensions = dimensions;
  13.         grid = new GameObject[dimensions.x, dimensions.y, dimensions.z];
  14.  
  15.         for (int x = 0; x < grid.GetLength(0); x++)
  16.         {
  17.             for (int y = 0; y < grid.GetLength(1); y++)
  18.             {
  19.                 for (int z = 0; z < grid.GetLength(2); z++)
  20.                 {
  21.                     float xPos, yPos, zPos = 0f;
  22.                     xPos = transform.position.x + x * padding;
  23.                     yPos = transform.position.y + y * padding;
  24.                     zPos = transform.position.z + z * padding;
  25.                     Vector3 pos = new Vector3(xPos, yPos, zPos);
  26.  
  27.                     GameObject tileGO = Instantiate(tilePrefab, pos, Quaternion.identity, transform);
  28.                     grid[x, y, z] = tileGO;
  29.  
  30.                     Tile tile = tileGO.GetComponent<Tile>();
  31.                     tile.Initialize(new Dimensions(x, y, z));
  32.                 }
  33.             }
  34.         }
  35.  
  36.         Bounds bounds = new Bounds(transform.position, Vector3.zero);
  37.         foreach (Renderer r in GetComponentsInChildren<Renderer>())
  38.         {
  39.             bounds.Encapsulate(r.bounds);
  40.         }
  41.  
  42.         TranslateTilesByOffset(bounds.center);
  43.     }
  44.  
  45.     void TranslateTilesByOffset(Vector3 offset)
  46.     {
  47.         foreach (GameObject tileGO in grid)
  48.         {
  49.             tileGO.transform.position -= offset;
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement