How to draw the default read-only object label in a CustomEditor?

I am starting to learn about custom editors to create a better inspector for my AI settings.
With so many settings, I decided to create a bool that while unchecked will hide things that I don’t need to change.

But, I could not find a way to create a read-only object field that opens the script when clicked, while every monobehaviours class has it in it’s default inspector.

By now, I am using a simple label to just act like an “placeholder” (as show in “AIBehaviourShoot”) for the desired label (as show in “AIBehaviourChase” and every other default inspector).

60793-sem-titulo.png

and here is my current script:

using UnityEditor;
    
[CustomEditor(typeof(AIBehaviourChase))]
[CanEditMultipleObjects]
public class AIBehaviourChaseEditor : Editor
{
    SerializedProperty canChase;
    
    private void OnEnable()
    {
        canChase = serializedObject.FindProperty("canChase");
    }
    
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        if (!canChase.hasMultipleDifferentValues)
        {
            if (canChase.boolValue)
            {
                // the defaul inspector shows the read-only object field
                DrawDefaultInspector();
            }
            else
            {
                // this is the placeholder label that I want to change.
                EditorGUILayout.LabelField("Script", "AIBehaviourChase");
    
                canChase.boolValue = EditorGUILayout.Toggle("Can Chase", canChase.boolValue);
            }
        }
        serializedObject.ApplyModifiedProperties();
    }
}

Sorry my english.

EDIT: There is no way of doing it?

1 Like

Maybe this is what you are looking for.

Here goes an example.

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(NewBehaviourScript))]
public class NewBehaviourScriptGUI : Editor {

    public override void OnInspectorGUI()
    {
        string[] guids = AssetDatabase.FindAssets("NewBehaviourScript t:Script");
        string path = AssetDatabase.GUIDToAssetPath(guids[0]);
        Object obj = AssetDatabase.LoadAssetAtPath<Object>(path);
        EditorGUI.BeginDisabledGroup(true);
        EditorGUILayout.ObjectField("Script:", obj, typeof(Object), false);
        EditorGUI.EndDisabledGroup();
    }
}

Follow up as of May 10, 2016.

I recently found that this question was asked before.

http://answers.unity3d.com/questions/550829/

And learned it can be also written in this way.

EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Script"));
EditorGUI.EndDisabledGroup();

Simplest! and may be this is the most similar to what Unity does internally, I guess.

It depends but you may prefer to use MonoScript.FromMonoBehaviour().
See comments.