- Home /
Editor: multiple Inspectors
Hi,
I would like to know if it s possible to show two gameobject properties in two different inspectors?
Thanks,
Do you mean you want to show two game objects through editor script code you've written or do you just want to line up two inspectors that edit two different objects?
Answer by Ben 14 · Dec 16, 2010 at 03:33 PM
Actually it's possible, but not intuitive. Bear with me:
- select your first game object
- lock the inspector by clicking the small lock icon on the top right of the inspector window. This will keep the inspector displaying your game object when selecting another one
- right-click the inspector tab. This should give you a context menu with a "Add Tab >" entry at the bottom.
- Expand "Add Tab >" and select "Inspector".
- this creates another inspector window, showing the same object - but this one is not locked.
- select your other object, and voila it displays in the new inspector window.
Hope this helps
Ben
Answer by vexe · May 30, 2014 at 12:06 AM
Based on @Ben-14's work-around, here's how you create a new inspector window inspecting a certain target gameObject from code.
/// <summary>
/// Creates a new inspector window instance and locks it to inspect the specified target
/// </summary>
public static void InspectTarget(GameObject target)
{
// Get a reference to the `InspectorWindow` type object
var inspectorType = typeof(Editor).Assembly.GetType("UnityEditor.InspectorWindow");
// Create an InspectorWindow instance
var inspectorInstance = ScriptableObject.CreateInstance(inspectorType) as EditorWindow;
// We display it - currently, it will inspect whatever gameObject is currently selected
// So we need to find a way to let it inspect/aim at our target GO that we passed
// For that we do a simple trick:
// 1- Cache the current selected gameObject
// 2- Set the current selection to our target GO (so now all inspectors are targeting it)
// 3- Lock our created inspector to that target
// 4- Fallback to our previous selection
inspectorInstance.Show();
// Cache previous selected gameObject
var prevSelection = Selection.activeGameObject;
// Set the selection to GO we want to inspect
Selection.activeGameObject = target;
// Get a ref to the "locked" property, which will lock the state of the inspector to the current inspected target
var isLocked = inspectorType.GetProperty("isLocked", BindingFlags.Instance | BindingFlags.Public);
// Invoke `isLocked` setter method passing 'true' to lock the inspector
isLocked.GetSetMethod().Invoke(inspectorInstance, new object[] { true });
// Finally revert back to the previous selection so that other inspectors continue to inspect whatever they were inspecting...
Selection.activeGameObject = prevSelection;
}
// Now you just:
InspectTarget(myGO);
This is an editor script so you need to add using UnityEditor;
as well as System.Reflection
for the BindingFlags
How did I know about this? Simple, by decompiling UnityEditor.dll and lurking inside. I personally like ILSpy, there's also JustDecompile, dotPeek and (now the infamous) .NET Reflector
How do you actually use this now? I created a new class called "DualInspectorWindows" and placed this in there, but I'm not entirely sure of how to use this.
Where does this go?
// Now you just: InspectTarget(myGO);
$$anonymous$$y class looks like this:
@Dave_Voyles: Normally you'd just put this static method in a static utility class and just call it on a gameObject. In your case just do: DualInspectorWindows.InspectTarget(someGameObject);
Answer by ravin5432 · Aug 12, 2017 at 08:04 PM
Thank you for telling me about that lock!!! wow, made it so much easier
@ravin5432 What you posted is not an answer. Also even as comment it's totally unnecessary to bump a thread that is already 7 years old... Just upvote the question and answer if you think it was helpful.