The following is used to create a masonry effect with varying height rectangles. How can I simplify and make this more elegant in Unity?
private float startX = 0.0f;
private float startY = 0.0f;
private float posX = 0.0f;
private float posY = 0.0f;
private float prevWidth = 0;
private float prevHeight = 0;
private int index = 0;
private int currentCol = 0;
private void AddCard(int index, int col, float width, float height)
{
GameObject goCard = (GameObject)GameObject.Instantiate(pfCard00);
PackedSprite psCard = goCard.GetComponentInChildren<PackedSprite>();
goCard.name = "QuestItemCard" + index.ToString();
goCard.transform.parent = goCardContainer.transform;
psCard.height = height;
if (currentCol != col)
{
posX += width;
posY = 0;
prevHeight = 0;
}
// placement
posY += (prevHeight / 2) + (height / 2);
goCard.transform.localPosition = new Vector3(startX + posX, startY - posY, goCard.transform.position.z);
if (currentCol != col)
{
prevWidth = width;
currentCol = col;
}
prevHeight = height;
}
Usage:
AddCard(0, 0, 100, 100);
AddCard(1, 0, 100, 100);
AddCard(2, 0, 100, 100);
AddCard(3, 0, 100, 200);
AddCard(4, 1, 100, 100);
AddCard(5, 1, 100, 100);
AddCard(6, 1, 100, 100);
AddCard(7, 1, 100, 200);