Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- public class TileGrid : MonoBehaviour
- {
- Dimensions dimensions;
- GameObject[,,] grid;
- float padding;
- public void Build(Dimensions dimensions, GameObject tilePrefab, float padding)
- {
- this.dimensions = dimensions;
- grid = new GameObject[dimensions.x, dimensions.y, dimensions.z];
- for (int x = 0; x < grid.GetLength(0); x++)
- {
- for (int y = 0; y < grid.GetLength(1); y++)
- {
- for (int z = 0; z < grid.GetLength(2); z++)
- {
- float xPos, yPos, zPos = 0f;
- xPos = transform.position.x + x * padding;
- yPos = transform.position.y + y * padding;
- zPos = transform.position.z + z * padding;
- Vector3 pos = new Vector3(xPos, yPos, zPos);
- GameObject tileGO = Instantiate(tilePrefab, pos, Quaternion.identity, transform);
- grid[x, y, z] = tileGO;
- Tile tile = tileGO.GetComponent<Tile>();
- tile.Initialize(new Dimensions(x, y, z));
- }
- }
- }
- Bounds bounds = new Bounds(transform.position, Vector3.zero);
- foreach (Renderer r in GetComponentsInChildren<Renderer>())
- {
- bounds.Encapsulate(r.bounds);
- }
- TranslateTilesByOffset(bounds.center);
- }
- void TranslateTilesByOffset(Vector3 offset)
- {
- foreach (GameObject tileGO in grid)
- {
- tileGO.transform.position -= offset;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement