Skip to content

Instantly share code, notes, and snippets.

@msaroufim
Created May 18, 2018 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msaroufim/5c90f69f259c167f7f005ce9bf256eef to your computer and use it in GitHub Desktop.
Save msaroufim/5c90f69f259c167f7f005ce9bf256eef to your computer and use it in GitHub Desktop.
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