- Home /
Query whether inspector is folded?
Hey, is there any way to query whether a custom editor (I call 'em inspectors) is collapsed/folded in the Unity editor via code?
Answer by Bunny83 · Oct 02, 2014 at 12:53 PM
I'm sure InternalEditorUtility.GetIsInspectorExpanded(target);
is what you need. However Unity (or to be more precise the InspectorWindow) seperately tracks the visible state for each editor. The method i mentioned has to be passed the reference to the inspected object (editor.target).
If that doesn't work it's getting tricky since the InspectorWindow uses a seperate Tracker class to remember the visibility state on a per-index-base of the current editor array. With reflection almost everything is possible, but it quickly get very complex.
edit
Well, i just wrote a little helper class that simplifies the access to the internal class InspectorWindow:
using UnityEditor;
using System.Runtime.InteropServices;
public static class InspectorWindowHelper
{
private static System.Type m_InspectorWindowType = null;
private static _MethodInfo m_GetAllInspectors = null;
private static _MethodInfo m_GetTracker = null;
static InspectorWindowHelper()
{
foreach(var T in typeof(EditorApplication).Assembly.GetTypes())
{
if (T.Name == "InspectorWindow")
{
m_InspectorWindowType = T;
m_GetAllInspectors = T.GetMethod("GetAllInspectorWindows",System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
m_GetTracker = T.GetMethod("GetTracker",System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
break;
}
}
}
public static EditorWindow[] GetAllInspectorWindows()
{
var array = m_GetAllInspectors.Invoke(null,null) as EditorWindow[];
return array;
}
public static ActiveEditorTracker GetTracker(EditorWindow aWin)
{
var tracker = m_GetTracker.Invoke(aWin,null) as ActiveEditorTracker;
return tracker;
}
public static void EnforceExpanded(System.Type aCustomEditor)
{
var windows = GetAllInspectorWindows();
for(int n = 0; n < windows.Length; n++)
{
var tracker = GetTracker(windows[n]);
var editors = tracker.activeEditors;
for(int i = 0; i < editors.Length; i++)
{
if (editors[i].GetType() == aCustomEditor)
{
int visible = tracker.GetVisible(i);
if (visible == 0)
{
tracker.SetVisible(i,1);
}
}
}
}
}
}
I've added an example method (EnforceExpanded) to show what you can do with it and how to use it.
EnforceExpanded can simply be called within OnInspectorGUI() of your custom inspector. Just pass the type of your editor class to prevent collapsing in all inspector windows.
You didn't say for what purpose you want that information and where you need it, so for the future you should be more precise when asking questions here.
Yeah, tried that but didn't work.
I haven't managed to reflect the particular tracker because I'm not sure where to get the InspectorWindow instance it belongs to from. Is there any sort of event I can hook into to get the state change at least that you know of?
@amir-barak: I've updated my answer.
It would be great to get some feedback ;)
Haven't had a chance to test this out yet. Will post an update once I do.
Awesome. I'll post up my code a bit later (modified it to suit my needs but yeah, that's a good base to start working from). I still don't see why the visible flag is hidden.. The UnityEditor.Editor class should have an event fired when the visibility changes.
Your answer
Follow this Question
Related Questions
How does one inspect static vars? 3 Answers
Editor: multiple Inspectors 3 Answers
Getting the selected enum value set in the Inspector 2 Answers
WrapMode not visible in inspector 1 Answer
Singleton & Inspector 1 Answer