Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
4
Question by BlazingPhoenix · Jul 31, 2016 at 01:25 AM · custom editor

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! ;)

Comment
Add comment
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

4 Replies

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

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;

Comment
Add comment · Show 1 · 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 BlazingPhoenix · Aug 07, 2016 at 08:26 AM 0
Share

@Bunny83

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

avatar image
4

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)

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
avatar image
0

Answer by Firedan1176 · Jul 31, 2016 at 01:26 AM

http://answers.unity3d.com/questions/550829/how-to-add-a-script-field-in-custom-inspector.html

Comment
Add comment · Show 1 · 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 BlazingPhoenix · Aug 02, 2016 at 11:17 AM 0
Share

@Firedan1176:

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;
 
 [...]
 }
avatar image
0

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();

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

6 People are following this question.

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

Related Questions

Confirm serialized property before changing it in a custom editor 1 Answer

custom button 2 Answers

Custom control in editor. 1 Answer

How to limit an ObjectField to assets with specific file extension 2 Answers

Custom Editor for array of custom class 1 Answer


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