#InspectorWindow & Editor instance

1 messages · Page 1 of 1 (latest)

gleaming tiger
#

@prisma crown @untold robin ah! found a solution!
Don't know how robust this is, but seems to do the trick

[CustomEditor(typeof(SomeMono))]
public class SomeMonoEditor : Editor
{
    private void OnEnable()
    {
        EditorWindow ownerInspector = GetOwnerInspector();

        if (ownerInspector == null)
        {
            Debug.Log($"Couldn't find owner inspector for SomeMonoEditor with ID {this.GetInstanceID()}. This happens when the owner inspector of an Editor instance is locked inspecting a different target object");
        }
        else
        {
            Debug.Log($"SomeMonoEditor ({this.GetInstanceID()}) owner inspector InstanceID: {ownerInspector.GetInstanceID()}");
            Debug.Log($"SomeMonoEditor ({this.GetInstanceID()}) owner inspector rect: {ownerInspector.position}");
        }
    }

    private EditorWindow GetOwnerInspector()
    {
        List<EditorWindow> inspectorsWindows = InspectorWindowAccessor.GetInspectors();

        foreach (EditorWindow inspector in inspectorsWindows)
        {
            InspectorWindowAccessor accessor = new InspectorWindowAccessor(inspector);
            ActiveEditorTracker tracker = accessor.Tracker;

            for (int i = 0; i < tracker.activeEditors.Length; i++)
            {
                if (tracker.activeEditors[i].GetInstanceID() == this.GetInstanceID())
                {
                    return inspector;
                }
            }
        }

        return null;
    }
}
untold robin
#

Will break if the editor is shown in a custom editor window

gleaming tiger
#

oh...

#

hmmm

#

yeah just tried it with an Editor in an EditorWindow by using CreateEditor

#

OnEnable doesn't get called

#

ah sorry, it gets called

#

it just doesn't find the "InspectorWindow" because there's none

prisma crown
#

Of course, as we said, Editor != Inspector

gleaming tiger
#

yeah 😅

prisma crown
#

But, in fact, this might work for 99%

#

Maybe you dont need this last percent

gleaming tiger
#

I mean currently the tool that I'm making is just to check whether or not a vertical scrollbar exists in an Inspector

prisma crown
#

Well, then dismiss the 1% scenario 🙂

#

Or go check the GUIView XD

gleaming tiger
prisma crown
#

Do you want the code?

#

I can provide the code

gleaming tiger
#

@prisma crown code for what?

#

the source code for the View classes?

prisma crown
#

Check the guiview

gleaming tiger
#

ok brb

#

maybe this?

#

my knowledge of the whole View, GUIView, etc classes is very limited

#

so I'm a little bit lost

prisma crown
#

Strange... I know View contain the information somewhere of its window

#

I cant find it

untold robin
#

And DockView

#

But it inherits from HostView

prisma crown
#

aaaaah probably why

gleaming tiger
#
[SerializeField] private EditorWindow m_ActualView;
prisma crown
#

Hell yeah

#

Thanks @untold robin for the reminder, I was lost into fucking View "the hell am I looking at O_o"

gleaming tiger
#

the View classes are a little bit obfuscated lmao

#

i had to make that example to remind myself of all of them

untold robin
prisma crown
#
var GUIView = typeof(Editor).Assembly.GetType("UnityEditor.GUIView");
var current = GUIView.GetProperty("current", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
object currentView = current.GetValue(null);
var HostView = typeof(Editor).Assembly.GetType("UnityEditor.HostView");
var actualView = HostView.GetField("m_ActualView", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
EditorWindow editorWindow = (EditorWindow)actualView.GetValue(currentView);
#

Try that @gleaming tiger

untold robin
#

I got tired of reflection so I just made a wrapper class for each View class with the same inheritance so I can basically use them like they do internally. Saved me so much time and the code is so much cleaner

prisma crown
#

Yeah, got the utilities, but not here at this moment 😄

#

And obviously MadLed would not have them under his hand

#

In normal time I got this to fast prototype CSharpMeta.Resolve<Rect>(Resources.FindObjectsOfTypeAll<EditorWindow>()[0], "rootVisualContainer.worldClip");

gleaming tiger
#

hmm, error

ArgumentException: Field m_ActualView defined on type UnityEditor.HostView is not a field on the target object which is of type System.Reflection.MonoProperty.
#

i've never encountered this error, i don't even know how to interpret it

prisma crown
#

Can you print currentView before?

#

I guess it is not a HostView

prisma crown
gleaming tiger
#

@prisma crown ah I'm dumb, I was getting the error on the Inspectors

#

not on the EditorWindow that has the created Editor

#

🙃 stupid brain

prisma crown
#

Both should work seamlessly

#

I mean, EditorWindow are in DockArea (inherit from HostView)

#

InspectorWindow I don't know who hosts it

gleaming tiger
#

i get a null value here ```cs
object currentView = current.GetValue(null);
Debug.Log($"currentView: {currentView.GetType()}");

#

with the Inspector

prisma crown
#

Surprising...

#

change "current" with "focusedView"

gleaming tiger
#

@prisma crown ah I know why

#

i was doing it on OnEnable

#

I guess the View is not fully initialized or something

prisma crown
#

I would have hope OnEnable would work as well

gleaming tiger
#

i guess I just make an initialization method and run it once on OnInspectorGUI

#

seems to work fine, and it's compatible with regular Editor instances and created Editor instances in EditorWindows

#

thanks a lot @prisma crown @untold robin !!! 😋

#

i would have struggled so much with this lol

#

🙏

#

@untold robin hey, would it be much to ask if you could share your wrapper classes for the View API stuff?

#

i just recently started learning about this topic and it's a little bit too cumbersome

untold robin
untold robin
gleaming tiger
gleaming tiger
#

it seems like there's so many messy APIs inside their internal code

untold robin
gleaming tiger
#

i'll keep it in mind

untold robin
gleaming tiger
#

so far the only thing I want to learn how to do with the View API is to dock windows

#

the EditorWindow.GetWindow method has the desiredDockNextTo

#

but that just makes the window become a tab of the target, not dock

untold robin
#

So when positioning Views, you need to also update the position/size of the other views to accommodate it.

gleaming tiger
#

i see, thanks for the tip!

untold robin
gleaming tiger
#

🧙

#

it's so hard to find examples for this stuff so anything I can get is like a gift from the gods

untold robin