Skip to content

Instantly share code, notes, and snippets.

@corytrese
Created November 23, 2019 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corytrese/5c9170640f854d219e15b12c01815962 to your computer and use it in GitHub Desktop.
Save corytrese/5c9170640f854d219e15b12c01815962 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
static class AddOnMenu {
[MenuItem("Edit/Snap to Grid %g")]
static void MenuSnapToGrid() {
// read-cache preferences found in (EDIT -> SNAP SETTINGS)
float snapX = EditorPrefs.GetFloat("MoveSnapX");
float snapY = EditorPrefs.GetFloat("MoveSnapY");
float snapZ = EditorPrefs.GetFloat("MoveSnapZ");
// iterate each selected, top-level & modified allowed
foreach (Transform transform in Selection.GetTransforms(SelectionMode.OnlyUserModifiable | SelectionMode.TopLevel)) {
// prepare undo configuration
EditorGUI.BeginChangeCheck();
// record previous state
Undo.RecordObject(transform, "Snap to Grid");
// adjust object
transform.position = new Vector3(Mathf.Round(transform.position.x / snapX) * snapX, Mathf.Round(transform.position.y / snapY) * snapY, Mathf.Round(transform.position.z / snapZ) * snapZ);
// commit undo change
EditorGUI.EndChangeCheck();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment