[ExecuteInEditMode]
public class EditModeGridSnap : MonoBehaviour {
public float snapValue = 1;
public float depth = 0;
void Update() {
float snapInverse = 1/snapValue;
float x, y, z;
// if snapValue = .5, x = 1.45 -> snapInverse = 2 -> x*2 => 2.90 -> round 2.90 => 3 -> 3/2 => 1.5
// so 1.45 to nearest .5 is 1.5
x = Mathf.Round(transform.position.x * snapInverse)/snapInverse;
y = Mathf.Round(transform.position.y * snapInverse)/snapInverse;
z = depth; // depth from camera
transform.position = new Vector3(x, y, z);
}
}
You may need to adjust the script depending on if objects are odd or even dimension, because it changes the centers, i.e. a cube .75 wide needs different math than a cube 1 unit wide when snapping to the nearest .25 units.