Can't seem to move a grouping of panels
I'm trying to create a grid of panels and found my problems are reversed: I was easily able to create a system that creates the grid within a defined space, but I can't seem to make it spawn in the space. Currently, no matter what variations I have tried (beyond hardcoding, which is something I'm trying to avoid), the value never hits anywhere near the field I'm trying to lay the grid on. Here's the function that generates everything:
public void GeneratePanel()
{
cursorVector = DMGGrid.GetComponent<RectTransform>().anchorMin;
int panelWidth = GetPanelSize(inFieldRec.width, columns);
int panelHeight = GetPanelSize(inFieldRec.height, rows);
for (int rowCount=1; rowCount <= rows; rowCount++)
{
for (int colCount=1; colCount <= columns; colCount++)
{
dmgPanel = Instantiate(panelPrefab, cursorVector, Quaternion.identity) as GameObject;
//Setting dmgPanelParams (i.e. creating the board)
dmgPanel.transform.SetParent(DMGGrid.transform); //set as parent for DMGCalcScript(LoadWeapon)
RectTransform newPanelRectT = dmgPanel.GetComponent<RectTransform>();
Rect newPanelRect = newPanelRectT.rect;
newPanelRect.width = panelWidth;
newPanelRect.height = panelHeight;
cursorVector.x += panelWidth;
}
cursorVector.x = DMGGrid.GetComponent<RectTransform>().anchorMin.x;
cursorVector.y += panelHeight;
}
}
DMGGrid is a public gameobject/panel that the sort of backdrop for the input field. Any help would be greatly appreciated!
Answer by Caestar · Nov 11, 2016 at 03:56 PM
So, got this working and posting what I have for others who might have the same problem/thoughts, variable names changed so that it's clearer to general circumstances than just my application. And my comments because I don't want to edit those out
public GameObject gridGO; //contains 2D Game Object to determine grid size & GO denoting "Game Object" in code
public GameObject panelPrefabGO;
private _panelGO;
public void GeneratePanel()
{
ZeroCheck(); //ensure zero isn't used to divide by (i.e. 0 columns/0 rows)
cursorVector = gridGO.GetComponent<RectTransform>().rect.min; //starting spawn point
float panelWidth = GetPanelSize(columns);
float panelHeight = GetPanelSize(rows);
Vector2 tileSizeV = new Vector2(panelWidth, panelHeight);
RectTransform newPanelRectT;
float cursorYChange = 0f;
for (int rowCount=1; rowCount <= rows; rowCount++)
{
for (int colCount=1; colCount <= columns; colCount++)
{
_panelGO = Instantiate(panelPrefabGO, new Vector2(), Quaternion.identity) as GameObject; //standard instatiate
_panelGO.transform.SetParent(gridGO.transform); //set as parent for separate script
#region Setting attributes
newPanelRectT = _panelGO.GetComponent<RectTransform>();
newPanelRectT.localPosition = cursorVector; //move spawned tile to cursor position relative to parent
newPanelRectT.sizeDelta = tileSizeV; //Size of tiles
newPanelRectT.localScale = tileSizeV; //<- Needed to make the tiles lie edge to edge (kinda stumbled onto this one)
#endregion
cursorVector.x += newPanelRectT.rect.size.x*tileSizeV.x; //move cursor from left to right via tilesize
cursorYChange = newPanelRectT.rect.size.y*tileSizeV.y; //set the cursorYChange (probably a better place to do this so it doesn't keep resetting, not sure where though)
//The reason for the equations is that the tile spawns at it's center point, so this moves it over so edges seem to be touching
}
cursorVector.x = gridGO.GetComponent<RectTransform>().rect.min.x; //reset cursor to left side of field
cursorVector.y += cursorYChange; //move by the cursorYchange factor
}
}
private float GetPanelSize(float num) //num being columns/rows
{
float dimensionF = 1 / num; //Here caused me a lot of frustration, but what it boils down to is 1 is needed so it's the entire length of the side (using the actual measurements of the distance causes headaches vs using the side as it's whole, regardless of measurement)
return dimensionF;
}
Your answer
Follow this Question
Related Questions
i try make enemy health slider or any uI just show the health of Enemy Over head 0 Answers
Script doesn't let me disable a second panel and I cant find a way around this. 0 Answers
NullReferenceException on Text element 1 Answer
How Can i Get This bool to work? Just need someone willing to explain. 2 Answers