How to make a 2D array of buttons?
Hello, i want to make a 5 x 5 grid with an array of buttons, so then i could sum rows and columns independently, i already have a 2d array on code, but i want to know how could i transform this in to a visible UI:
public int Row = 5;
public int Column = 5;
int sum = 0;
private int[,] numbers = new int [Row,Column];
void start (){
for (int x=0; x < Row; x++){
for(int y=0; y < Column; y++){
/* for example if (y == 1){
sum += grid [x,y]; } */
}
}
}
Comment
Little re$$anonymous$$der : start
function won't be called by Unity, Start
will.
Answer by Bilelmnasser · Oct 03, 2017 at 08:00 AM
Hi, use Grid Layout Group to design your grid add buttons Ui as childrens.
use a public gameobject type list in your script, use unity inspector to grab all your buttons inside the list.
public List<Button> ButtonsArrayList;
Answer by Positive7 · Oct 03, 2017 at 11:14 AM
var num = 1;
for (var x = 0; x < 5; x++)
{
for (var y = 0; y < 5; y++)
{
arrayInts[x, y] = num;
sum += arrayInts[x, y];
Debug.Log(sum);
num++;
}
}
To create an UI from script would be something like this :
private int sum;
private readonly int[,] arrayInts = new int[5, 5];
private void Awake()
{
CreateUi();
}
private void CreateUi()
{
var canvas = new GameObject("Canvas");
canvas.gameObject.AddComponent<RectTransform>();
canvas.gameObject.AddComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
canvas.gameObject.AddComponent<GraphicRaycaster>();
canvas.gameObject.AddComponent<CanvasScaler>();
var panel = new GameObject("Panel");
panel.gameObject.AddComponent<RectTransform>();
panel.gameObject.AddComponent<CanvasRenderer>();
panel.AddComponent<Image>();
panel.GetComponent<Image>().color = new Color(1, 1, 1, 0.2f);
panel.GetComponent<RectTransform>().SetParent(canvas.GetComponent<RectTransform>());
panel.gameObject.GetComponent<RectTransform>().offsetMin = Vector2.zero;
panel.gameObject.GetComponent<RectTransform>().offsetMax = Vector2.zero;
panel.gameObject.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0.5f);
panel.gameObject.GetComponent<RectTransform>().anchorMin = Vector2.zero;
panel.gameObject.GetComponent<RectTransform>().anchorMax = Vector2.one;
panel.gameObject.GetComponent<RectTransform>().sizeDelta = Vector2.zero;
panel.gameObject.AddComponent<GridLayoutGroup>();
panel.gameObject.GetComponent<GridLayoutGroup>().constraint = GridLayoutGroup.Constraint.FixedRowCount;
panel.gameObject.GetComponent<GridLayoutGroup>().constraintCount = 5;
panel.gameObject.GetComponent<GridLayoutGroup>().childAlignment = TextAnchor.MiddleCenter;
var button = new GameObject("Button");
button.gameObject.AddComponent<RectTransform>();
button.gameObject.AddComponent<CanvasRenderer>();
button.AddComponent<Image>();
button.gameObject.AddComponent<Button>();
var text = new GameObject("Text");
text.gameObject.AddComponent<RectTransform>();
text.gameObject.AddComponent<CanvasRenderer>();
text.gameObject.AddComponent<Text>();
text.gameObject.GetComponent<Text>().font = Font.CreateDynamicFontFromOSFont("Arial", 14);
text.gameObject.GetComponent<Text>().color = Color.grey;
text.gameObject.GetComponent<Text>().alignment = TextAnchor.MiddleCenter;
text.GetComponent<RectTransform>().SetParent(button.GetComponent<RectTransform>());
var num = 1;
for (var x = 0; x < 5; x++)
{
for (var y = 0; y < 5; y++)
{
var newButton = Instantiate(button);
newButton.GetComponentInChildren<Text>().text = num.ToString();
newButton.GetComponent<RectTransform>().SetParent(panel.GetComponent<RectTransform>());
var x1 = x;
var y1 = y;
newButton.GetComponent<Button>().onClick.AddListener(delegate { Sum(arrayInts[x1, y1]); });
arrayInts[x, y] = num;
num++;
}
}
}
private void Sum(int num)
{
sum += num;
Debug.Log(sum);
}
Of course you can create the UI elements in editor and assign it to the script :)