How to show the standard script line with a custom Editor

Hi,

I have written a custom editor for a script I’m using. All works fine, but I don’t get how to show this standard script field that is normaly shown grayed when using the standard editor and is filled with the script itself, so that it works as a shortcut to open the script in (for example) Visual Studio.

I know, I could use DrawDefaultInspector(); but I don’t want to, 'cause it screws up the rest of my custom editor and shows far too much stuff.

Thanks already! :wink:

Well, if you don’t want to change the script you shouldn’t assign the result back to the variable. Actually you don’t need a variable in the first place.

To “disable” the ObjectField you just need to set GUI.enabled to false before you draw the objectfield. Don’t forget to re-enable it afterwards or everything else would be disabled as well:

GUI.enabled = false;
EditorGUILayout.ObjectField("Script:", MonoScript.FromMonoBehaviour((FormationControl)target), typeof(FormationControl), false);
GUI.enabled = true;

A slight improvement on Bunny83’s answer:

using (new EditorGUI.DisabledScope(true))
    EditorGUILayout.ObjectField("Script", MonoScript.FromMonoBehaviour((MonoBehaviour)target), GetType(), false);

This is now copy-paste ready, with the specific class name (FormationControl) replaced.


I’ve also:

  • Changed “Script:” to “Script” so it looks exactly like Unity’s default

  • Used the nesting-friendly EditorGUI.DisabledScope (instead of GUI.enabled)

The target can be a MonoBehaviour or a ScriptableObject

EditorGUI.BeginDisabledGroup(true);

		// It can be a MonoBehaviour or a ScriptableObject
		var monoScript = (target as MonoBehaviour) != null
			? MonoScript.FromMonoBehaviour((MonoBehaviour)target)
			: MonoScript.FromScriptableObject((ScriptableObject)target);

		EditorGUILayout.ObjectField("Script", monoScript, GetType(), false);

EditorGUI.EndDisabledGroup();
1 Like