- Home /
Drawing Order of DecoratorDrawer doesn't work?
Hi! I have a question about drawing order of Decorator drawer.
I've created custom PropertyAttribute and DecoratorDrawer.
DecoratorDrawer has "order" property that is used for drawing order when there are multiple DecoratorDrawer in the same field.
However, it doesn't seem to work. When there are two DecoratorDrawer "Header" and "Customed Attribute" and set them order properly, but Header priority is always highest. (Same problem is occurred "Header" and "Space")
Sample code is below.
public class URLAttribute : PropertyAttribute
{
public string m_text = string.Empty;
public string m_url = string.Empty;
public URLAttribute(string text, string url)
{
m_text = text;
m_url = url;
}
}
[CustomPropertyDrawer(typeof(URLAttribute))]
public class PropertyDrawerURL : DecoratorDrawer
{
Texture m_icon = EditorGUIUtility.IconContent("_Help").image;
public override void OnGUI(Rect position)
{
URLAttribute attr = (URLAttribute)attribute;
GUIStyle style = new GUIStyle(EditorStyles.miniButton);
style.imagePosition = ImagePosition.ImageLeft;
style.fixedWidth = 128;
style.fixedHeight = 24;
style.richText = true;
string text = attr.m_text;
text = $"<color=#80ffffff>{text}</color>";
GUIContent content = new GUIContent(text, m_icon, attr.m_url);
if (GUI.Button(position, content, style))
{
Application.OpenURL(attr.m_url);
}
}
}
public class Test : MonoBehaviour
{
[URL("Open help", "https://xxxxxxx", order = 0)]
[Header("Category0", order = 1)]
[SerializeField]
GameObject m_object = null;
// some code
}
Please teach me how to solve this problem. And I'm sorry that I'm not good at English.
Answer by Bunny83 · Feb 23, 2021 at 04:41 PM
I just copied your example into my test project and it works just as it should. Though your fixed height of 24 is more than the designated line height that is used by Unity by default (which is 16 afaik). Therefore the button may overlap with other things below it. So you may want to include the GetHeight method to specify how much height your drawer needs:
public override float GetHeight()
{
return 24f;
}
With that I get those results:
The first one has the header with a lower order value, the second one has our own URL attribute with the lower order. As you can see it works just as it should.
Note that the header attribute automatically adds some padding above. Since a header is supposed to create a new section it adds some spacing above to seperate it from what comes before. That's why in the second case there's more space between our button and the header.
So the result is: I can't really reproduce your issue. I tested this in Unity "2020.2.3f1".
Note if you want to get rid of that spacing I mentioned, you may want to create a combined "Header with help URL" decorator. That way you could even move the help button to the right and keep the header text on the left side. This may be more convenient. in some cases. The drawer could be flexible enough to ignore the header title if it's empty and behave differently.
@Bunny83 Thank you for your reply!! I think the way you pointed out about height is correct. And GetHeight() method works properly.
But order property of AttributeProperty doesn't work. Always the result is same as first screenshot you attached.
I guess the deference between yours and $$anonymous$$e is caused by Unity version. I use Unity "2019.4.16f1".
Now, I belong to the project using Unity "2019.4.16f1". And it is difficult to upgrade the version. So I'll not use multiple DecoderDrawer in same field until our team upgrade new Unity version...
I really appreciate your good useful advice!! Thank you.