Unity: Show Grid Coordinates Scene View


I’ve been working with the FFTWindow analysing audio input and needed a quick way to view the coordinate space of the grid. I’m surprised there is not a feature in Unity to do this (at least not that I could find). This is a quick script to help with debugging coordinates in the Scene View.

Grid Line Coordinates on the XY plane

The script basically just draws a set of Axis in the scene and labels the x,y,z coordinates. It’s best when you are viewing along the alignment of an axis (I use the XYZ Gizmo in the corner to switch between them).

In 3D view with Cyan and Magenta sample lines.

You can toggle the Z plane on and off to make it easier to see just the XY plane and there is a few sample lines to show how it might be useful.

I was using to analyse audio spectrum data on the fly like this:

The blue Logarithmic representation of the PCM signal in red

Code

Here is the code for the grid lines/coordinates below:

It’s also up on GitHub: https://github.com/zuluonezero/UnityGrid

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class GridLines : MonoBehaviour
{
	public int X_Axis_Length = 10;
	public int Y_Axis_Length = 10;
	public bool Z_Plane = true;
	public int font_Size;
	public bool show_Test = true;
	
	private GUIStyle guiStyle = new GUIStyle(); //create a new variable
	public void OnDrawGizmos()
	{
		// Axis
		Gizmos.color = Color.red; // X
		Gizmos.DrawLine(new Vector3((X_Axis_Length / -1), 0, 0), new Vector3((X_Axis_Length), 0, 0));
		Gizmos.color = Color.green; // Y
		Gizmos.DrawLine(new Vector3(0, (Y_Axis_Length / -1), 0), new Vector3(0, (Y_Axis_Length), 0));
		Gizmos.color = Color.blue; // Y
		Gizmos.DrawLine(new Vector3(0, 0, (X_Axis_Length / -1)), new Vector3(0, 0, (X_Axis_Length)));  // assumes Z Axis is the same length as the X


		//Gizmos.DrawWireSphere(centre, Radius);
		guiStyle.fontSize = font_Size;
		int x = -X_Axis_Length;
		int y = -Y_Axis_Length;	
	
		while (x < X_Axis_Length)
		{

			Handles.Label(new Vector3(x, y, 0), (x + "," + y));
			
			while ( y < Y_Axis_Length)
			{
				Handles.Label(new Vector3(x, y, 0), (x + "," + y));
				y++;
			}
			 x++;
			 y = -Y_Axis_Length;
		}
		
		if (Z_Plane)
		{
			int z = -X_Axis_Length;	// Z Axis
			y = -Y_Axis_Length;	
	
			while (z < X_Axis_Length)  // Z Axis
			{

				Handles.Label(new Vector3(0, y, z), (z + "," + y));
				
				while ( y < Y_Axis_Length)
				{
					Handles.Label(new Vector3(0, y, z), (z + "," + y));
					y++;
				}
				z++;
				y = -Y_Axis_Length;
			}
		}
		
		if (show_Test)
		{
		// Test lines
		Gizmos.color = Color.cyan; 
		Gizmos.DrawLine(Vector3.zero, new Vector3(4, 5, 0));
		Gizmos.DrawWireSphere(Vector3.zero, 0.2f);
		Gizmos.DrawCube(new Vector3(4, 5, 0), new Vector3(0.2f, 0.2f, 0.2f));
		
		Gizmos.color = Color.magenta; 
		Gizmos.DrawLine(new Vector3(-3, 3, -3), new Vector3(2, 2, 2)); 
		Gizmos.DrawWireSphere(new Vector3(-3, 3, -3), 0.2f);
		Gizmos.DrawCube(new Vector3(2, 2, 2), new Vector3(0.2f, 0.2f, 0.2f));			
		}
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *