AutoSnapToGrid give away

Maybe someone will find this useful since this function is not built in into Unity…

// Snap To Grid Editor Script
// How to use: Simply put this script in a Editor/ folder in your project.

using UnityEditor;
using UnityEngine;
using System;

[InitializeOnLoad]
public class SnapToGrid : Editor {

    public static bool autoSnap = false;
    public static float gridSize = 1f;
    private static Vector3 prevPosition;

    public static int SelectedTool
    {
        get
        {
            return EditorPrefs.GetInt("SelectedGridTool", 0);
        }

        set
        {
            if (value == SelectedTool)
                return;

            switch(value)
            {
                case 0:
                    EditorPrefs.SetInt("SelectedGridTool", 0);
                    autoSnap = false;
                    Tools.hidden = false;
                    break;
                case 1:
                    EditorPrefs.SetInt("SelectedGridTool", 1);
                    autoSnap = true;
                    Tools.hidden = false;
                    break;
            }
        }
    }

    static SnapToGrid()
    {
        SceneView.onSceneGUIDelegate -= OnSceneGUI;
        SceneView.onSceneGUIDelegate += OnSceneGUI;
    }

    void OnDestroy()
    {
        SceneView.onSceneGUIDelegate -= OnSceneGUI;
    }

    static void OnSceneGUI(SceneView sceneView)
    {
        DrawGridTool(sceneView.position);

        if(SnapToGrid.SelectedTool == 1)
        {
            if (Selection.transforms.Length > 0 && !EditorApplication.isPlaying)
                if (Selection.transforms[0].position != prevPosition)
                    Snap();
        }

    }

    static void DrawGridTool(Rect position)
    {
        Handles.BeginGUI();

        GUILayout.BeginArea(new Rect(0, 0, position.width, 20), EditorStyles.toolbar);
        {
            string[] buttonLabels = new string[] { "GridSnap OFF", "GridSnap ON" };

            SelectedTool = GUILayout.SelectionGrid(
                SelectedTool,
                buttonLabels,
                2,
                EditorStyles.toolbarButton,
                GUILayout.Width(200));
        }
        GUILayout.EndArea();

        GUILayout.BeginArea(new Rect(0, position.height - 35, 250, 20), EditorStyles.toolbar);
        {
            gridSize = EditorGUILayout.FloatField("Grid Size", gridSize);
        }

        GUILayout.EndArea();
        Handles.EndGUI();
        HandleUtility.Repaint();
    }

    static void Snap()
    {
        try
        {
            for(int i = 0; i < Selection.transforms.Length; i++)
            {
                Vector3 t = Selection.transforms[i].transform.position;
                t.x = round(t.x);
                t.y = round(t.y);
                t.z = round(t.z);
                Selection.transforms[i].transform.position = t;
            }
            prevPosition = Selection.transforms[0].position;
        }
        catch(Exception e)
        {
            Debug.LogError("Nothing to move.  " + e);
        }
    }

   static float round(float input)
    {
        float snappedValue = gridSize * Mathf.Round(input / gridSize);
        return (snappedValue);
    }
}

If someone could guide me how to draw a custom grid in the scene view which resizes according to gridSize in the script above I would be forever thankful

er… hold down ctrl whilst moving an object in the scene snaps the object to the grid, granularity of the snap is set in the options somewhere (can’t remember where off the top of my head)

I know… but I can guarantee that you will like this option alot more…

Oh sorry… This is Auto(!) Snap To Grid which you can toggle on/off right from the SceneView… without holding Ctrl…