- Home /
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! ;)
Answer by Bunny83 · Aug 04, 2016 at 11:59 AM
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;
That was the solution I searched for. Got nearly mad about that stupid little problem. And then the solution is so simple... If you can make your comment into an answer, I'd like to mark it as correct, for other users.
Thank you very much!
Greets BlazingPhoenix
Answer by booferei · Apr 18, 2021 at 07:19 PM
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)
Answer by Firedan1176 · Jul 31, 2016 at 01:26 AM
Ok, now I got a script field with the correct script preselected. But sadly thats not exactly what I'm looking for. The field is neither greyed out nor unchangeable from within the inspector. Any other ideas or am I just missing something in the linked article?
the actual code at the moment is:
public override void OnInspectorGUI() {
$$anonymous$$onoScript script;
script = $$anonymous$$onoScript.From$$anonymous$$onoBehaviour((FormationControl)target);
script = EditorGUILayout.ObjectField("Script:", script, typeof($$anonymous$$onoScript), false) as $$anonymous$$onoScript;
[...]
}
Answer by PedroPoni · Apr 14 at 03:40 PM
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();
Your answer
