A little window for modifying Grid Snapping: Grid Size and Increment Snapping: Move quickly and easily.
Last active
May 6, 2022 12:12
-
-
Save yeonghoey/8d1eb266e22dd76a0db9a749d0f09388 to your computer and use it in GitHub Desktop.
GridAndSnapWindow.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
using UnityEngine; | |
using UnityEditor; | |
public class GridAndSnapWindow : EditorWindow | |
{ | |
private static readonly float[] valuePresets = new float[] { 1f, .5f, .25f, .1f, .05f, .01f }; | |
private PropertyInfo propInfoGridSize; | |
private PropertyInfo propInfoSnapMove; | |
[MenuItem("Window/Custom/GridAndSnap")] | |
static void OpenWindow() | |
{ | |
var window = (GridAndSnapWindow)EditorWindow.GetWindow(typeof(GridAndSnapWindow)); | |
window.Show(); | |
} | |
void OnGUI() | |
{ | |
Init(); | |
EditorGUILayout.BeginVertical(); | |
EditorGUILayout.BeginHorizontal(); | |
EditorGUIUtility.labelWidth = 16f; | |
EditorGUILayout.BeginVertical(); | |
AddGridSizeField(); | |
EditorGUILayout.Space(); | |
AddGridSizePresetButtons(); | |
EditorGUILayout.EndVertical(); | |
EditorGUILayout.Space(); | |
EditorGUILayout.BeginVertical(); | |
AddSnapMoveField(); | |
EditorGUILayout.Space(); | |
AddSnapMovePresetButtons(); | |
EditorGUILayout.EndVertical(); | |
EditorGUIUtility.labelWidth = 0f; | |
EditorGUILayout.EndHorizontal(); | |
EditorGUILayout.EndVertical(); | |
} | |
private void Init() | |
{ | |
Assembly assembly = Assembly.Load("UnityEditor.dll"); | |
// https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/SceneView/SceneViewGrid.cs#L11 | |
Type gridSettings = assembly.GetType("UnityEditor.GridSettings"); | |
this.propInfoGridSize = gridSettings.GetProperty("size"); | |
// https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/Grids/EditorSnap.cs#L47 | |
Type editorSnapSettings = assembly.GetType("UnityEditor.EditorSnapSettings"); | |
this.propInfoSnapMove = editorSnapSettings.GetProperty("move"); | |
} | |
private void AddGridSizeField() | |
{ | |
float gsOld = gridSize.x; | |
float gsNew = EditorGUILayout.FloatField("G: ", gsOld); | |
if (gsNew != gsOld) | |
{ | |
gridSize = new Vector3(gsNew, gsNew, gsNew); | |
} | |
} | |
private void AddSnapMoveField() | |
{ | |
float smOld = snapMove.x; | |
float smNew = EditorGUILayout.FloatField("S: ", smOld); | |
if (smNew != smOld) | |
{ | |
snapMove = new Vector3(smNew, smNew, smNew); | |
} | |
} | |
private void AddGridSizePresetButtons() | |
{ | |
AddPresetButtons(valuePresets, (v) => gridSize = new Vector3(v, v, v)); | |
} | |
private void AddSnapMovePresetButtons() | |
{ | |
AddPresetButtons(valuePresets, (v) => snapMove = new Vector3(v, v, v)); | |
} | |
private void AddPresetButtons(float[] valuePresets, Action<float> onPressed) | |
{ | |
EditorGUILayout.BeginHorizontal(); | |
for (int i = 0; i < valuePresets.Length; i++) | |
{ | |
if (i % 2 == 0) | |
{ | |
EditorGUILayout.EndHorizontal(); | |
EditorGUILayout.BeginHorizontal(); | |
} | |
float v = valuePresets[i]; | |
if (GUILayout.Button($"{v:0.00}")) | |
{ | |
onPressed(v); | |
} | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
private Vector3 gridSize | |
{ | |
get => (Vector3)propInfoGridSize.GetValue(null); | |
set => propInfoGridSize.SetValue(null, value); | |
} | |
private Vector3 snapMove | |
{ | |
get => (Vector3)propInfoSnapMove.GetValue(null); | |
set => propInfoSnapMove.SetValue(null, value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment