Hide "Script" ObjectField in custom Editor

Hey peops,

I’m at the point where I want to make custom editors for some of my scripts, to make them production ready and failsafe. One problem I have is: the default Scripts ObjectField is still showing up.
I’m sure I made custom editor before and the Scrips field just magically disappeared and I didn’t really thing much about it. But with this layout it is really annoying!
Anyone who knows how I get rid of it without always writing a custom editor vor every field in my scripts?


So green is my custom editor
yellow is the base.OnInspectorGUI()
but why is the red ObjectField appearing?
I always had the impression that a custom editor mutes this field, but apparently it belongs to the base.OnInspectorGUI() ?

Anyway: I want to get rid of it :wink:

If you selectively want to get rid of a single field methinks you’ll have to tell Unity field by field what to draw. Maybe simply copy the Editor’s own generic approach from ProprtyDrawer and just drop the script field?

It’s a part of base.OnInspectorGUI(), so your yellow square is wrong.

Other than that, what @scofranz says. Use the SerializedProperty’s iterator feature to iterate all the features and draw the properties.

2 Likes

I tried to fiddle with that, but the PropertyDrawer from unity is not really overridable, which is a problem. I guess if you really try hard you could get in somehow, but the approach from @Baste seems to be the intended way.

I went on how to implement that and found this example script: https://gist.github.com/rutcreate/d550aa1ae4052e0a0b37

This gave me enough understanding to use it for my purpose. I added the functionality to skip field to my ExtensionMethods, so I don’t have to include the script in all of my editor scripts.

I’ll share my solution with you:

        /// <summary>
        /// Draws all properties like base.OnInspectorGUI() but excludes a field by name.
        /// </summary>
        /// <param name="fieldToSkip">The name of the field that should be excluded. Example: "m_Script" will skip the default Script field.</param>
        public static void DrawInspectorExcept(this SerializedObject serializedObject, string fieldToSkip)
        {
            serializedObject.DrawInspectorExcept(new string[1] { fieldToSkip });
        }

        /// <summary>
        /// Draws all properties like base.OnInspectorGUI() but excludes the specified fields by name.
        /// </summary>
        /// <param name="fieldsToSkip">
        /// An array of names that should be excluded.
        /// Example: new string[] { "m_Script" , "myInt" } will skip the default Script field and the Integer field myInt.
        /// </param>
        public static void DrawInspectorExcept(this SerializedObject serializedObject, string[] fieldsToSkip)
        {
            serializedObject.Update();
            SerializedProperty prop = serializedObject.GetIterator();
            if (prop.NextVisible(true))
            {
                do
                {
                    if (fieldsToSkip.Any(prop.name.Contains))
                        continue;

                    EditorGUILayout.PropertyField(serializedObject.FindProperty(prop.name), true);
                }
                while (prop.NextVisible(false));
            }
            serializedObject.ApplyModifiedProperties();
        }

Usage:
(this will hide the red marked Script field in my first post)

    [CustomEditor(typeof(Billboard))]
    public class BillboardEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            Billboard t = (Billboard)target;
            t.cameraToFollow = (Billboard.CameraToFollow)EditorGUILayout.Popup("Camera to follow", (int)t.cameraToFollow, new string[] { "Main Camera", "Custom Camera", "Camera Name" });
            switch (t.cameraToFollow)
            {
                case Billboard.CameraToFollow.CustomCamera:
                    t.facingCamera = (Camera)EditorGUILayout.ObjectField(" ", t.facingCamera, typeof(Camera), true);
                    break;
                case Billboard.CameraToFollow.CameraName:
                    t.cameraName = EditorGUILayout.TextField(" ", t.cameraName);
                    break;
            }
            // Instead of base.OnInspectorGUI()
            serializedObject.DrawInspectorExcept("m_Script");
        }
    }

You can also remove multiple Fields by putting:
serializedObject.DrawInspectorExcept(new string[ ] { "mimicTilt" , "m_Script" });
this will get rid of the default Script field and the mimicTilt bool field.

Thanks for the leading in the right direction!

I know I am several years late to this party, but just to further expand into @Noblauch solution and if someone else ends up here then: If you want to move the “Script” field to the top of the CustomEditor (instead of removing it or instead of having it in between custom fields), then you can do it like this:

EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Script"));
DrawPropertiesExcluding(serializedObject, new string[] { "m_Script" });
7 Likes

it really helps, thanks so much