### Author Topic: TGS : Scale sprite to fit a cell similar to Demo scene 21  (Read 778 times)

#### manofspirit

• Jr. Member
•  • Posts: 55 ##### TGS : Scale sprite to fit a cell similar to Demo scene 21
« on: November 30, 2019, 02:00:20 PM »
Hi there,

Noticed a new cool looking demo scene in 21 where you scaled down a Quad to fit Grid background, which reminds me of a problem i had last time i used TGS.

So can you please guide how to scale a spriterenderer to fit a "single cell" size?

Thanks

#### Kronnect

• Hero Member
•     • Posts: 5511 ##### Re: TGS : Scale sprite to fit a cell similar to Demo scene 21
« Reply #1 on: December 01, 2019, 09:06:54 AM »
Use this code:

Code
``            // Compute sprite scale            float tileSize = Vector3.Distance(tgs.CellGetVertexPosition(0, 0), tgs.CellGetVertexPosition(0, 3));            float spriteSize = Vector3.Distance(spriteRenderer.bounds.max, spriteRenderer.bounds.min);            float scale = tileSize / spriteSize;            Vector3 spriteScale = new Vector3(scale, scale, 1f);``

First, it computes the size of a tile (distance from two opposite points). In this example, I'm assuming you're using an hexagonal grid. For squared grids, use vertices indices 0 and 2.
Second, it computes the size of the sprite (diagonal from min and max corners of the sprite renderer).
Finally, the scale you need to assign to the sprite gameobject is tileSize / spriteSize;

#### manofspirit

• Jr. Member
•  • Posts: 55 ##### Re: TGS : Scale sprite to fit a cell similar to Demo scene 21
« Reply #2 on: December 01, 2019, 02:16:14 PM »
Use this code:

Code
``            // Compute sprite scale            float tileSize = Vector3.Distance(tgs.CellGetVertexPosition(0, 0), tgs.CellGetVertexPosition(0, 3));            float spriteSize = Vector3.Distance(spriteRenderer.bounds.max, spriteRenderer.bounds.min);            float scale = tileSize / spriteSize;            Vector3 spriteScale = new Vector3(scale, scale, 1f);``

First, it computes the size of a tile (distance from two opposite points). In this example, I'm assuming you're using an hexagonal grid. For squared grids, use vertices indices 0 and 2.
Second, it computes the size of the sprite (diagonal from min and max corners of the sprite renderer).
Finally, the scale you need to assign to the sprite gameobject is tileSize / spriteSize;

Thank you for the code snippet,

I'm using Rect Grid, so code will be like this?

Code
``//For squared grids, use vertices indices 0 and 2.float tileSize = Vector3.Distance(tgs.CellGetVertexPosition(0, 0), tgs.CellGetVertexPosition(0, 2));``

My math is not good so i myself can never ever come up with scaling code so i had a rough idea that your code will be like this but it came differently, so i'd appreciate if you clear that confusion too

I earlier assumed that you will use CellGetRect.size and then sprite bound.size and divde to get right scaling
but you are using CellGetVertex.

If you can explain why didnt you use CellGetRect that'd be really helpful for me.

PS: Reason you are on Top 2 best asset store developers is that your unmatched support, you go beyond to provide support and helping lots of folks to develop their dream.

#### Kronnect

• Hero Member
•     • Posts: 5511 ##### Re: TGS : Scale sprite to fit a cell similar to Demo scene 21
« Reply #3 on: December 01, 2019, 02:37:58 PM »
Hey, thanks.

You can use both approaches - they will produce approximately same ratios, especifically same results with a rect grid but a slight difference if you use an hexagonal grid.

For example:
Code
``            Bounds tileBounds = tgs.CellGetRectWorldSpace(0);            float tileSize = Vector3.Distance(tileBounds.min, tileBounds.max);            float spriteSize = Vector3.Distance(sprite.bounds.max, sprite.bounds.min);            float scale = tileSize / spriteSize;``

This code produces same results in the case of a rect/square grid. But if you use an hexagonal grid, the diagonal of the enclosing rect is a bit longer than the distance between two vertices of the hexagon hence the result is slightly different. In any case you can just multiply the final scale by an "adjusting" factor to match your preferences. For example, multiply it by 0.95 or 0.9 to add a small padding.

Regards