Skip to content

Instantly share code, notes, and snippets.

@estebanpadilla
Last active April 10, 2021 21:27
Show Gist options
  • Save estebanpadilla/6089862 to your computer and use it in GitHub Desktop.
Save estebanpadilla/6089862 to your computer and use it in GitHub Desktop.
Auto snap to grid in Unity If you need to auto snap a gameObject to the grid in unity this script will help you. Usage: Create a Editor folder in your assets folder. Add the AutoSnap.cs script to the Editor folder. To open press Command + L (Mal), Control + L (PC) Source: http://answers.unity3d.com/questions/148812/is-there-a-toggle-for-snap-to-…
using UnityEngine;
using UnityEditor;
public class AutoSnap : EditorWindow
{
private Vector3 prevPosition;
private bool doSnap = true;
private float snapValue = 1;
[MenuItem( "Edit/Auto Snap %_l" )]
static void Init()
{
var window = (AutoSnap)EditorWindow.GetWindow( typeof( AutoSnap ) );
window.maxSize = new Vector2( 200, 100 );
}
public void OnGUI()
{
doSnap = EditorGUILayout.Toggle( "Auto Snap", doSnap );
snapValue = EditorGUILayout.FloatField( "Snap Value", snapValue );
}
public void Update()
{
if ( doSnap
&& !EditorApplication.isPlaying
&& Selection.transforms.Length > 0
&& Selection.transforms[0].position != prevPosition )
{
Snap();
prevPosition = Selection.transforms[0].position;
}
}
private void Snap()
{
foreach ( var transform in Selection.transforms )
{
var t = transform.transform.position;
t.x = Round( t.x );
t.y = Round( t.y );
t.z = Round( t.z );
transform.transform.position = t;
}
}
private float Round( float input )
{
return snapValue * Mathf.Round( ( input / snapValue ) );
}
}
@bobmoff
Copy link

bobmoff commented Mar 12, 2016

Replace the Snap method with this to add support for RecTransforms (UI)

private void Snap()
    {
        foreach ( var transform in Selection.transforms )
        {
            RectTransform rectTrans = transform.transform as RectTransform;
            if (rectTrans) {
                var pos = rectTrans.anchoredPosition;
                pos.x = Round(pos.x);
                pos.y = Round(pos.y);
                rectTrans.anchoredPosition = pos;
                var size = rectTrans.sizeDelta;
                size.x = Round(size.x);
                size.y = Round(size.y);
                rectTrans.sizeDelta = size;
            }
            else {
                var t = transform.transform.position;
                t.x = Round( t.x );
                t.y = Round( t.y );
                t.z = Round( t.z );
                transform.transform.position = t;
            }
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment