Render a 2D grid in game at a certain depth
First off: I'm new to Unity.
I am currently reviving a game I once made in XNA and C# which never made it past the first prototype stage. It's a 2D puzzle game where you have to place objects on a grid.
Now here's what I can't wrap my head around. In Unity there are obviously plenty of things you can do and use, but I can't find any simple solution as how to draw a grid.
It should look kinda like the editor's built-in grid with the difference that I want to highlight a cell when hovering over it.
I saw an example rendering with the GL-class which seems oddly low-level for something so simple, I tried the Line Renderer (Effects -> Line) which doesn't work at all for some reasons (blurred, gradient line, though it should be a solid line) as well as creating it in form of dynamically generated tiles (which seems to be the most work-heavy thing).
So what can you recommend or what would be an easy and fast way (since this will be a prototype obviously)?
Thanks!
Answer by streeetwalker · Apr 05, 2020 at 03:28 PM
Hi @SlickPhazer, You've run through the options to draw a grid - probably the simplest is to use are LineRenderers, but to highlight an arbitrary grid cell, and to make it simple, I believe you will have to have separate objects that form the grid.
I think one of sthe easiest route is going to be to create a square graphic, say 256x256, filled white and with an black or colored outline around the edge.
You'll create a sprite out of that when you import, or apply it as a material to a sprite, and save it as a prefab.
Then instance the prefab at the locations you want in a couple of nested loops to form the rows and columns of the grid.
From there it will be a relatively simple code to detect which sprite has been selected/hovered over using the mouse or touch - there are few different ways you can do it - and apply a different color to the sprite to highlight the cell.