- Home /
Question by
samim99 · Apr 18, 2018 at 03:28 PM ·
unity 5unity 2dgridgrid based game
Grid Letter Box is not expanding,
I Am trying to draw a grid then place my letterboxes on x and y positions of it. If I set the tileSize to 4 I can see a very small grid in middle of panel, if I make it any bigger I don't get any spaces between tiles and position gets messed up too - see picture.
tileSize = 4f:
here is tileSize=50f:
I have a panel and a grid.cs script that am attaching to the panel.
public class Grid : MonoBehaviour
{
[SerializeField]
private GameObject LetterBoxPrefab;
private GameObject[,] _grid;
private DataController _dataController;
private LevelData _currentLevelData;
void Start()
{
_dataController = FindObjectOfType<DataController>();
_currentLevelData = _dataController.GetCurrentLevelData();
_grid = new GameObject[_currentLevelData.GridRows, _currentLevelData.GridColumns];
CreateGrid();
}
private float tileSize = 50f;
Vector2 middleOffset = new Vector2();
Vector2 GetScreenPointFromLevelIndices(int row, int col)
{
//converting indices to position values, col determines x & row determine y
return new Vector2(col * tileSize - middleOffset.x, row * -tileSize + middleOffset.y);
}
private void CreateGrid()
{
for (int x = 0; x < _grid.GetLength(0); x++)
{
for (int y = 0; y < _grid.GetLength(1); y++)
{
GameObject letterBox = Instantiate(LetterBoxPrefab);
letterBox.name = "letter_box_" + x + "_" + y;
letterBox.transform.localScale = new Vector2(1, 1) * (tileSize - 1);//set tile size
letterBox.transform.position = GetScreenPointFromLevelIndices(x, y); //place in scene based on level indices
letterBox.transform.SetParent(transform, false);
}
}
}
}
,
unityissue.jpg
(38.2 kB)
unityissue-small.jpg
(36.7 kB)
Comment