Skip to content

Instantly share code, notes, and snippets.

@jringrose
Last active June 1, 2018 00:37
Show Gist options
  • Save jringrose/3fc8c96798a2e3148464563c96018a74 to your computer and use it in GitHub Desktop.
Save jringrose/3fc8c96798a2e3148464563c96018a74 to your computer and use it in GitHub Desktop.
A utility to open up temporary inspector windows to view objects. NewInspectorWindow.cs and InspectorButtonPropertyDrawer.cs should be inside an Editor folder.
using UnityEngine;
public class InspectorButtonAttribute : PropertyAttribute { }
using System;
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(InspectorButtonAttribute))]
public class InspectorButtonPropertyDrawer : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
EditorGUI.PropertyField(new Rect(position.x, position.y, position.width - 50f, position.height), property);
if (GUI.Button(new Rect(position.width - 50f, position.y, 50f, position.height), "Inspect")) {
NewInspectorWindow.InspectTarget(property.objectReferenceValue);
}
}
}
using UnityEngine;
using UnityEditor;
using System.Collections;
public static class NewInspectorWindow {
public static void InspectTarget(GameObject target)
{
System.Type inspectorType = typeof(Editor).Assembly.GetType("UnityEditor.InspectorWindow");
EditorWindow inspectorInstance = ScriptableObject.CreateInstance(inspectorType) as EditorWindow;
var p = inspectorInstance.position;
Vector2 pos = GUIUtility.GUIToScreenPoint(Event.current.mousePosition);
pos.x = pos.x - p.size.x * 0.5f;
pos.y += 20f;
p.position = pos;
p.size = new Vector2(400f, 600f);
inspectorInstance.ShowUtility();
inspectorInstance.position = p;
var prevSelection = Selection.activeObject;
Selection.activeObject = target;
var isLocked = inspectorType.GetProperty("isLocked", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
isLocked.GetSetMethod().Invoke(inspectorInstance, new object[] { true });
Selection.activeObject = prevSelection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment