x


Is there a toggle for snap to grid?

Currently the only way I know of to use Snap to Grid feature when moving objects is to hold down the "Command" (or "Control" for PC).

Is there anywhere in the editor you can toggle this option so you don't have to hold down a button all the time for object placement?

If there is not a toggle option some where, what would be the best way to go about adding this feature to the editor?

more ▼

asked Jul 26 '11 at 10:54 PM

spenick gravatar image

spenick
46 1 1 3

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

I scoured the docs, but it doesn't look like this is something configurable. Instead, I wrote this little editor script that does essentially the same thing. (Note- It will always round down, but this could be pretty easily fixed)

Place this script in a folder named "Editor", and hit CTRL-L or open Edit/AutoSnap to access the menu.

@script ExecuteInEditMode()

class autoSnap extends EditorWindow
{
    var prevPosition    : Vector3;
    var doSnap  : boolean = false;
    var snapValue: float = 1;
    @MenuItem("Edit/Auto Snap %_l")

    static function Init () 
    {
        var window : autoSnap = EditorWindow.GetWindow(autoSnap);
    }

    function OnGUI()
    {
        doSnap = EditorGUILayout.Toggle("Autosnap", doSnap);
        snapValue = EditorGUILayout.FloatField("Snap Value", snapValue);
    }

    function Update()
    {
        if(Selection.transforms.Length > 0 && !EditorApplication.isPlaying)
        if(doSnap && Selection.transforms[0].position != prevPosition)
            snap();
    }

    function snap()
    {
        try
        {
            for(i = 0; i < Selection.transforms.Length; i++)
            {               
                var t : Vector3 = 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(e)
        {
            // Nothing to move.
            Debug.LogError("Nothing to move.  " + e);
        }
    }

    function round(input : float)
    {
        var snappedValue : float;

        snappedValue = snapValue * Mathf.Round((input / snapValue));

        return(snappedValue);
    }
}
more ▼

answered Jul 27 '11 at 02:15 AM

karl_ gravatar image

karl_
2.4k 41 53 70

is this a C# script? i get this error:

NullReferenceException: Object reference not set to an instance of an object
UnityEditor.EditorWindow.GetWindow (System.Type t, Boolean utility, System.String title, Boolean focus)
UnityEditor.EditorWindow.GetWindow (System.Type t)
autoSnap.Init () (at /users/dc/Dropbox/travagent/un1/app1/Assets/Editor/GridSnap.js:12)

which is this line:

var window : autoSnap = EditorWindow.GetWindow(autoSnap);

window var is also flagged as not used...

Sep 06 '11 at 02:30 AM yatayata

This is written in JS

Sep 06 '11 at 02:53 AM karl_

Cool, great script, thanks!

Just a note for others, it seems that the window needs to stay open for the autosnapping to function. But you can just leave it in the background behind other windows.

Feb 28 '12 at 02:29 AM Essential

You can also dock it somewhere to keep it open and out of the way.

Apr 15 '12 at 09:14 PM mweldon
(comments are locked)
10|3000 characters needed characters left

Here is the C# version.

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 ) );
    }
}
more ▼

answered Apr 15 '12 at 09:13 PM

mweldon gravatar image

mweldon
252 1 5

Awesome job karl_ and mweldon. Above and beyond. Very generous use of your time.

Jul 18 '12 at 05:05 PM Lukas Barbarossa
(comments are locked)
10|3000 characters needed characters left

No, there is not a toggle for this. And I do not know the best way to add this.

more ▼

answered Jul 27 '11 at 12:22 AM

ConfinedDarkness gravatar image

ConfinedDarkness
81 3 3 4

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x268
x162
x56

asked: Jul 26 '11 at 10:54 PM

Seen: 7113 times

Last Updated: Jul 18 '12 at 05:05 PM