- Home /
In 2D. Is there a way you can set the size of an object to fit the grid perfectly?
Would have been great.
What kind of object you want to fit in the grid ? Are you talking about fitting in some image sprite into a grid ?
If you want to do it with a script you can do it like this.
Let say you have Grid and you want to add some sprite into it of some perfect size. First create an Image UI in your grid object. Assign the size to the UI images into your grid and then from the inspector change the image sprite to the sprite you want.
You can also do it by a script by adding all the sprites you want to add in the grid into a Resources folder and then you
public GameObject gridContainer; public GameObject limageSpritePrefab; void start() Sprite[] thumbnails = Resources.LoadAll<Sprite>("[Name Of The Folder Holding The Sprites In Resources Folder]"); foreach(Sprite thumbnail in thumbnails) { GameObject container = Instantiate(imageSpritePrefab) as GameObject; container.GetComponent<Image>().sprite = thumbnail; container.transform.SetParent(gridContainer.transform, false); } }
Answer by RadonRaph · Feb 10, 2019 at 12:37 PM
@JonasForsberg There is a free assets made by unity called proGrids its really useful.
Your answer
![](https://koobas.hobune.stream/wayback/20220612191438im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Change grid scale or scale whole library 0 Answers
How to get equal screen coordinates on different resolutions? 1 Answer
How to make 2d distortion? 2 Answers
Unity 2d Sprite size 1 Answer