I followed this tutorial to form a table in Unity.
What I like to do is (1)I like to change the Cell's width x height dynamically in the code. (2)I like to set height of the first row of grids higher than the rest.
My code is
public class PopulateCellsToLeftPanel : MonoBehaviour {
public GameObject cell;
private int numberOfCells;
private float cellWidth;
private float cellHeight;
public PopulateCellsToLeftPanel(int num, float w, float h)
{
numberOfCells = num;
cellWidth = w;
cellHeight = h;
}
// Use this for initialization
void Start()
{
GameObject newObj; // Create GameObject instance
GridLayoutGroup grid = this.GetComponent<GridLayoutGroup>();
grid.cellSize = new Vector2(cellWidth, cellHeight);
int numRow = numberOfCells % 4;
for (int i = 0; i < numRow; i++)//row
{
for (int j = 0; j < 4; j++)//column
{
newObj = (GameObject)Instantiate(cell, transform);
if(i%2 == 0)
newObj.GetComponent<Image>().color = Color.white;
else
newObj.GetComponent<Image>().color = Color.red;
}
}
}
}
Then from the main class, I tried to set using the class's constructor.
public class Historycanvas : MonoBehaviour, IPointerClickHandler
{
PopulateCellsToLeftPanel pL;
pL = new PopulateCellsToLeftPanel(30, CellWidth, CellHeight);
}
But numberOfCells, cellWidth and cellHeight are always 0. Is that void Start()
is called earlier than class constructor?
Is it possible to set the height of first row of cells in the grid is different from the rest?
EDIT:
I did Debug.Log at both constructor function and Start function as below.
public PopulateCellsToLeftPanel(int num, float w, float h)
{
Debug.Log("i am from constructor start");
numberOfCells = num;
cellWidth = w;
cellHeight = h;
Debug.Log("i am from constructor end");
}
// Use this for initialization
void Start()
{
Debug.Log("i am from Start start");
GameObject newObj; // Create GameObject instance
GridLayoutGroup grid = this.GetComponent<GridLayoutGroup>();
grid.cellSize = new Vector2(cellWidth, cellHeight);
int numRow = numberOfCells % 4;
for (int i = 0; i < numRow; i++)//row
{
for (int j = 0; j < 4; j++)//column
{
newObj = (GameObject)Instantiate(cell, transform);
if(i%2 == 0)
newObj.GetComponent<Image>().color = Color.white;
else
newObj.GetComponent<Image>().color = Color.red;
}
}
Debug.Log("i am from Start end");
}
But I am surprised as Debug.log from Start() function was printed earlier than those from constructor(). Please look at in the following attached image. That is why my variables are always 0.
EDIT 2:
I tried to make a separate public function to instantiate image into grid as below, but images are not populated. Does it work only in Start()? Because if I put the same thing in Start() function, it works and the only issue with Start() is those parameters (int rows, float cellWidth, float cellHeight) I can't change dynamically. I need to change dynamically.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PopulateCellToLeftPanel : MonoBehaviour {
public GameObject cell;
// Use this for initialization
void Start()
{
}
public void Plot(int rows, float cellWidth, float cellHeight)
{
Debug.Log("cell width " + cellWidth + " cell height " + cellHeight + " num rows " + rows);
GameObject newObj; // Create GameObject instance
GridLayoutGroup grid = this.GetComponent<GridLayoutGroup>();
if (grid == null)
Debug.Log("grid is null");
grid.cellSize = new Vector2(cellWidth, cellHeight);
for (int i = 0; i < rows; i++)//row
{
for (int j = 0; j < 4; j++)//column
{
newObj = (GameObject)Instantiate(cell, transform);
if (i % 2 == 0)
newObj.GetComponent<Image>().color = Color.white;
else
newObj.GetComponent<Image>().color = Color.red;
}
}
}
}
Main Class
public class main: MonoBehaviour
{
PopulateCellToLeftPanel pL = null;
void Start()
{
pL = gameObject.AddComponent<PopulateCellToLeftPanel>();
pL.Plot(13, CellWidth, CellHeight);
}
}
My console output is as follow and grid is null.