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
0
Question by edwardrowe · Apr 05, 2015 at 09:15 PM · guieditorheader

Header attribute and Custom Property Drawer overlap in inspector, repeat header text

I've created my own custom PropertyDrawer for an attribute - I simply want it to display the same label only with an '*' preceding it. I think I've built it correctly, but when I use this attribute in conjunction with the [Header] DecoratorDrawer attribute, my property is sized strangely in the inspector and gets the Header text twice.

Edit Here is sample code of the two attributes used together:

  public class TestAsterixLabel : MonoBehaviour
      {
          [Header ("Header")]
          [AsterixLabelAttribute]
          public GameObject FieldWithAttributes;
      }

Has anyone else seen this? I've output my label text in the OnGui and it's simply "Custom Property Drawer" (no Header text in there at all). Is this a Unity bug?

https://www.dropbox.com/s/b5rxtltyr5smfhy/Screenshot%202015-04-05%2017.06.30.png?dl=0 overlapping drawers

Edit I've actually stripped out everything on my attribute drawer, and the problem still exists. Also note this does seem to have to do with the implementation of GetPropertyHeight, either. Or the order of the two attributes on the field.

 CustomPropertyDrawer(typeof(RequireWireAttribute))]
 public class RequireWireDrawer : PropertyDrawer
 {
     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
     {
         EditorGUI.PropertyField(position, property, label);
     }
 }



headerdrawer.png (11.3 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
1

Answer by DoTA_KAMIKADzE · Apr 06, 2015 at 01:03 PM

Do you utilize those at start and end of your property drawer?:

EditorGUI.BeginProperty

EditorGUI.EndProperty

If I understood you right - then not, but you should to.

P.S. Here is an updated answer with a "basic" example how to use those^ above:

     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         EditorGUI.BeginProperty(position, label, property);
         EditorGUI.PropertyField(position, property, new GUIContent("*" + label.text));
         EditorGUI.EndProperty();
     }

I've even tested it on my end just to make sure that it does work, and it does.

P.P.S. Here is an update that explains how to do it with Color:

1) In your code that will be shown in Inspector somewhere:

 [Header("I'm header")]
 public Color colWithHead;
 public Color colWithoutHead;

2.1) Where you add your Custom drawers:

 [CustomPropertyDrawer(typeof(HeaderAttribute))]
 public class HColorDraw : PropertyDrawer
 {
     public static GUIStyle HeaderStyle
     {
         get
         {
             GUIStyle labelStyle = new GUIStyle(EditorStyles.label);
             labelStyle.fontStyle = FontStyle.Bold;
             labelStyle.alignment = TextAnchor.UpperCenter;
             labelStyle.fontSize = 14;
             labelStyle.normal.textColor = new Color32(255, 0, 0, 255);
             return labelStyle;
         }
     }
     HeaderAttribute headAttribute { get { return ((HeaderAttribute)attribute); } }
     private const float headHeight = 16f;
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         if (property.propertyType == SerializedPropertyType.Color)
         {
             EditorGUI.BeginProperty(position, label, property);
             string labTxt = "*" + label.text;
             EditorGUI.LabelField(position, headAttribute.header, HeaderStyle);
             position.yMin += headHeight;
             property.colorValue =EditorGUI.ColorField(position, labTxt, property.colorValue);
             EditorGUI.EndProperty();
         }
         else
         {
             //...
         }
     }
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         if (property.propertyType == SerializedPropertyType.Color)
         {
             return base.GetPropertyHeight(property, label) + headHeight;
         }
         else return base.GetPropertyHeight(property, label);
     }
 }
 [CustomPropertyDrawer(typeof(Color))]
 public class ColorDraw : PropertyDrawer
 {
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         EditorGUI.BeginProperty(position, label, property);
         EditorGUI.PropertyField(position, property, new GUIContent("*" +label.text));
         EditorGUI.EndProperty();
     }
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return base.GetPropertyHeight(property, label);
     }
 }

And that results in:

alt text

2.2) Alternatively you could just draw yourself everything, like I have shown with just Header and that will work with default HeaderDrawer:

 [CustomPropertyDrawer(typeof(Color))]
 public class ColorDraw : PropertyDrawer
 {
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         EditorGUI.BeginProperty(position, label, property);
         property.colorValue = EditorGUI.ColorField(position, "*" + label.text, property.colorValue);
         EditorGUI.EndProperty();
     }
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return base.GetPropertyHeight(property, label);
     }
 }

alt text

P.P.P.S. Added color saving for the code above^ just for the sake of completeness. If I've missed something let me know, that kinda lots of coding over here ))

Comment
Add comment · Show 8 · 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 edwardrowe · Apr 06, 2015 at 01:59 PM 0
Share

I did on my original drawer (before stripping it down to this line of code) but it doesn't seem to make a difference whether I add it or not, as far as this bug goes.

avatar image DoTA_KAMIKADzE · Apr 06, 2015 at 02:29 PM 0
Share

You're doing something wrong for sure, I'll update my answer with a "basic" example how to add asterisk in few $$anonymous$$.

avatar image edwardrowe · Apr 06, 2015 at 02:47 PM 0
Share

I appreciate you taking time to help - did you try that on a field that also has a [Header ("TestHeader")] attribute? It still has the overlap issue for me. I'm using Unity 5.0.0f4 (latest)

https://www.dropbox.com/s/5h192jg8t0wgbzl/Screenshot%202015-04-06%2010.47.33.png?dl=0

alt text

screenshot-2015-04-06-104733.png (6.4 kB)
avatar image DoTA_KAMIKADzE · Apr 06, 2015 at 05:05 PM 0
Share

You should make a [CustomPropertyDrawer(typeof(HeaderAttribute))]

But if you're ok with duplication then you can do something like this:

 public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
 {
     return base.GetPropertyHeight(property, label) + 24f;//24f is the height of default HeaderDrawer, if you have a custom DecoratorDrawer for header then use its height
 }

P.S. Just in case you didn't know if you'll go with HeaderAttritube property drawer:

You can get the type of "current field" like that:

     if (property.propertyType == SerializedPropertyType.String) Debug.Log("I'm string");
     //or like that:
     switch (property.propertyType)
     {
         case SerializedPropertyType.Integer:
             {
                 Debug.Log("I'm int");
                 break;
             }
     }
avatar image edwardrowe · Apr 06, 2015 at 05:35 PM 0
Share

Are you getting the correct results from that code? This is all of my script - it's not much but it's a nice example of the issue in action:

// Tester class

     public class TestAsterixLabel : $$anonymous$$onoBehaviour
     {
         [Header ("Header")]
         [AsterixLabelAttribute]
         public GameObject FieldWithAttributes;
     }

// Attribute

     [System.AttributeUsage (System.AttributeTargets.Field)]
     public class AsterixLabelAttribute : PropertyAttribute {
     }

//Custom Drawer

     [CustomPropertyDrawer(typeof(AsterixLabelAttribute))]
     public class AsterixLabelDrawer : PropertyDrawer
     {
         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
         {
             EditorGUI.BeginProperty(position, label, property);
             EditorGUI.PropertyField(position, property, new GUIContent("*" + label.text));
             EditorGUI.EndProperty();
         }
     }
Show more comments
avatar image
0

Answer by edwardrowe · Apr 08, 2015 at 03:41 PM

Ok I've figured this out. And it was kind of dumb.

So the problem is that I am using EditorGUI.PropertyField and passing in the very property that I'm trying to display. This must in turn re-call the decorators.

When I switched this to EdtiorGUI.ObjectField (which is the type I'm ultimately looking for), it works fine. Thanks for everyone's help.

Comment
Add comment · Show 2 · 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 DoTA_KAMIKADzE · Apr 08, 2015 at 04:27 PM 0
Share

That's the 2.2 way in my answer )) also don't forget to return the value to the property itself (that is also shown in my 2.2 code example).

avatar image edwardrowe · Apr 08, 2015 at 04:39 PM 0
Share

Ah I see that now. The signficance of the EditorGUI.PropertyField function was just lost on me and clouded the way I read your response.

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

20 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

Related Questions

Unity editor GUI, how to prevent "passthrough"/"clickthrough" etc? 0 Answers

Handling Undo/REdo in EditorWindow when editing DB record. 0 Answers

Get Mouse Events with EditorWindow 3 Answers

Add EditorUI elements when a button is pressed 0 Answers

EditorGUI like light explorer window 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