Search Unity

Helper grid or scene bounds in Unity 2D

Discussion in 'Getting Started' started by BlueWoozle, Mar 16, 2015.

  1. BlueWoozle

    BlueWoozle

    Joined:
    Mar 16, 2015
    Posts:
    8
    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!
     
  2. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    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.

    Code (CSharp):
    1.    using UnityEngine;
    2.    using System.Collections;
    3.  
    4.    public class DrawGridLines  : MonoBehaviour {
    5.    
    6.       public float gridSpacingX = 0.5f;
    7.       public float gridSpacingY = 0.5f;
    8.    
    9.       public int blocksWide =32;
    10.       public int blocksHigh = 24;
    11.    
    12.       public Vector3 gridCentre;
    13.    
    14.    
    15.       Vector3 GetLowerLeftDrawPoint(int squaresWide , int squaresHigh ) // returns the starting point at the lower left point
    16.       {
    17.          float xPos =(float)( -((squaresWide * gridSpacingX)/2) + gridCentre.x);
    18.          float yPos = (float)(-((squaresHigh * gridSpacingY)/2) + gridCentre.y);
    19.          return new Vector3 (xPos, yPos, 0f);
    20.       }
    21.    
    22.    
    23.       void OnDrawGizmos ()
    24.       {
    25.          Gizmos.color = Color.grey;
    26.          Vector3 lowerLeftStartPoint = GetLowerLeftDrawPoint(blocksWide, blocksHigh);
    27.        
    28.          float lineLength = blocksWide * gridSpacingX;
    29.          for( int xLine = 0 ; xLine <= blocksHigh ; xLine++)
    30.          {
    31.             Vector3 vFrom =  lowerLeftStartPoint + new Vector3(0f,(float)(xLine * gridSpacingY), 0f);
    32.             Vector3 vTo = vFrom + new Vector3(lineLength, 0f, 0f);
    33.             Gizmos.DrawLine (vFrom, vTo);
    34.          }
    35.        
    36.          // draw the vertical lines now
    37.          lineLength = blocksHigh * gridSpacingY;
    38.          for (int yLine = 0; yLine <= blocksWide ; yLine++)
    39.          {
    40.             Vector3 hFrom = lowerLeftStartPoint + new Vector3((float)(yLine * gridSpacingX), 0f, 0f);
    41.             Vector3 hTo = hFrom + new Vector3(0f, lineLength, 0f);
    42.             Gizmos.DrawLine (hFrom, hTo);
    43.          }
    44.        
    45.        
    46.       }
    47.    }
     
    theANMATOR2b likes this.