Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Editor Script - Access the User's Snap Settings?

Discussion in 'Scripting' started by The MC, Aug 26, 2012.

  1. The MC

    The MC

    Joined:
    Oct 31, 2005
    Posts:
    105
    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.
     
  2. The MC

    The MC

    Joined:
    Oct 31, 2005
    Posts:
    105
    Anyone? I just need to know if what I'm trying to do is even possible or not...
     
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
  4. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    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

    Code (CSharp):
    1.     [MenuItem(HotkeysMenu + "/Transform/Grid Snap selection _g", false, HotkeysPriority)]
    2.     static void MenuSnapToGrid() {
    3.         foreach (Transform t in Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable)) {
    4.             Undo.RegisterFullObjectHierarchyUndo(t, "GridSnapSelection");
    5.             t.position = new Vector3 (
    6.                 Mathf.Round(t.position.x / EditorPrefs.GetFloat("MoveSnapX")) * EditorPrefs.GetFloat("MoveSnapX"),
    7.                 Mathf.Round(t.position.y / EditorPrefs.GetFloat("MoveSnapY")) * EditorPrefs.GetFloat("MoveSnapY"),
    8.                 Mathf.Round(t.position.z / EditorPrefs.GetFloat("MoveSnapZ")) * EditorPrefs.GetFloat("MoveSnapZ")
    9.             );
    10.         }
    11.     }
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    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
     
    halley and FeastSC2 like this.
  6. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    @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.

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

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




    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?