Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by brunocoimbra · Dec 29, 2015 at 01:31 AM · c#scripting probleminspectoreditor extensioneditorscript

How to draw the default read-only object label in a CustomEditor?

I am starting to learn about custom editors to create a better inspector for my AI settings. With so many settings, I decided to create a bool that while unchecked will hide things that I don't need to change.

But, I could not find a way to create a read-only object field that opens the script when clicked, while every monobehaviours class has it in it's default inspector.

By now, I am using a simple label to just act like an "placeholder" (as show in "AIBehaviourShoot") for the desired label (as show in "AIBehaviourChase" and every other default inspector).

alt text

and here is my current script:

 using UnityEditor;
     
 [CustomEditor(typeof(AIBehaviourChase))]
 [CanEditMultipleObjects]
 public class AIBehaviourChaseEditor : Editor
 {
     SerializedProperty canChase;
     
     private void OnEnable()
     {
         canChase = serializedObject.FindProperty("canChase");
     }
     
     public override void OnInspectorGUI()
     {
         serializedObject.Update();
         if (!canChase.hasMultipleDifferentValues)
         {
             if (canChase.boolValue)
             {
                 // the defaul inspector shows the read-only object field
                 DrawDefaultInspector();
             }
             else
             {
                 // this is the placeholder label that I want to change.
                 EditorGUILayout.LabelField("Script", "AIBehaviourChase");
     
                 canChase.boolValue = EditorGUILayout.Toggle("Can Chase", canChase.boolValue);
             }
         }
         serializedObject.ApplyModifiedProperties();
     }
 }

Sorry my english.

EDIT: There is no way of doing it?

sem-titulo.png (10.1 kB)
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

2 Replies

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

Answer by a_p_u_r_o · Jan 18, 2016 at 05:47 PM

Maybe this is what you are looking for.

http://docs.unity3d.com/ScriptReference/EditorGUI.BeginDisabledGroup.html

Here goes an example.

 using UnityEditor;
 using UnityEngine;
 
 [CustomEditor(typeof(NewBehaviourScript))]
 public class NewBehaviourScriptGUI : Editor {
 
     public override void OnInspectorGUI()
     {
         string[] guids = AssetDatabase.FindAssets("NewBehaviourScript t:Script");
         string path = AssetDatabase.GUIDToAssetPath(guids[0]);
         Object obj = AssetDatabase.LoadAssetAtPath<Object>(path);
         EditorGUI.BeginDisabledGroup(true);
         EditorGUILayout.ObjectField("Script:", obj, typeof(Object), false);
         EditorGUI.EndDisabledGroup();
     }
 }


Follow up as of May 10, 2016.

I recently found that this question was asked before.

http://answers.unity3d.com/questions/550829/

And learned it can be also written in this way.

 EditorGUI.BeginDisabledGroup(true);
 EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Script"));
 EditorGUI.EndDisabledGroup();

Simplest! and may be this is the most similar to what Unity does internally, I guess.

It depends but you may prefer to use MonoScript.FromMonoBehaviour(). See comments.

Comment
Add comment · Show 4 · 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 brunocoimbra · Jan 18, 2016 at 06:46 PM 0
Share

YES! Exactly this! I already knew the DisabledGroup thing, the problem was to reference the asset path, never knew about GUIDToAssetPath, thank you so much!

avatar image Bunny83 · Jan 19, 2016 at 05:53 AM 1
Share

This seems to be a very complicated and risky way of getting hold of the script asset reference. Unity has a method for getting the $$anonymous$$onoScript instance from a $$anonymous$$onoBehaviour reference:

$$anonymous$$onoScript.From$$anonymous$$onoBehaviour

This method will return the reference to the $$anonymous$$onoScript asset that the given $$anonymous$$onoBehaviour belongs to. $$anonymous$$onoScript is an editor class that represents a script asset. $$anonymous$$onoScript is derived from TextAsset and literally represents the plain text of the script file. It also adds the GetClass method which returns the System.Type of the $$anonymous$$onoBehaviour class / ScriptableObject class that is defined inside that script asset.

avatar image a_p_u_r_o · Jan 19, 2016 at 07:05 AM 2
Share

Thank you, Bunny83! I thought there must be something of that effect. But I couldn't find it out.

Now we can make it as easy as this.

 using UnityEditor;
 using UnityEngine;
 
 [CustomEditor(typeof(NewBehaviourScript))]
 public class NewBehaviourScriptGUI : Editor {
 
     public override void OnInspectorGUI() {
         $$anonymous$$onoScript script = $$anonymous$$onoScript.From$$anonymous$$onoBehaviour(target as $$anonymous$$onoBehaviour);
         EditorGUI.BeginDisabledGroup(true);
         EditorGUILayout.ObjectField("Script", script, typeof($$anonymous$$onoScript), false);
         EditorGUI.EndDisabledGroup();
     }
 }
avatar image brunocoimbra · Jan 19, 2016 at 10:11 PM 0
Share

Learning something new everyday... Thank you both for the replies! $$anonymous$$nowing about that $$anonymous$$onoScript class will surely help me even more.

avatar image
0

Answer by Masterio · Jan 04, 2016 at 12:51 PM

http://docs.unity3d.com/ScriptReference/EditorGUILayout.ObjectField.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 brunocoimbra · Jan 04, 2016 at 02:19 PM 0
Share

I already checked the scripting API, but it only show how to create a selectable object field. I really wanted to know how Unity made it to be a read-only object field.

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

Inspector changes set TextMeshPro values on play 1 Answer

Distribute terrain in zones 3 Answers

Can I add an enum value in the inspector? 4 Answers

Arrays/Lists in the inspector of Editor Extension scripts 0 Answers

There is no GameObject attached to this GameObject 2 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