Created
May 18, 2018 02:37
-
-
Save msaroufim/5c90f69f259c167f7f005ce9bf256eef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Map : MonoBehaviour { | |
//grid specifics | |
[SerializeField] | |
private int rows; | |
[SerializeField] | |
private int cols; | |
[SerializeField] | |
private Vector2 gridSize; | |
[SerializeField] | |
private Vector2 gridOffset; | |
[SerializeField] | |
private Sprite cellSprite; | |
private Vector2 cellSize; | |
private Vector2 cellScale; | |
// Use this for initialization | |
void Start () { | |
InitCells(); | |
} | |
void InitCells() | |
{ | |
GameObject cellObject = new GameObject(); | |
cellObject.AddComponent<SpriteRenderer>().sprite = cellSprite; | |
cellSize = cellSprite.bounds.size; | |
Vector2 newCellSize = new Vector2(gridSize.x / (float) cols, gridSize.y / (float) rows); | |
cellScale.x = newCellSize.x / cellSize.x; | |
cellScale.y = newCellSize.y / cellSize.y; | |
cellSize = newCellSize; | |
cellObject.transform.localScale = new Vector2(cellScale.x, cellScale.y); | |
for (int row = 0; row < rows; row++) | |
{ | |
for(int col = 0; col < cols; cols++) | |
{ | |
Vector2 pos = new Vector2(col * cellSize.x + gridOffset.x + transform.position.x, row * cellSize.y + gridOffset.y + transform.position.y); | |
GameObject cO = Instantiate(cellObject, pos, Quaternion.identity) as GameObject; | |
cO.transform.SetParent(transform); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment