Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Hiding "Script" Field In Default Inspector

Discussion in 'Scripting' started by JAKJ, Sep 18, 2014.

  1. JAKJ

    JAKJ

    Joined:
    Aug 17, 2014
    Posts:
    185
    If you're like me, this field is annoying, because I never find a use for it (though I do know it has valid uses, just not for me) and it clutters the visual space and makes it more difficult to quickly sort through objects that have a bunch of scripts attached. So, here is something to put into your Editor folder that will hide that field in everything using a default inspector and provide a method to replace DrawDefaultInspector in your code to do the same.

    (Unfortunately, since extension methods cannot override/hide existing methods, any code that calls Editor.DrawDefaultInspector that you cannot change will still show the Script field.)

    (For reference, the field in question is "m_Script" in the SerializedObject and seems to always come first.)

    Code (CSharp):
    1. using UnityEngine ;
    2. using UnityEditor ;
    3.  
    4. [ CustomEditor ( typeof ( MonoBehaviour ) , true ) ]
    5. public class DefaultInspector : Editor
    6. {
    7.     public override void OnInspectorGUI ( )
    8.     {
    9.         this . DrawDefaultInspectorWithoutScriptField ( ) ;
    10.     }
    11. }
    12.  
    13. public static class DefaultInspector_EditorExtension
    14. {
    15.     public static bool DrawDefaultInspectorWithoutScriptField ( this Editor Inspector )
    16.     {
    17.         EditorGUI . BeginChangeCheck ( ) ;
    18.        
    19.         Inspector . serializedObject . Update ( ) ;
    20.        
    21.         SerializedProperty Iterator = Inspector . serializedObject . GetIterator ( ) ;
    22.        
    23.         Iterator . NextVisible ( true ) ;
    24.        
    25.         while ( Iterator . NextVisible ( false ) )
    26.         {
    27.             EditorGUILayout . PropertyField ( Iterator , true ) ;
    28.         }
    29.        
    30.         Inspector . serializedObject . ApplyModifiedProperties ( ) ;
    31.        
    32.         return ( EditorGUI . EndChangeCheck ( ) ) ;
    33.     }
    34. }
     
    DrAwesomeXD, ModLunar and mghts like this.
  2. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    270
    I know this is a somewhat old thread, but I found this pretty handy! I'm creating a few editor controls where the default inspector is embedded inside of another inspector. Using this to skip the script field makes it a lot less cluttered. Thanks!
     
  3. sinjimonkey

    sinjimonkey

    Joined:
    Jul 31, 2013
    Posts:
    72
    This is old, but it came up in a search.

    If you are using classes derived from Odin Inspector's SerializedMonoBehavior you have to put that in where it says MonoBehavior. Apparently Odin's SerializedMonoBehavior doesn't decent from MonoBehaviour.

    After doing that it works perfectly.

    EDIT: Nevermind. It completely breaks OdinInspector. It successfully removes the script line but it breaks everything else.

    EDIT 2: I asked the Odin Inspector developers - turns out there's an option in the preferences (under the tools menu) that does this automatically when using that plugin.
     
    Last edited: Feb 15, 2018
    Mikkelens and ModLunar like this.