- Home /
Improving Grid Algorithm
HI
I am trying to instantiate a 4X4 grid of Ui buttons. these buttons are children of a Panel object. Is ther a cleaner way of instantiating and placing these buttons.
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
private GameObject[] button;
private GameObject panel;
private Vector2 gridSize;
void Start ()
{
gridSize = new Vector2 (4, 4);
panel = GameObject.Find ("PlayArea");
button = new GameObject[16];
for (int i = 0; i < button.Length; i++)
{
for (int x = 0; x < gridSize.x; x++)
{
for (int y = 0; y < gridSize.y; y++)
{
button[i] = (GameObject)Instantiate (Resources.Load ("Button"));
button[i].transform.SetParent (panel.transform, worldPositionStays:true);
button[i].transform.position += new Vector3 ((x * 60) + 150, (y * 80) + 40, 0);
}
}
}
}
}
Comment
Best Answer
Answer by Kk1496 · Apr 10, 2015 at 12:19 PM
I ended up using a Grid Layout Group Component and didn't need to place the buttons via script at all. I just needed to instant irate the buttons.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
UI Instantiate?,Ui, Sumonar UI? 0 Answers
Multiple Cars not working 1 Answer
Get the coordinate of a tap/click and generate an effect on UI 0 Answers