- Home /
Multiple attributes
Hello!
I'm currently working on adding some additional attributes to Unity to display messages when I need to. The messages work nicely, and I'm able to display the original property as it's supposed to be below the message. So for example, this: However, it always shows the property as it would be by default, and doesn't seem to support multiple attributes. Take the below example... Here, I have two properties, both have the slider attribute, but only the one without a message works - the other has defaulted back to being a regular integer box! Here is my code of the properties...
[SerializeField]
[Error("This is an error message")]
private Camera cam = null;
// Movement speed
[SerializeField]
[Range(10f, 200f, order = 0)]
[Warning("This is a warning message", order = 1)]
private float rotationSpeed = 60f;
[SerializeField]
[Range(10f, 200f)]
private float zoomSpeed = 100f;
I'm using the optional order attribute because I thought if it drew the scroller first it may work, but nope. And here is my code for the property drawer...
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Begin
EditorGUI.BeginProperty(position, label, property);
// Add message
position.height = GetPropertyHeight(property, label) / NUM_ROWS;
MessageAttribute attr = attribute as MessageAttribute;
EditorGUI.HelpBox(position, attr.text, attr.messageType);
// Add original editor contents
position.y = position.y + GetPropertyHeight(property, label) / NUM_ROWS;
GUIContent newLabel = new GUIContent("");
GUIContent[] subLabels = { newLabel };
EditorGUI.MultiPropertyField(position, subLabels, property, label);
// End
EditorGUI.EndProperty();
}
I'm using MultiPropertyField as that's the simplest way I found of recreating the original inspector element. I'd really like for it to become a slider, any help is much appreciated!
I've dabbled a lot with this in the past. It's a shame after many years Unity still lack this basic editor functionality. If you want to stick with Unity's, maybe write a custom property that supports all your property needs. So it would support an info message, slider, etc. All in one attribute class, and then in your drawer, you figure what fields are being used in the attribute and draw things accordingly. Does that make sense? If you don't want to stick to Unity, check out VFW. It's old but it still works.