Editor Script - Access the User's Snap Settings?

Hey Guys,

I’m working on an Editor Scripted Tool that I want to be able to snap when you hold Ctrl, just like the standard Unity transform tools. However, I can’t seem to find any documentation as to how to access the Snap Settings the User has specified. Does this functionality exist?

Any assistance is greatly appreciated. Thanks in advance.

Anyone? I just need to know if what I’m trying to do is even possible or not…

Hey, I am also looking for this… Anyone knows about this?

I see unity recently added snap features: Unity - Scripting API: Snapping
But I still don’t know how to access the Editor’s “Grid And Snap” world grid size. (with Unity 2020).

6495883--730855--upload_2020-11-6_6-20-32.png

I found more information on this old page, but the “MoveSnap” value in the EditorPrefs is not up-to-date anymore. It’s always set to 1.

http://wiki.unity3d.com/index.php?title=SnapToGrid

    [MenuItem(HotkeysMenu + "/Transform/Grid Snap selection _g", false, HotkeysPriority)]
    static void MenuSnapToGrid() {
        foreach (Transform t in Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable)) {
            Undo.RegisterFullObjectHierarchyUndo(t, "GridSnapSelection");
            t.position = new Vector3 (
                Mathf.Round(t.position.x / EditorPrefs.GetFloat("MoveSnapX")) * EditorPrefs.GetFloat("MoveSnapX"),
                Mathf.Round(t.position.y / EditorPrefs.GetFloat("MoveSnapY")) * EditorPrefs.GetFloat("MoveSnapY"),
                Mathf.Round(t.position.z / EditorPrefs.GetFloat("MoveSnapZ")) * EditorPrefs.GetFloat("MoveSnapZ")
            );
        }
    }

Searched for this a bit, and this thread came up pretty high. Had to dig deeper to find the correct result, so for future googlers; the current way to get the snap settings is much cleaner:

EditorSnapSettings

2 Likes

@Baste Hey Baste, nice find.

In my case, It’s not the EditorSnapSettings.move that determines the snap of objects with the translate gizmos.
Instead it’s the World Grid’s size and I see this value is not accessible in the EditorSnapSettings.

6573466--745822--upload_2020-11-30_12-36-43.png

I’m talking about when you move objects with this grid snap option toggled on.

6573466--745825--upload_2020-11-30_12-37-51.png

So with these settings, objects you translate will snap every 1 unit, and not every 3 units as one might expect. What is EditorSnapSettings.move for?