Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by bruce965 · Sep 11, 2012 at 11:00 AM · c#inspector

How to remove "Script" field in inspector?

There is a "Script" field in the inspector that allows quick replacement of a MonoBehaviour, but I don't want it to show...

alt text

Is there a way to hide it without having to rewrite OnInspectorGUI completely. Thanks!

immagine.png (6.9 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lpye · Sep 11, 2012 at 12:06 PM 1
Share

Can I ask why you feel the need to remove that field from the inspector? I ask this only because at first blush it isn't obvious, to me at least, why there would be a need for this, so understanding what you are trying to accomplish might help to understand the problem.

avatar image bruce965 · Sep 11, 2012 at 12:47 PM 1
Share

Actually that isn't foundamental, I just want to make a script looking more professional by showing only what is stictly necessary, and the "script" field is only a distraction. Thanks for your time.

avatar image bruce965 · Sep 11, 2012 at 01:13 PM 1
Share

That is only because I like keeping my stuff clean, so I wanted to make this script look quite like the builtin Terrain, but since it's generated programmatically I don't need many parameters, so I don't need to write a custom interface, but still I don't like that field. If you say there is no other way than rewriting the inspector OnInspectorGUI function I will do so. Thanks again for your time and your kindness.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Sep 11, 2012 at 01:23 PM

You just have to implement a custom inspector for your class(es). You have to scroll half way down since the first part is about EditorWindows.

btw there's no way to not "rewrite" the GUI since the DefaultInspector works as it is.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bruce965 · Sep 11, 2012 at 01:27 PM 0
Share

Thanks, that is what I will do, but that is also what i wanted to avoid doing.

avatar image Kryptos · Sep 11, 2012 at 02:01 PM 0
Share

This custom inspector will be very easy to write since you can use DrawDefaultInspector() (unless this also draws the script field).

avatar image bruce965 · Sep 11, 2012 at 04:42 PM 0
Share

@Fattie: Yes, I know the Invoke command even if I don't like using interruptions in single-thread applications, so I never use it.

avatar image Kryptos · Sep 11, 2012 at 07:37 PM 1
Share

@bruce965 This has nothing to do with interruptions and threading. Coroutines are another kind of mechanism (Invoke() use a coroutine internally).

You can use it with almost no overhead at all.

avatar image bruce965 · Sep 11, 2012 at 07:52 PM 1
Share

C# is already simple enough, imagine you had to code in C or in TAS$$anonymous$$... That would have been a pain, coding in a high level language is so simple!

Show more comments
avatar image
25

Answer by Johat · Feb 17, 2015 at 02:52 PM

Hi,

I know this is kind of old now, but I saw that people were still responding.

This is actually very simple to achieve, but it will involve a custom editor of sorts.

The property you're looking to exclude is "m_Script", as others have said and Unity actually gives you a nice alternative to DrawDefaultInspector that allows excluding properties: DrawPropertiesExcluding. They're not exactly equivalent (the names should hint at what they're both doing), but it makes it very easy to get your desired behaviour. DrawPropertiesExcluding has the following signature:

 protected internal static void DrawPropertiesExcluding(SerializedObject obj, params string[] propertyToExclude);

You can actually use this to exclude any properties you don't want showing up.

So, simply call it from within an editor script, passing the SerializedObject (in this case, it would simply be the serializedObject that inheriting from Editor gives you access to).

Make sure you call serializedObject.Update() before, and serializedObject.ApplyModifiedProperties() after.

So, a basic implementation could be like so:

 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(MyClassIDontWantToSeeScriptIn))]
 public class ScriptlessEditor : Editor
 {
     private static readonly string[] _dontIncludeMe = new string[]{"m_Script"};
     
     public override void OnInspectorGUI()
     {
         serializedObject.Update();
 
         DrawPropertiesExcluding(serializedObject, _dontIncludeMe);
 
         serializedObject.ApplyModifiedProperties();
     }
 }

Now, that still seems like a bit to add when you want something as simple as removing a property, so I recommend creating something you can inherit from. If you only want to remove the script property then something like this would do you:

ScriptlessEditor.cs:

     using UnityEngine;
     using UnityEditor;
     
     public abstract class ScriptlessEditor : Editor
     {
         private static readonly string[] _dontIncludeMe = new string[]{"m_Script"};
         
         public override void OnInspectorGUI()
         {
             serializedObject.Update();
     
             DrawPropertiesExcluding(serializedObject, _dontIncludeMe);
     
             serializedObject.ApplyModifiedProperties();
         }
     }

And then any MonoBehaviour you wanted to act like this, you'd just create:

MyScriptlessMonoBehaviourInspector.cs:

 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(MyScriptlessMonoBehaviour))]
 public class MyScriptlessMonoBehaviourInspector : ScriptlessEditor
 {}

Personally, I find I regularly want to make minor tweaks to how an Inspector displays, but not enough to justify reimplementing everything, so I actually use a variant of the following script to derive from, which makes minor tweaks/small custom behaviour a breeze. It should speak for itself, but let me know if you want me to explain any of it.

TweakableEditor.cs:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 /// <summary>
 /// A simple class to inherit from when only minor tweaks to a component's inspector are required.
 /// In such cases, a full custom inspector is normally overkill but, by inheriting from this class, custom tweaks become trivial.
 /// 
 /// To hide items from being drawn, simply override GetInvisibleInDefaultInspector, returning a string[] of fields to hide.
 /// To draw/add extra GUI code/anything else you want before the default inspector is drawn, override OnBeforeDefaultInspector.
 /// Similarly, override OnAfterDefaultInspector to draw GUI elements after the default inspector is drawn.
 /// </summary>
 public abstract class TweakableEditor : Editor
 {
     private static readonly _emptyStringArray = new string[0];
     
     public override void OnInspectorGUI()
     {
         serializedObject.Update();
         
         OnBeforeDefaultInspector();
         DrawPropertiesExcluding(serializedObject, GetInvisibleInDefaultInspector());
         OnAfterDefaultInspector();
 
         serializedObject.ApplyModifiedProperties();
     }
 
     protected virtual void OnBeforeDefaultInspector()
     {}
 
     protected virtual void OnAfterDefaultInspector()
     {}
 
     protected virtual string[] GetInvisibleInDefaultInspector()
     {
         return _emptyStringArray;
     }
 }
 

Then simply inherit and override any features you want to use, for example:

ExampleEditor.cs:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomEditor(typeof(Example))]
 public class ExampleEditor : TweakableEditor
 {
     // Stops showing the script field
     protected override string[] GetInvisibleInDefaultInspector()
     {
         return new[] { "m_Script" };
     }
 
     // Just showing other stuff we can add
     protected override void OnBeforeDefaultInspector()
     {
         GUILayout.Label("What a fancy Inspector this is!");
     }
 }

I've found this approach a nice balance between having some control, but being able to mostly default to the default inspector (except when I really need to make something custom because that's what's appropriate for the script).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pawl · Dec 02, 2015 at 03:49 AM 0
Share

DrawPropertiesExcluding doesn't appear to be listed on the Unity documentation anywhere, so this is great! Thanks!!

avatar image Aissasa · Apr 06, 2016 at 01:38 PM 0
Share

You, sir, need a cookie (y) Very useful response, I love it. Thank you.

avatar image
1

Answer by JAKJ · Sep 18, 2014 at 10:05 AM

I have come up with 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.)

 using UnityEngine ;
 using UnityEditor ;
 
 [ CustomEditor ( typeof ( MonoBehaviour ) , true ) ]
 public class DefaultInspector : Editor
 {
     public override void OnInspectorGUI ( )
     {
         this . DrawDefaultInspectorWithoutScriptField ( ) ;
     }
 }
 
 public static class DefaultInspector_EditorExtension
 {
     public static bool DrawDefaultInspectorWithoutScriptField ( this Editor Inspector )
     {
         EditorGUI . BeginChangeCheck ( ) ;
         
         Inspector . serializedObject . Update ( ) ;
         
         SerializedProperty Iterator = Inspector . serializedObject . GetIterator ( ) ;
         
         Iterator . NextVisible ( true ) ;
         
         while ( Iterator . NextVisible ( false ) )
         {
             EditorGUILayout . PropertyField ( Iterator , true ) ;
         }
         
         Inspector . serializedObject . ApplyModifiedProperties ( ) ;
         
         return ( EditorGUI . EndChangeCheck ( ) ) ;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Inspector Definitions for New Script Components Problem 0 Answers

What is the name of/How do I use the box in the Script Inspector that allows you to assign values to Public data? 2 Answers

Inspector cannot find script instance 1 Answer

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges