I’m new to Unity and am trying to make some small 2D games to get the hang of the editor. I’d like to know if there’s a way to toggle a helper grid on when in Scene mode so that I can position elements more easily on screen.
Also, if there’s a way to put guides/helper lines on so that I know where the edges of my scene/screen will be whilst working, that would be super useful!
Morning, I had a bit of code that would use screen gizmos, might help u a bit.
I just made it to show me lines in a grid based on how high and wide, also what size I wanted the grid. Helped me a bit a while ago to try and get a rough idea of 2d screen real estate.
Hope it gives u a starting point to look at.
using UnityEngine;
using System.Collections;
public class DrawGridLines : MonoBehaviour {
public float gridSpacingX = 0.5f;
public float gridSpacingY = 0.5f;
public int blocksWide =32;
public int blocksHigh = 24;
public Vector3 gridCentre;
Vector3 GetLowerLeftDrawPoint(int squaresWide , int squaresHigh ) // returns the starting point at the lower left point
{
float xPos =(float)( -((squaresWide * gridSpacingX)/2) + gridCentre.x);
float yPos = (float)(-((squaresHigh * gridSpacingY)/2) + gridCentre.y);
return new Vector3 (xPos, yPos, 0f);
}
void OnDrawGizmos ()
{
Gizmos.color = Color.grey;
Vector3 lowerLeftStartPoint = GetLowerLeftDrawPoint(blocksWide, blocksHigh);
float lineLength = blocksWide * gridSpacingX;
for( int xLine = 0 ; xLine <= blocksHigh ; xLine++)
{
Vector3 vFrom = lowerLeftStartPoint + new Vector3(0f,(float)(xLine * gridSpacingY), 0f);
Vector3 vTo = vFrom + new Vector3(lineLength, 0f, 0f);
Gizmos.DrawLine (vFrom, vTo);
}
// draw the vertical lines now
lineLength = blocksHigh * gridSpacingY;
for (int yLine = 0; yLine <= blocksWide ; yLine++)
{
Vector3 hFrom = lowerLeftStartPoint + new Vector3((float)(yLine * gridSpacingX), 0f, 0f);
Vector3 hTo = hFrom + new Vector3(0f, lineLength, 0f);
Gizmos.DrawLine (hFrom, hTo);
}
}
}
1 Like