• 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 xarismax · Nov 09, 2017 at 07:10 PM · editorinspectorpropertydrawerhealth

Property Drawers In Serializable Classes - GetPropertyHeight

I love how unity allows us to specify PropertyDrawers in Serializable classes. For Example:

 [Serializable] public class CustomClass
 {
       [MyPropertyDrawer] public int A;
 }

This works 100% and allows me to move part of editor validation logic and editor callbacks into the serializable classes instead the MonoBehaviour, resulting in more reusable code.

 public class MonoDrawer : MonoBehaviour
 {
     public CustomClass TestA; //works
     //implied usage of MyPropertyDrawer
     //for field TestA.A
 }

Now imagine the following scenario, i have two [MyPropertyDrawer] nested as seen here.

 public class StackDrawer : MonoBehaviour
 {
     [MyPropertyDrawer]
     public CustomClass TestA;
     //+1 implied from CustomClass
 }

Behaviour:

1) All property drawers and validation logic 100% works even though it is nested.

2) Bug: The Nested Property Drawers have wrong GetPropertyHeight(). I am using the default one. I think this must be an unintended effect from me "abusing" unity code in ways the designers didn't foresee.

So my question is if there is a workaround for this issue. Since [MyPropertyDrawer] could be in any depth, nested unlimited times, it would be hard if not impossible to predict the final GetPropertyHeight(). Maybe using "static global variables" as a hack ?

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
2
Best Answer

Answer by Adam-Mechtley · Nov 09, 2017 at 07:24 PM

There's no bug here. If you have some custom property drawer that is intended to work with some type that has child properties (e.g., a custom struct or class) then you just need to implement GetPropertyHeight() correctly, as the default just returns a single line height. You may find my answer here helpful, but in short, it should suffice to simply use EditorGUI.GetPropertyHeight() inside your GetPropertyHeight() implementation.

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 Bunny83 · Nov 09, 2017 at 08:16 PM 0
Share

Ahh, damn, waited too long to submit my answer ^^.

I always forget this hidden PropertyDrawer stack inside the "propertyDrawer" property of the PropertyHandler. I still think this is a bad implementation that a line like this:

 if (this.propertyDrawer != null)

will evaluate to true one time and to false another time, even though we use exactly the same objects every time.

So yes, you can use GetPropertyHeight inside your GetPropertyHeight method ^^.

avatar image
0

Answer by Bunny83 · Nov 09, 2017 at 07:46 PM

I don't quite understand your question. A property drawer is responsible for drawing "the one" property which it is related to. So if you want your property drawer to support custom classes you have to handle them properly. How is your PropertyDrawer actually implemented?

The "default" propertydrawer GetHeight method is implemented like this:

 public virtual float GetPropertyHeight(SerializedProperty property, GUIContent label)
 {
     return 16f;
 }

Of course you have to specify which height your property drawer requires. That means if you what to use it on a custom class and want that custom class to be displayed "normally" you have to calculate the required height inside GetPropertyHeight.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

99 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

Related Questions

Custom PropertyDrawer in Array. Problem with expanding. 0 Answers

How To Draw a PropertyDrawer within a PropertyDrawer 0 Answers

Customizing Inspector variables. 2 Answers

How do I implement Draggable Properties with custom Labels in Editor or PropertyDrawer? 1 Answer

How to assign a field within a PropertyDrawer? 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