From 06f1730d0aac7098d874e5f231c5bf2f353aa497 Mon Sep 17 00:00:00 2001 From: alelievr Date: Sat, 25 Nov 2017 18:12:08 +0100 Subject: [PATCH] added snap tool editor --- ExamplesScenes/BasicHandles.unity | 86 ++++++++++++++++++----- ExamplesScenes/Editor/SnapHandleEditor.cs | 76 +++++++++----------- 2 files changed, 101 insertions(+), 61 deletions(-) diff --git a/ExamplesScenes/BasicHandles.unity b/ExamplesScenes/BasicHandles.unity index 80538a6..407735a 100644 --- a/ExamplesScenes/BasicHandles.unity +++ b/ExamplesScenes/BasicHandles.unity @@ -187,7 +187,7 @@ Transform: m_PrefabInternal: {fileID: 0} m_GameObject: {fileID: 56076083} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -13.206, y: 1, z: -10} + m_LocalPosition: {x: -11.2, y: 1, z: -10} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} @@ -210,7 +210,6 @@ GameObject: m_Component: - component: {fileID: 68830448} - component: {fileID: 68830447} - - component: {fileID: 68830446} - component: {fileID: 68830445} m_Layer: 0 m_Name: Cube @@ -253,18 +252,6 @@ MeshRenderer: m_SortingLayerID: 0 m_SortingLayer: 0 m_SortingOrder: 0 ---- !u!65 &68830446 -BoxCollider: - m_ObjectHideFlags: 0 - m_PrefabParentObject: {fileID: 0} - m_PrefabInternal: {fileID: 0} - m_GameObject: {fileID: 68830444} - m_Material: {fileID: 0} - m_IsTrigger: 0 - m_Enabled: 1 - serializedVersion: 2 - m_Size: {x: 1, y: 1, z: 1} - m_Center: {x: 0, y: 0, z: 0} --- !u!33 &68830447 MeshFilter: m_ObjectHideFlags: 0 @@ -278,13 +265,13 @@ Transform: m_PrefabParentObject: {fileID: 0} m_PrefabInternal: {fileID: 0} m_GameObject: {fileID: 68830444} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 4.458, y: -0.013, z: -2.4448} + m_LocalRotation: {x: 0.38268343, y: 0, z: 0, w: 0.92387956} + m_LocalPosition: {x: 3.6786275, y: 0.009540021, z: -1.0655398} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} m_RootOrder: 2 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_LocalEulerAnglesHint: {x: 45, y: 0, z: 0} --- !u!1 &1248301270 GameObject: m_ObjectHideFlags: 0 @@ -313,3 +300,68 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1586994006 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 5 + m_Component: + - component: {fileID: 1586994008} + - component: {fileID: 1586994007} + m_Layer: 0 + m_Name: Directional light + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!108 &1586994007 +Light: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1586994006} + m_Enabled: 1 + serializedVersion: 8 + m_Type: 1 + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_Intensity: 1 + m_Range: 10 + m_SpotAngle: 30 + m_CookieSize: 10 + m_Shadows: + m_Type: 0 + m_Resolution: -1 + m_CustomResolution: -1 + m_Strength: 1 + m_Bias: 0.05 + m_NormalBias: 0.4 + m_NearPlane: 0.2 + m_Cookie: {fileID: 0} + m_DrawHalo: 0 + m_Flare: {fileID: 0} + m_RenderMode: 0 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_Lightmapping: 4 + m_AreaSize: {x: 1, y: 1} + m_BounceIntensity: 1 + m_ColorTemperature: 6570 + m_UseColorTemperature: 0 + m_ShadowRadius: 0 + m_ShadowAngle: 0 +--- !u!4 &1586994008 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1586994006} + m_LocalRotation: {x: 0.5045439, y: -0.23069301, z: 0.50454736, w: 0.6615499} + m_LocalPosition: {x: 13.78, y: -0.63, z: -9.9} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 3 + m_LocalEulerAnglesHint: {x: 64.205, y: 27.941002, z: 92.406006} diff --git a/ExamplesScenes/Editor/SnapHandleEditor.cs b/ExamplesScenes/Editor/SnapHandleEditor.cs index b435be7..c7e8a4e 100644 --- a/ExamplesScenes/Editor/SnapHandleEditor.cs +++ b/ExamplesScenes/Editor/SnapHandleEditor.cs @@ -4,68 +4,66 @@ using UnityEditor; using BetterHandles; -[CustomEditor(typeof(Transform), true)] -public class SnapHandleEditor : Editor +[InitializeOnLoad] +public class SnapHandleEditor { - const float snapHandleDistance = 3.5f; - const float snapHandleSize = .15f; - const float defaultSnapUnit = .1f; - const float shiftSnapUnit = .5f; - const float commandSnapUnit = .05f; + static float snapHandleDistance = 3.5f; + static float snapHandleSize = .15f; + static float shiftSnapUnit = .1f; + static float commandSnapUnit = .25f; - Mesh snapToolMesh; - Transform transform; - Material handleMat; - - Quaternion currentRotation; - Color currentColor; + static Mesh snapToolMesh; + static Transform transform; + static Material handleMat; - Editor defaultTransformEditor; + static Quaternion currentRotation; + static Color currentColor; - void OnEnable() + static SnapHandleEditor() { snapToolMesh = Resources.Load< Mesh >("snapToolHandle"); - Debug.Log("target: " + target + ", type: " + target.GetType()); - transform = target as Transform; + SceneView.onSceneGUIDelegate += OnSceneGUI; } - void OnSceneGUI() + static void OnSceneGUI(SceneView sv) { - // if (Tools.current != Tool.Move) - // return ; + if (Tools.current != Tool.Move) + return ; + + if (Selection.activeGameObject == null) + return ; + + transform = Selection.activeGameObject.transform; //x axis - DrawAxisHandle(Quaternion.Euler(0, 0, 90), transform.right, Handles.xAxisColor, 0); + DrawAxisHandle(Quaternion.Euler(0, 0, 90), transform.right, Handles.xAxisColor); //y axis - DrawAxisHandle(Quaternion.Euler(0, 0, 0), transform.up, Handles.yAxisColor, 1); + DrawAxisHandle(Quaternion.Euler(0, 0, 0), transform.up, Handles.yAxisColor); //z axis - DrawAxisHandle(Quaternion.Euler(90, 0, 0), transform.forward, Handles.zAxisColor, 2); + DrawAxisHandle(Quaternion.Euler(90, 0, 0), transform.forward, Handles.zAxisColor); } - void DrawAxisHandle(Quaternion rotation, Vector3 direction, Color color, int index) + static void DrawAxisHandle(Quaternion rotation, Vector3 direction, Color color) { var e = Event.current; - float snapUnit = (e.shift) ? shiftSnapUnit : (e.command) ? commandSnapUnit : defaultSnapUnit; + float snapUnit = 0; float size = HandleUtility.GetHandleSize(transform.position) * snapHandleSize; float dist = size * snapHandleDistance; + if (EditorGUI.actionKey) + snapUnit = (e.shift) ? shiftSnapUnit : commandSnapUnit; + currentRotation = rotation; currentColor = color; Vector3 addPos = direction * dist; - Vector3 newPosition = Handles.Slider(transform.position + addPos, direction, size, SnapHandleCap, 0) - addPos; - - if (newPosition != transform.position) - { - //handmade snapping selected axis - newPosition[index] = Mathf.Round(newPosition[index] / snapUnit) * snapUnit; + Vector3 newPosition = Handles.Slider(transform.position + addPos, direction, size, SnapHandleCap, snapUnit) - addPos; - transform.position = newPosition; - } + transform.position = newPosition; } - void SnapHandleCap(int controlId, Vector3 position, Quaternion rotation, float size, EventType eventType) + static void SnapHandleCap(int controlId, Vector3 position, Quaternion rotation, float size, EventType eventType) { if (eventType == EventType.Repaint) { @@ -84,14 +82,4 @@ void SnapHandleCap(int controlId, Vector3 position, Quaternion rotation, float s HandleUtility.AddControl(controlId, distance); } } - - public override void OnInspectorGUI() - { - if (defaultTransformEditor == null) - defaultTransformEditor = Editor.CreateEditor(transform); - - Debug.Log("deafultTransformEditor: " + defaultTransformEditor); - if (defaultTransformEditor.GetType() != typeof(SnapHandleEditor)) - defaultTransformEditor.OnInspectorGUI(); - } }