How to create an empty gameobject with canvas from runtime generated child objects with sprites?

Hello, I am a student and trying different things with unity.
So, I have this procedurally generated shape:
62716-capture.jpg

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:
62717-capture2.jpg

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

Yes, it’s possible, but you’ll need to rewrite most of your code. SpriteRenderer is for a scene object. The canvas UGUI equivalent is the Image class. You’ll need to generate objects with RectTransform, CanvasRenderer, and Image components. Then you’ll need to parent them to the canvas and set their bounds and anchors through their RectTransform components. It takes a bit of time to wrap your head around how the UGUI works in code, but once you do, it’s nice and flexible.