Hello, I am a student and trying different things with unity.
So, I have this procedurally generated shape:
As you notice, it does not have a canvas or a pivot point.
Code for generating children for this gameObject:
public void buildShape() {
int count = 0;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (blocks [i, j] == 1) {
GameObject ogo = new GameObject ("square" + count);
ogo.transform.parent = transform; //set parent to main gameObject
ogo.AddComponent<SpriteRenderer> (); //add a sprite renderer to render sprites
ogo.tag = "squares"; //set tag to squares
SpriteRenderer otherSR = ogo.GetComponent<SpriteRenderer> (); //get the sprite renderer to a var
otherSR.sprite = Resources.Load<Sprite> ("Sprites/square"); //load the sprite to a gameobject
//set the position of a seperate square
ogo.transform.Translate ((float)otherSR.bounds.extents.x * 2 * i, (float)otherSR.bounds.extents.y * 2 * j, 0);
count++;
}
}
}
}
The thing is that the generation leaves quite a lot of space. The 2D array blocks has 1s and 0s to represent this shape so far left down corner of the gameobject is empty space.
What I want is to make all of these smaller gameobjects not to only have the same parent but to have a canvas and a pivot like this:
So, the question is if there is a way to combine all children elements with their separate sprites and colliders to a one gameobject and set a canvas on top (from WIDTH and HEIGHT of shape) with a pivot point in the centre?
Thanks in advance, Laurynas