Skip to content

Instantly share code, notes, and snippets.

@tylearymf
Last active October 16, 2020 03:00
Show Gist options
  • Select an option

  • Save tylearymf/42b45128fa05be0d0a695313ad497ca9 to your computer and use it in GitHub Desktop.

Select an option

Save tylearymf/42b45128fa05be0d0a695313ad497ca9 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
using System;
using System.Reflection;
using Editor = UnityEditor.Editor;
[CanEditMultipleObjects]
[CustomEditor(typeof(Transform), true)]
public class TransformInspector : Editor
{
static Type s_InspectorType;
static MethodInfo s_OnEnableMethod;
Editor m_InspectorInstance;
Action m_Inspecotr_OnEnable;
static TransformInspector()
{
var type = typeof(Editor).Assembly.GetType("UnityEditor.TransformInspector");
s_InspectorType = type;
s_OnEnableMethod = type.GetMethod("OnEnable", BindingFlags.Public | BindingFlags.Instance);
}
void OnEnable()
{
m_InspectorInstance = CreateEditor(targets, s_InspectorType);
m_Inspecotr_OnEnable = (Action)Delegate.CreateDelegate(typeof(Action), m_InspectorInstance, s_OnEnableMethod, true);
m_Inspecotr_OnEnable?.Invoke();
}
void OnDisable()
{
if (m_InspectorInstance)
{
UnityEngine.Object.DestroyImmediate(m_InspectorInstance);
m_InspectorInstance = null;
}
}
public override void OnInspectorGUI()
{
using (new EditorGUILayout.HorizontalScope())
{
using (new EditorGUI.IndentLevelScope(1))
{
using (new EditorGUILayout.VerticalScope())
{
m_InspectorInstance?.OnInspectorGUI();
var indentPerLevel = 15F;
var lastRect = GUILayoutUtility.GetLastRect();
var singleLineHeight = EditorGUIUtility.singleLineHeight;
lastRect = DrawButton(lastRect, EditorGUI.indentLevel * indentPerLevel, 0, singleLineHeight, "S", x => x.localScale = Vector3.one);
lastRect = DrawButton(lastRect, EditorGUI.indentLevel * indentPerLevel, singleLineHeight + 2, singleLineHeight, "R", x => x.localEulerAngles = Vector3.zero);
lastRect = DrawButton(lastRect, EditorGUI.indentLevel * indentPerLevel, singleLineHeight + 2, singleLineHeight, "P", x => x.localPosition = Vector3.zero);
}
}
}
}
Rect DrawButton(Rect previousRect, float x, float offsetY, float height, string name, Action<Transform> callback)
{
var rect = previousRect;
rect.x = x;
rect.y -= offsetY;
rect.width = rect.height = height;
if (GUI.Button(rect, name))
{
Undo.RecordObjects(targets, "Inspector");
foreach (Transform item in targets)
{
callback(item);
}
}
return rect;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment