Responsive Grid Layout in Unity

if you have worked with the grid layout, you should already know it gets pretty ugly in different screens. in the example below, you can see what it does to UI:

the settings to see image below

now change the screen:

the moment you see that white border…

but usually, you want your UI elements fit to left and right margin of grid layout. the only acceptable free space is for the lower side. to achieve this, I wrote a code that calculates the number of rows and the size of the UI elements, according to the aspect ratio of UI elements and the number of elements. this is basically how it works:

download the code here, add it to the Grid Layout gameobject, fill the inspector fields and enjoy!