• 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
0
Question by JanZagar · Dec 27, 2018 at 12:44 PM · inspectoreditor-scriptingcustom editoreditorguicustom inspector

Custom inspector editor - how to put new editor fields in a specific place

Hi everyone :)

I'm trying to get new editor fields when a bool is checked in the inspector. While I did manage to do that, I don't know how to make it show right below the boolean you had to check and not at the bottom of the inspector.

Here is my code:

 override public void OnInspectorGUI()
 {
     base.OnInspectorGUI ();

     var myScript = target as CardAsset;

     if (myScript.KillEffect) {
         Debug.Log ("KillEffect is true");

         EditorGUILayout.LabelField ("TEST");
     }
 }

Any help is really appreciated :)

Comment
Add comment · Show 4
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 Vega4Life · Dec 27, 2018 at 01:30 PM 0
Share

You are saying it's showing at the bottom because there are more inspector variables from your other script?

Show more comments
avatar image TreyH · Dec 27, 2018 at 02:38 PM 2
Share

I don't think you can insert a single field into an existing inspector GUI. You only have access to its base.OnInspectorGUI() as you've seen, which just draws the entire default inspector block.

I think you will have to make a custom inspector for that type (which it seems like you're doing already) and just create those other fields yourself.

1 Reply

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

Answer by Vega4Life · Dec 27, 2018 at 03:08 PM

For fun I made an example on how to make what you want. Two scripts. To see how it works, copy these scripts and put the Example script on a gameObject. Then toggle the boolean.


     using UnityEngine;
 
     // Nothing special here, just a script with some exposed inspector variables
     public class Example : MonoBehaviour
     {
         [SerializeField] bool boolVariable;
         [SerializeField] float floatVariable;
         [SerializeField] int intVariable;
     }


Here is the custom editor for the above script that will put a label underneath the bool property if true


     using UnityEngine;
     using UnityEditor;
 
 
     [CustomEditor(typeof(Example))]
     public class CustomExample : Editor
     {
         SerializedProperty boolProperty;
         SerializedProperty floatProperty;
         SerializedProperty intProperty;
 
 
         void OnEnable()
         {
             boolProperty = serializedObject.FindProperty("boolVariable");
             floatProperty = serializedObject.FindProperty("floatVariable");
             intProperty = serializedObject.FindProperty("intVariable");
         }
 
 
         public override void OnInspectorGUI()
         {
             serializedObject.Update();
 
             EditorGUILayout.Space();
 
             boolProperty.boolValue = EditorGUILayout.Toggle(boolProperty.displayName, boolProperty.boolValue);
 
             if (boolProperty.boolValue)
             {
                 OnBoolPropertyTrue();
             }
 
             floatProperty.floatValue = EditorGUILayout.FloatField(floatProperty.displayName, floatProperty.floatValue);
             intProperty.intValue = EditorGUILayout.IntField(intProperty.displayName, intProperty.intValue);
 
             serializedObject.ApplyModifiedProperties();
         }
 
 
         void OnBoolPropertyTrue()
         {
             EditorGUILayout.LabelField("I AM BELOW THE BOOL PROPERTY NOW");
         }
     }



I hope this helps!


Comment
Add comment · Show 5 · 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 JanZagar · Dec 27, 2018 at 03:39 PM 0
Share

Huge thanks. You are awesome <3

avatar image Vega4Life JanZagar · Dec 27, 2018 at 03:43 PM 0
Share

Sure. Good luck!

avatar image JanZagar Vega4Life · Dec 27, 2018 at 10:29 PM 0
Share

Hey Vega4Life. $$anonymous$$ind telling me something? Why do you use serializedObject.Update(); with the lower case? If I use the upper case like this SerializedObject.Update(); I get errors. (I was trying to use the upper case because the official documentation uses the upper case)

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Trying to create Custom Inspector Labels and I get erros 0 Answers

How to Hide/Show List or Array in the inspector based on a variable? 0 Answers

Inspector Overlapping Text Label at a Position 1 Answer

Custom Inspector: How to Align EditorGUILayout.Objectfield in a Horizontal Group with other EditorGUILayout.Objectfield that aren't in Horizontal Groups? 0 Answers

Using a CustomEditor in the inspector and also seeing normal inspector fields 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