I’m running Unity 5 (5.6.7f1) so can’t use the package I’ll be linking to as it is for 2018, and I’d like to build my own to understand how it works better anyway.
I’m looking to add buttons to reset (zero), copy and paste transforms (both local and global) of selected objects.
Example:
What I’m trying to replicate is what is seen here (second image).
Their code seems very complex and I’m looking for something far more lightweight I can actually understand.
So far, I’ve been able to draw the custom foldouts I’m using elsewhere in my custom tools, but haven’t been able to successfully replicate this without losing significant functionality.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.Reflection;
using UnityEngine.SocialPlatforms;
[CustomEditor(typeof(Transform))]
[CanEditMultipleObjects]
public class CustomTransformExtension : Editor
{
//Built-in editor
Editor defaultEditor;
Transform transform;
bool localSpace = true;
bool worldSpace = false;
void OnEnable()
{
//Called when inspector is created
defaultEditor = Editor.CreateEditor(targets, Type.GetType("UnityEditor.TransformInspector, UnityEditor"));
transform = target as Transform;
}
void OnDisable()
{
//Prevents memory leakage
MethodInfo disableMethod = defaultEditor.GetType().GetMethod("OnDisable", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if(disableMethod != null)
{
disableMethod.Invoke(defaultEditor, null);
}
DestroyImmediate(defaultEditor);
}
public override void OnInspectorGUI()
{
//If Arcane Transform Extension is enabled
if(EditorPrefs.GetBool("arcaneTransformExtension") == true)
{
EditorGUILayout.Space();
localSpace = Foldout("Local Space", localSpace);
if (localSpace)
{
defaultEditor.OnInspectorGUI();
}
EditorGUILayout.Space();
worldSpace = Foldout("World Space", worldSpace);
if (worldSpace)
{
}
}
else //Use default Editor
{
defaultEditor.OnInspectorGUI();
}
}
public static bool Foldout(string title, bool display)
{
var style = new GUIStyle("ShurikenModuleTitle");
style.font = new GUIStyle(EditorStyles.label).font;
style.border = new RectOffset(15, 7, 4, 4);
style.fixedHeight = 22;
style.contentOffset = new Vector2(20f, -2f);
var rect = GUILayoutUtility.GetRect(16f, 22f, style);
GUI.Box(rect, title, style);
var e = Event.current;
var toggleRect = new Rect(rect.x + 4f, rect.y + 2f, 13f, 13f);
if (e.type == EventType.Repaint)
{
EditorStyles.foldout.Draw(toggleRect, false, false, display, false);
}
if (e.type == EventType.MouseDown && rect.Contains(e.mousePosition))
{
display = !display;
e.Use();
}
return display;
}
}
Does anyone have any idea how I can achieve something similar without breaking everything?