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
0
Question by bfops · Nov 14, 2017 at 04:24 PM · custom editorcustom-inspectorattributepropertyfield

Custom editor renders attributes that overlap properties

I have a custom editor explicitly manipulates Rects and calls EditorGUI instead of EditorGUILayout. This mostly works fine except for fields with Header attributes, where it seems like too many things are being rendered into the same rect (i.e. the header overlaps the property). How can I explicitly pull out the Header and render it separately, or ask EditorGUI to hand me a Rect that's resized to hold the Header?

Example attempt to reimplement the default editor:

 using UnityEditor;
 using UnityEngine;
 using System;
 using System.Collections;
 
 [CustomEditor(typeof(ScriptableObject), true)]
 [CanEditMultipleObjects]
 public class CustomEditorBase : Editor {
     protected virtual void RenderProperty(Rect position, SerializedProperty property) {
         EditorGUI.PropertyField(position, property); // if the property has a header attribute, this renders them overlapping
     }
 
     protected void RenderChildren(SerializedProperty property, SerializedProperty end) {
         if (!property.NextVisible(true)) {
             property = null;
         }
         while (!SerializedProperty.EqualContents(property, end)) {
             RenderProperty(EditorGUILayout.GetControlRect(), property);
 
             var nextSibling = property.Copy();
             bool hasNext = nextSibling.NextVisible(false);
             if (!hasNext) {
                 nextSibling = null;
             }
 
             if (property.isExpanded) {
                 EditorGUI.indentLevel++;
                 RenderChildren(property, nextSibling);
                 EditorGUI.indentLevel--;
             }
 
             property = nextSibling;
         }
     }
 
     void RenderObject(SerializedObject obj) {
         RenderChildren(obj.GetIterator(), null);
     }
 
     public override void OnInspectorGUI() {
         serializedObject.Update();
         RenderObject(serializedObject);
         serializedObject.ApplyModifiedProperties();
     }
 }
Comment
Add comment · Show 2
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 Bunny83 · Nov 14, 2017 at 04:58 PM 0
Share

Way too unspecific question. You have to give more details about your editor and show the relevant code. A "normal" custom editor uses the layoutsystem while the built-in inspector mainly uses fixed sized gui elements. That's what a PropertyDrawer / DecoratorDrawer are ment for. If a field has an Header attribute the header is not drawn automatically if you create a custom editor unless you use PropertyField.

avatar image bfops Bunny83 · Nov 16, 2017 at 12:22 AM 0
Share

Right, I am using PropertyField, but something like EditorGUI.PropertyField(EditorGUILayout.GetControlRect(), property) renders the header in a way that overlaps the property, which is surprising. How can I either manually render the header separately, or automatically get a rect that is sized to incorporate the property and the header?

updated the question to be clearer

1 Reply

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

Answer by Bunny83 · Nov 16, 2017 at 02:55 AM

I don't quite get why you use the EditorGUI version of PropertyField instead of the EditorGUILayout version?.


Anyways, if you use EditorGUILayout.GetControlRect you have to specify the desired height. As you can read in the documentation if you don't specify the height it uses EditorGUIUtility.singleLineHeight which is simply a constant value of 16 pixels. This doesn't make much sense when using a PropertyField.


If you really want to keep using EditorGUI instead of EditorGUILayout you would have to use EditorGUI.GetPropertyHeight to get the required height for a given property. This height has to be passed to the GetControlRect method.

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 Bunny83 · Nov 16, 2017 at 03:06 AM 0
Share

ps: Why do you create a copy of the iterator every iteration? The normal usage of an iterator is to just call Next / NextVisible and reuse the iterator. You should create a copy when you do a recursion since you need the current iterator in the current state when you want to continue this iteration. You somehow have inverted this logic. You create a new iterator every iteration but keep the current when doing a recursive call.


pps: You are aware of the existence of PropertyDrawers and DecoratorDrawers? Your custom inspector doesn't seem to do anything useful. Btw: the "Header" attribute is actually a DecoratorDrawer.

avatar image bfops Bunny83 · Nov 16, 2017 at 07:31 PM 0
Share

Perfect, thanks for your help!

This is a stripped down editor that just mirrors the default one. RenderProperty is virtual so subclasses can do more interesting things, e.g. render ScriptableObjects inline or turn lists into ReorderableLists. Recursively rendering ScriptableObjects inside ReorderableLists using the the default layout manager doesn't seem to work as expected - the objects get rendered under the list ins$$anonymous$$d of inside it.

I haven't found a way to use PropertyDrawer to recursively render in ScriptableObjects, because I render them using OnInspectorGUI, which causes the PropertyDrawer to complain about layout events.

avatar image Bunny83 bfops · Nov 17, 2017 at 05:37 AM 0
Share

But i still don't get why your "RenderProperty" takes a Rect. This just introduces the same limitation as PropertyDrawers have with the addition that you can't actually define the rect size. Propertydrawers can override the GetPropertyHeight method in order to communicate how much space it needs. In your case you dictate from outside how much space the property gets. Again, why don't use just use the GUILayout version and just remove the Rect parameter?

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.

Follow this Question

Answers Answers and Comments

74 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

Related Questions

Null reference exceptions when using a Custom Editor 0 Answers

Responsive Editor UI Button with custom style | How to remove GUIStyle.hover delay 0 Answers

How can i get SerializedProperty from UnityEvent which in List. Sorry for my Eng. 2 Answers

Custom Window with Hierarchy 1 Answer

Questions Regarding Images in Custom Inspector/Editor 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