Skip to content

Commit

Permalink
trying to get the default TransformEditor to work with my custom editor
Browse files Browse the repository at this point in the history
  • Loading branch information
alelievr committed Nov 25, 2017
1 parent 6a2dc5f commit 9f963e5
Show file tree
Hide file tree
Showing 14 changed files with 381 additions and 128 deletions.
6 changes: 4 additions & 2 deletions Editor/HandlesMaterials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ public static class HandlesMaterials
{
public static Material vertexColor;
public static Material textured;
public static Material overlayColor;

static HandlesMaterials()
{
vertexColor = Resources.Load< Material >("vertexColorMaterial");
textured = Resources.Load< Material >("texturedMaterial");
vertexColor = Resources.Load< Material >("VertexColorMaterial");
textured = Resources.Load< Material >("TexturedMaterial");
overlayColor = Resources.Load< Material >("OverlayColorHandle");
}
}
}
4 changes: 2 additions & 2 deletions ExamplesScenes/BasicHandles.unity
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ MeshRenderer:
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 68830444}
m_Enabled: 0
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
Expand Down Expand Up @@ -279,7 +279,7 @@ Transform:
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 68830444}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalPosition: {x: 4.458, y: -0.013, z: -2.4448}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
Expand Down
85 changes: 82 additions & 3 deletions ExamplesScenes/Editor/SnapHandleEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,96 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using BetterHandles;

[CustomEditor(typeof(Transform))]
[CustomEditor(typeof(Transform), true)]
public class SnapHandleEditor : Editor
{
const float snapHandleDistance = 3.5f;
const float snapHandleSize = .15f;
const float defaultSnapUnit = .1f;
const float shiftSnapUnit = .5f;
const float commandSnapUnit = .05f;

Mesh snapToolMesh;
Transform transform;
Material handleMat;

Quaternion currentRotation;
Color currentColor;

Editor defaultTransformEditor;

void OnEnable()
{

snapToolMesh = Resources.Load< Mesh >("snapToolHandle");
Debug.Log("target: " + target + ", type: " + target.GetType());
transform = target as Transform;
}

void OnSceneGUI()
{
Handles.Label(Vector3.zero, "snap handle editro here", EditorStyles.whiteLabel);
// if (Tools.current != Tool.Move)
// return ;

//x axis
DrawAxisHandle(Quaternion.Euler(0, 0, 90), transform.right, Handles.xAxisColor, 0);
//y axis
DrawAxisHandle(Quaternion.Euler(0, 0, 0), transform.up, Handles.yAxisColor, 1);
//z axis
DrawAxisHandle(Quaternion.Euler(90, 0, 0), transform.forward, Handles.zAxisColor, 2);
}

void DrawAxisHandle(Quaternion rotation, Vector3 direction, Color color, int index)
{
var e = Event.current;

float snapUnit = (e.shift) ? shiftSnapUnit : (e.command) ? commandSnapUnit : defaultSnapUnit;
float size = HandleUtility.GetHandleSize(transform.position) * snapHandleSize;
float dist = size * snapHandleDistance;

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;

transform.position = newPosition;
}
}

void SnapHandleCap(int controlId, Vector3 position, Quaternion rotation, float size, EventType eventType)
{
if (eventType == EventType.Repaint)
{
//we set the material color or preselected color if mouse is near from our handle
Color color = (HandleUtility.nearestControl == controlId || GUIUtility.hotControl == controlId) ? Handles.preselectionColor : currentColor;
HandlesMaterials.overlayColor.SetColor("_Color", color);

//we draw the cylinder with overlay material
HandlesMaterials.overlayColor.SetPass(0);
Matrix4x4 trs = Matrix4x4.TRS(position, transform.rotation * currentRotation, Vector3.one * size);
Graphics.DrawMeshNow(snapToolMesh, trs);
}
else if (eventType == EventType.Layout)
{
float distance = HandleUtility.DistanceToCircle(position, size);
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();
}
}
2 changes: 1 addition & 1 deletion ExamplesScenes/curves/curve4.asset
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ MonoBehaviour:
m_Mode: 0
m_NumColorKeys: 2
m_NumAlphaKeys: 2
curveSize: {x: 5.27, y: 5.74}
curveSize: {x: 5.53, y: 5.74}
sampleCount: 500
76 changes: 76 additions & 0 deletions Resources/OverlayColorHandle.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: OverlayColorHandle
m_Shader: {fileID: 4800000, guid: 01e9f68f8b863406893a2dc8ee9a4340, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 0.227451, g: 0.4784314, b: 0.972549, a: 0.93}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
10 changes: 10 additions & 0 deletions Resources/OverlayColorHandle.mat.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Resources/snapToolHandle.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Blender MTL File: 'None'
# Material Count: 1

newmtl None
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
9 changes: 9 additions & 0 deletions Resources/snapToolHandle.mtl.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9f963e5

Please sign in to comment.