Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity Editor - DrawDefaultInspector is not working

I'm trying to write a simple editor extension just for learning-purposes. This extension will just check if the target is a JSON-file then count it's keys or do some other arbitrary task.

This is how the inspector looks like by default. This is how the inspector looks like by default

Then I started writing my custom inspector, just like this.

[CustomEditor(typeof(TextAsset))]
public class TestInspector : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();
    }
}

Note the call to DrawDefaultInspector().

Now the inspector looks like this.

Now the inspector looks like this.

Why is it not drawing the default inspector? What I can understand my extension should basically do nothing, right?

like image 482
Rasmus Avatar asked May 21 '15 13:05

Rasmus


2 Answers

If you want to override the built-in text asset inspector, you'll have to find it and call it from your own editor. The class is called UnityEditor.TextAssetInspector but it's a private internal class that can usually only be referenced by Unity internally. However, you can access it using reflection like this:

using System;
using System.Reflection;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(TextAsset))]
public class TestInspector : Editor
{
    private Editor m_editor;

    public override void OnInspectorGUI()
    {
        if (m_editor == null)
        {
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (var a in assemblies)
            {
                var type = a.GetType("UnityEditor.TextAssetInspector");
                if (type != null)
                {
                    m_editor = Editor.CreateEditor(target, type);
                    break;
                }
            }
        }

        if (m_editor != null)
            m_editor.OnInspectorGUI();
    }
}

This searches through all currently loaded assemblies for the type UnityEditor.TextAssetInspector, and then creates an instance of it for the current asset target. We use the version of Editor.CreateEditor that lets us pass in the type of editor to create, because otherwise it will try to create an instance of TestInspector.

Note that this is assuming certain things about Unity internals and may not work in future versions of Unity.

like image 102
Sam Avatar answered Oct 01 '22 14:10

Sam


I think the problem is TextAsset does not use normal editor class for inspector. Many builtin class of unity using special builtin editor class which is not public

You could try with changing [CustomEditor(typeof(TextAsset))] to [CustomEditor(typeof(Transform))] or [CustomEditor(typeof(Animator))] to see The "Real Default Inspector" of transform and animator is not the same as normal one

So if you want to make any custom editor. Don't care default inspector. Just make it from scratch. Good luck

like image 26
Thaina Yu Avatar answered Oct 01 '22 16:10

Thaina Yu