Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
7
Question by Joseph-Ferano · Dec 13, 2012 at 09:27 AM · propertydrawerserializedpropertyattributepropertycustomeditor

Draw default property types in custom PropertyDrawer

If I made a custom attribute that I wish would be applied to all properties, is there a way I can make it draw the respective EditorGUI field?

Right now, I would have to go through each property like so;

 if (property.propertyType == SerializedPropertyType.Float)
             EditorGUI.Slider (position, property, range.min, range.max, label);
         else if (property.propertyType == SerializedPropertyType.Integer)
             EditorGUI.IntSlider (position, property, range.min, range.max, label);
         else
             EditorGUI.LabelField (position, label.text, "Use Range with float or int.");



That would be pretty inconvenient to have to write all that. Any way around this?

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

Answer by Darress · Mar 05, 2015 at 11:15 AM

This is an old thread, but here is the solution.

     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         EditorGUI.PropertyField(position, property, label, true);
     }
 
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return EditorGUI.GetPropertyHeight(property);
     }

If you want to expand it's functionality with eg. a button:

     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         EditorGUI.PropertyField(position, property, label, true);
         if (property.isExpanded)
         {
             if (GUI.Button(new Rect(position.xMin + 30f, position.yMax - 20f, position.width - 30f, 20f), "button"))
             {
                 // do things
             }
         }
     }
 
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         if (property.isExpanded)
             return EditorGUI.GetPropertyHeight(property) + 20f;
         return EditorGUI.GetPropertyHeight(property);
     }


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
4

Answer by friuns3 · Feb 20, 2013 at 10:50 PM

    foreach (SerializedProperty a in property)
         {            
             position.y += 20;
             EditorGUI.PropertyField(position, a, new GUIContent(a.name));
         }
Comment
Add comment · Show 3 · 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 AmbroseFurback · Apr 01, 2013 at 07:37 PM 0
Share

I tried your solution but ended up with fields overwriting each other.

avatar image gardian06 · Sep 24, 2013 at 04:46 PM 0
Share

you need to implement a function that overwrites GetPropertyHeight() so that it is multiplied by the number of elements

avatar image AnomalusUndrdog · Mar 12, 2014 at 06:18 PM 0
Share

Why are you iterating through the property variable? Don't PropertyDrawers only draw one of the object's field/variable?

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

13 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

Related Questions

AttributeDrawer: Draw parent property? (the one [AttributeName] is attached to) 0 Answers

Getting CustomPropertyDrawer from SerializedProperty 1 Answer

alternative for header atrribute for enums 1 Answer

Draw custom Property Field in Custom Editor 1 Answer

Editor: How to do PropertyField for List elements? 4 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