Skip to content

Instantly share code, notes, and snippets.

@gazugafan
Created April 15, 2018 00:02
Show Gist options
  • Save gazugafan/2f781463c65b550ae8408711e0d87458 to your computer and use it in GitHub Desktop.
Save gazugafan/2f781463c65b550ae8408711e0d87458 to your computer and use it in GitHub Desktop.
Add to a Unity GameObject to easily see the center of a scene in the editor.
using UnityEngine;
using System.Collections;
// DrawGizmoGrid.cs
// draws a useful reference grid in the editor in Unity.
// 09/01/15 - Hayden Scott-Baron
// twitter.com/docky
// no attribution needed, but please tell me if you like it ^_^
//Forked by gazugafan, 4/14/2018...
//Added individual color options for center, major, and minor grid lines
//Added toggles for vertical and horizontal gridlines--allowing both to be shown at once
//More useful for seeing the exact center of the scene
public class DrawGizmoGrid:MonoBehaviour
{
// universal grid scale
public float gridScale = 1f;
// extents of the grid
public int minX = -15;
public int minY = -15;
public int maxX = 15;
public int maxY = 15;
// nudges the whole grid rel
public Vector3 gridOffset = Vector3.zero;
// choose a colour for the gizmos
public bool drawHorizontal = true;
public bool drawVertical = true;
public int gizmoMajorLines = 5;
public Color centerLineColor = new Color(1f, 0f, 0f, 0.5f);
public Color majorLineColor = new Color(1f, 0.5f, 0f, 0.2f);
public Color minorLineColor = new Color(1f, 0.5f, 0f, 0.08f);
// rename + centre the gameobject upon first time dragging the script into the editor.
void Reset()
{
if (name == "GameObject")
name = "Center Grid";
transform.position = Vector3.zero;
}
// draw the grid :)
void OnDrawGizmos()
{
// orient to the gameobject, so you can rotate the grid independently if desired
Gizmos.matrix = transform.localToWorldMatrix;
// draw the horizontal lines
for (int x = minX; x < maxX + 1; x++)
{
if (x != 0)
DrawXLine(x);
}
// draw the vertical lines
for (int y = minY; y < maxY + 1; y++)
{
if (y != 0)
DrawYLine(y);
}
DrawXLine(0);
DrawYLine(0);
}
private void DrawXLine(int x)
{
// find major lines
Gizmos.color = (x % gizmoMajorLines == 0 ? majorLineColor : minorLineColor);
if (x == 0)
Gizmos.color = centerLineColor;
Vector3 pos1 = new Vector3(x, minY, 0) * gridScale;
Vector3 pos2 = new Vector3(x, maxY, 0) * gridScale;
if (drawVertical)
Gizmos.DrawLine((gridOffset + pos1), (gridOffset + pos2));
pos1 = new Vector3(pos1.x, 0, pos1.y);
pos2 = new Vector3(pos2.x, 0, pos2.y);
if (drawHorizontal)
Gizmos.DrawLine((gridOffset + pos1), (gridOffset + pos2));
}
private void DrawYLine(int y)
{
// find major lines
Gizmos.color = (y % gizmoMajorLines == 0 ? majorLineColor : minorLineColor);
if (y == 0)
Gizmos.color = centerLineColor;
Vector3 pos1 = new Vector3(minX, y, 0) * gridScale;
Vector3 pos2 = new Vector3(maxX, y, 0) * gridScale;
if (drawVertical)
Gizmos.DrawLine((gridOffset + pos1), (gridOffset + pos2));
pos1 = new Vector3(pos1.x, 0, pos1.y);
pos2 = new Vector3(pos2.x, 0, pos2.y);
if (drawHorizontal)
Gizmos.DrawLine((gridOffset + pos1), (gridOffset + pos2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment