# Question instantiate prefab into grid position (always square)

Discussion in 'UGUI & TextMesh Pro' started by VincentAmbulant, Aug 23, 2023.

1. ### VincentAmbulant

Joined:
Aug 22, 2023
Posts:
2
Hi,

I'm looking to instantiate a prefab button, in multiple instances so it can cover the entire canvas/screen.
I have a Grid Layout group on my canvas.
But I don't want to guess the number of button needed to make a perfect square. I wish to have a script that instantiate the good amount of button so the size of each one is approximately 1.5" in real life, no matter the screen's size in use.

Here's what I got. (simply :
for (int i = 0; i < numberOfObjects; i++)
{

canvasGO = this.gameObject;
myCanvas = canvasGO.GetComponent<Canvas>();

Instantiate(prefabButt, new Vector3(0, 0, 0), Quaternion.identity,myCanvas.transform);
}
)

And I would wish something that look like that

I can calculate the size needed with the screen.witdh/height. But i'm have issue to make them spawn in a square formation.

Really hope it makes sense somehow....
Thanks a lot
Vince

File size:
60 KB
Views:
30
2. ### wideeyenow_unity

Joined:
Oct 7, 2020
Posts:
728
I'd love to see how you figure that one out, that sounds like a ton of math depending on the monitors actual screen size(versus application screen size) and resolution.. So I'd probably let that idea go.

But as you can see in your second picture, you'd need to set the Constraint Size, and what ever your horizontal count is would just be multiplied by it self.
``5 across? = 5 x 5 total``

In your first picture you show Constraint Size as Flexible, so I would assume once your done getting the screen display size, and calculating the grid display unit to what you're comfortable with being "1.5 inches", you just divide that by screen display width and round it. Then take that number, multiply it by itself, and Instantiate that many.

I would suggest reading further into the documentation:
https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/script-GridLayoutGroup.html

As maybe it says something I'm unaware of, that can basically do some of that math for you. Content Size Fitter being one them.

3. ### VincentAmbulant

Joined:
Aug 22, 2023
Posts:
2
I did go the Constraint size way. meaning if I want 100 object, It will do 10x10 (square root of 100) also added a, rounded off so 101 will also do a 10x10.
Now... Is there any way to change the cell size in script ? It always trow me a error, that says somethings like, can't change grid cell sizez 'cause not a variable.
Here, as a exemple, I have a 10x10 button layout. Is there any way to keep it in the camera view ( If i can't change the grid cell size. IDK what should I scale ? )

thanks again

4. ### wideeyenow_unity

Joined:
Oct 7, 2020
Posts:
728
if you're "screen" is more width than height in that case, only calculate from height. Just in that last picture ^ I can see both boxes being cut in half, so it should be able to fit a whole(7x7).

Other than that, if you want a set number of boxes, then scale your Grid Layout Group to fit height wise.