OnDrawGizmos() is how I’m visualizing the layout in the editor without needing to enter play mode to check my code every time. _spawnStartPosition finds the correct corner of the grid based on the spawnOrigin setting. So that it spawns inside the grid instead of on the grid, I’m offsetting the x and z values by adding troopSize to them.

You’ll also notice both troopSize and columns/rows are being multiplied by troopPadding to ensure they properly scale up as the padding between units scales up.

Gizmos.DrawSphere is showing the position of _spawnStartPosition and Gizmos.DrawWireCube is showing the bounds of the squad.

Once you get into the for loop, I’m using a switch statement to determine how the troops fill the area based on the fill setting. I couldn’t just invert a value like I did for top/bottom because the fill is totally contingent on knowing when it’s gotten to the end of a row or column.

I took most of the inspiration for this structure from this tutorial on how to place objects in a grid which goes into more detail around the math behind it all than I care to go into here 😆 so shout out to Coding With Unity for that, but I’m really happy with how I was able to take that premise and turn it into something very flexible.

Now you just take that same code that is drawing gizmos on the grid, and drop it into a method to instantiate troop prefabs in the game! I’m eventually gonna swap this to use pooling/relocation, but for now it’s instantiating—you get the point though.