Isometric Grid Snapping in Unity 2D
28 Feb 2016When developing Terrenae we found building levels for it slow and error prone, because Unity doesn't have any tools for editing in 2D isometric. In order to allow our designer to rapidly create and tweak levels, I developed a script to snap to 2D isometric coordinates at edit time. There are two parts to setting up an editor script for custom snapping in Unity, both of which I will cover below.
The first part is the snapping function itself. In order to convert between normal and isometric 2D grid coordinates, we need to know the tile dimensions as shown. These values are in scene units, and were obtained by dividing the dimensions of a tile by the pixels per unit of the tile sprite. As this is a shared value, it is recommended that all tiles are the same size in Units.
In the game code the world is represented as a simple 2D array of tiles. The above dimensions are used to calculate the skew to transform the 2D grid into an isometric grid.
With the tile size in units calculated, we can write a snapping function that snaps any game object to the isometric grid:
X and Y are calculated using the pre-calculated values. Z is simply a function of X and Y, primarily tied to the Y axis, with adjustments based on X to ensure that tiles are correctly rendered on top when they are closer to the camera. This snap function could be swapped out for a different one if you were working with, say; Hex grids instead of Iso grids.
Now that we've done the maths to figure out WHERE we should put our tile, we need to make the editor use this Snap function when a tile object is moved in the editor. I attached this functionality within the Grid script as it manages the grid and so was a convenient place to calculate and store the tile size in units, plus, there are relatively few Grid objects in the scene; this reduces the amount of code execution at design time. Firstly we need to include the Unity editor commands:
and enable our script to run during edit time:
In order to track changes made in the editor window we need to subscribe to the SceneView delegate. It is best to do this in OnEnable and OnDisable, as this guarantees it will run as long as the Grid is enabled (note the use of "#if UNITY_EDITOR" to ensure that this code is only run at design time - we don't want it to run when the game is being played:
The actual code to check if a tile has been moved is quite simple. We get a reference to the currently selected GameObject and check if the mouse has been released, if it has been, then snap the GameObject - obviously this results in false calls to Snap occasionally, but it is a simple check to make, and the cost of the Snap function is not that high.
There you have it, a simple solution to custom grid snapping in Unity. This method was developed during Global Game Jam, so it is undoubtably not as efficient as it could be. I will post an update if I find a better way to do this in the future. If you have any queries or corrections, hit me up on twitter! Complete source listing below: