- Home /
Draw grid lines in game view?
What is the best way to show grid lines in game view.. I want to show grid to the user in my game.. I was wondering what is the most convenient way?
Since I have mouse selection on many objects, I do not want that the grid cause any problem with the selection..
You could try a custom shader on your objects. I am not sure if there is a way to show the scene grid. You may have to manually create one.
I'm looking for a way to generate this grid at a specific point.
I'd like to know If I can specify where this grid is generated. I would like this grid to move with the camera. So I would like to generate the grid in a position specified by a child gameobject of the camera.
So when the camera moves the grid moves relative to the camera.
Any Ideas?
Any help would be greatly appreciated.
Look at the three properties startX, startY, startZ. They specify the origin of the grid. You should be able to tie these values to the transform.position of some other game object by adding a reference to it.
Hello guys,
How do I make this grid draw behind my game objects? Currently, they are drawn over my game elements.
Thanks!
Answer by codeguyross1 · Aug 30, 2016 at 07:46 PM
I had the need to be able to move the grid after the original placement so I added a little code to @Em3rgency's script to allow me to move the grid around. This code also includes the small addition to prevent the infinite loops while modifying small Step and Large Step variables.
using UnityEngine;
using System.Collections;
public class gridOverlay : MonoBehaviour {
public GameObject plane;
public bool showMain = true;
public bool showSub = false;
public int gridSizeX;
public int gridSizeY;
public int gridSizeZ;
public float smallStep;
public float largeStep;
public float startX;
public float startY;
public float startZ;
public float transformX;
public float transformY;
public float transformZ;
private float offsetY = 0;
private float scrollRate = 0.1f;
private float lastScroll = 0f;
private Material lineMaterial;
private Color mainColor = new Color(0f,1f,0f,1f);
private Color subColor = new Color(0f,0.5f,0f,1f);
void Start ()
{
}
void Update ()
{
if(lastScroll + scrollRate < Time.time)
{
if(Input.GetKey(KeyCode.KeypadPlus))
{
plane.transform.position = new Vector3(plane.transform.position.x, plane.transform.position.y + smallStep, plane.transform.position.z);
offsetY += smallStep;
lastScroll = Time.time;
}
if(Input.GetKey(KeyCode.KeypadMinus))
{
plane.transform.position = new Vector3(plane.transform.position.x, plane.transform.position.y - smallStep, plane.transform.position.z);
offsetY -= smallStep;
lastScroll = Time.time;
}
}
}
void CreateLineMaterial()
{
if( !lineMaterial ) {
lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
"SubShader { Pass { " +
" Blend SrcAlpha OneMinusSrcAlpha " +
" ZWrite Off Cull Off Fog { Mode Off } " +
" BindChannels {" +
" Bind \"vertex\", vertex Bind \"color\", color }" +
"} } }" );
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;}
}
void OnPostRender()
{
CreateLineMaterial();
// set the current material
lineMaterial.SetPass( 0 );
GL.Begin( GL.LINES );
if (smallStep <= 0)
smallStep = 1;
if (largeStep <= 0)
largeStep = 1;
if(showSub)
{
GL.Color(subColor);
//Layers
for(float j = 0; j <= gridSizeY; j += smallStep)
{
//X axis lines
for(float i = 0; i <= gridSizeZ; i += smallStep)
{
GL.Vertex3( startX + transformX, j + offsetY + transformY, startZ + i + transformZ);
GL.Vertex3( gridSizeX + transformX, j + offsetY + transformY, startZ + i + transformZ);
}
//Z axis lines
for(float i = 0; i <= gridSizeX; i += smallStep)
{
GL.Vertex3( startX + i + transformX, j + offsetY + transformY, startZ + transformZ);
GL.Vertex3( startX + i + transformX, j + offsetY + transformY, gridSizeZ + transformZ);
}
}
//Y axis lines
for(float i = 0; i <= gridSizeZ; i += smallStep)
{
for(float k = 0; k <= gridSizeX; k += smallStep)
{
GL.Vertex3( startX + k + transformX, startY + offsetY + transformY, startZ + i + transformZ);
GL.Vertex3( startX + k + transformX, gridSizeY + offsetY + transformY, startZ + i + transformZ);
}
}
}
if(showMain)
{
GL.Color(mainColor);
//Layers
for(float j = 0; j <= gridSizeY; j += largeStep)
{
//X axis lines
for(float i = 0; i <= gridSizeZ; i += largeStep)
{
GL.Vertex3( startX + transformX, j + offsetY + transformY, startZ + i + transformZ);
GL.Vertex3( gridSizeX + transformX, j + offsetY + transformY, startZ + i + transformZ);
}
//Z axis lines
for(float i = 0; i <= gridSizeX; i += largeStep)
{
GL.Vertex3( startX + i + transformX, j + offsetY + transformY, startZ + transformZ);
GL.Vertex3( startX + i + transformX, j + offsetY + transformY, gridSizeZ + transformZ);
}
}
//Y axis lines
for(float i = 0; i <= gridSizeZ; i += largeStep)
{
for(float k = 0; k <= gridSizeX; k += largeStep)
{
GL.Vertex3( startX + k + transformX, startY + offsetY + transformY, startZ + i + transformZ);
GL.Vertex3( startX + k + transformX, gridSizeY + offsetY + transformY, startZ + i + transformZ);
}
}
}
GL.End();
}
}
Answer by aloata · Oct 24, 2016 at 07:01 PM
in my case, my unity freeze when i pressed play button; i suspect because the code does not handle for floating points? (i used the same parameter as mentioned below)
idk but i modified the code, here is what i used,worked fine with floating points(i use parameter gridsize 29x18 with 0.46 small step and 0.92 large step). I added offset on X axis aswell.
using UnityEngine;
using System.Collections;
public class ShowGrid : MonoBehaviour {
public bool showMain = true;
public bool showSub = false;
public float gridSizeX;
public float gridSizeY;
public float smallStep;
public float largeStep;
public float startX;
public float startY;
public float offsetY;
public float offsetX;
private Material lineMaterial;
private Color mainColor = new Color(0f, 1f, 0f, 1f);
private Color subColor = new Color(0f, 0.5f, 0f, 1f);
void Start()
{
gridSizeX *= smallStep;
gridSizeY *= smallStep;
}
void Update()
{
}
void CreateLineMaterial()
{
if (!lineMaterial)
{
lineMaterial = new Material("Shader \"Lines/Colored Blended\" {" +
"SubShader { Pass { " +
" Blend SrcAlpha OneMinusSrcAlpha " +
" ZWrite Off Cull Off Fog { Mode Off } " +
" BindChannels {" +
" Bind \"vertex\", vertex Bind \"color\", color }" +
"} } }");
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
}
}
void OnPostRender()
{
CreateLineMaterial();
// set the current material
lineMaterial.SetPass(0);
GL.Begin(GL.LINES);
if (showSub)
{
GL.Color(subColor);
//Layers
for (float j = 0; j <= gridSizeY; j += smallStep)
{
//X axis lines
GL.Vertex3(startX + offsetX, j + offsetY, 0);
GL.Vertex3(gridSizeX + offsetX, j + offsetY, 0);
}
//Y axis lines
for (float k = 0; k <= gridSizeX; k += smallStep)
{
GL.Vertex3(startX + k + offsetX, startY + offsetY, 0);
GL.Vertex3(startX + k + offsetX, gridSizeY + offsetY, 0);
}
}
if (showMain)
{
GL.Color(mainColor);
//Layers
for (float j = 0; j <= gridSizeY; j += largeStep)
{
//X axis lines
GL.Vertex3(startX + offsetX, j + offsetY, 0);
GL.Vertex3(gridSizeX + offsetX, j + offsetY, 0);
}
//Y axis lines
for (float k = 0; k <= gridSizeX; k += largeStep)
{
GL.Vertex3(startX + k + offsetX, startY + offsetY, 0);
GL.Vertex3(startX + k + offsetX, gridSizeY + offsetY, 0);
}
}
GL.End();
}
}
hope this helps
Answer by JPhilipp · Jul 19 at 08:14 PM
I had some specific needs for our app, so with help from the other answers posted above, composed below in case anyone benefits from it. Some context: I wanted one white and one contrasting black "shadow" line for each line, so as to make it visible on any background; the script is added and destroyed to the camera when a setting is toggled, so there's no additional doShow bool or such; the center of the grid follows the camera as one (vr)-teleports around, rounding to 1 meter for the grid size; there is a small indicator at half a meter for each line; lines fade out a bit towards the edge.

using UnityEngine;
public class GridLines : MonoBehaviour {
const float stretch = 0.495f;
const float max = 2f;
const float step = 1f;
Material lineMaterial;
bool[] trueFalse = new bool[] {true, false};
void Start() {
var shader = Shader.Find("Hidden/Internal-Colored");
lineMaterial = new Material(shader);
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
lineMaterial.SetInt( "_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha );
lineMaterial.SetInt( "_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha );
lineMaterial.SetInt( "_Cull", (int)UnityEngine.Rendering.CullMode.Off );
lineMaterial.SetInt("_ZWrite", 0);
}
void OnPostRender() {
Vector3 start = new Vector3(
Mathf.Round(transform.position.x),
Mathf.Round(transform.position.y),
Mathf.Round(transform.position.z)
);
lineMaterial.SetPass(0);
GL.Begin(GL.LINES);
foreach (bool isWhite in trueFalse) {
if (!isWhite) {
const float offset = 0.0015f;
start = new Vector3(start.x + offset, start.y - offset, start.z + offset);
}
foreach (bool drawEdge in trueFalse) {
float alpha = drawEdge ? 0.1f : 0.75f;
float tone = isWhite ? 1f : 0f;
GL.Color( new Color(tone, tone, tone, alpha) );
for (float x = -max; x <= max; x += step) {
for (float y = -max; y <= max; y += step) {
for (float z = -max; z <= max; z += step) {
bool isEdge = Mathf.Abs(x) == max || Mathf.Abs(y) == max || Mathf.Abs(z) == max;
if ( (isEdge && drawEdge) || (!isEdge && !drawEdge) ) {
GL.Vertex3(start.x + x - stretch, start.y + y, start.z + z);
GL.Vertex3(start.x + x + stretch, start.y + y, start.z + z);
GL.Vertex3(start.x + x, start.y + y - stretch, start.z + z);
GL.Vertex3(start.x + x, start.y + y + stretch, start.z + z);
GL.Vertex3(start.x + x, start.y + y, start.z + z - stretch);
GL.Vertex3(start.x + x, start.y + y, start.z + z + stretch);
}
}
}
}
}
}
GL.End();
}
}
And if someone needs to snap placed objects at half meter points:
Vector3 SnapToGrid(GameObject thing) {
Vector3 point = thing.transform.position;
const float factorA = 2f;
const float factorB = 0.5f;
point.x = Mathf.Round(point.x * factorA) * factorB;
point.y = Mathf.Round(point.y * factorA) * factorB;
point.z = Mathf.Round(point.z * factorA) * factorB;
return point;
}
Your answer
Welcome to Unity Answers
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Follow this Question
Related Questions
Cloud recognition in Vuforia 0 Answers
I need a grid to help me model and design things. 4 Answers
How to create a 14 by 12 grid, depending on screen size? 1 Answer
Why can't I view my grid? 1 Answer
Animation Question with PlayMode 1 Answer