- Home /
How to write Unity tests for a CustomPropertyDrawer?
Consider the below PropertyDrawer:
[CustomPropertyDrawer(typeof(TagAttribute))]
class TagPropertyDrawer: PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
property.stringValue = EditorGUI.TagField(position, label, property.stringValue);
}
}
Unity recognizes the [CustomPropertyDrawer(typeof(TagAttribute))]
and creates a [Tag]
which I have used on the tagAttribute
field below:
public class Demo : MonoBehaviour
{
[Tag] public string tagAttribute;
}
This instructs the Unity Inspector to use my TagPropertyDrawer.OnGUI
for drawing the tagAttribute
field, resulting in a Tag dropdown instead of a plain string textbox as shown below:
While this works just fine, I am not sure how do I write a Unity test case that ensures that a TagField is rendered when a [Tag]
property is used on a component's field? Is it even possible to write such a test?
I could just try and call the TagPropertyDrawer.OnGUI
function directly, but while that can test that the EditorGUI.TagField
is called when OnGUI
is called, it won't test that OnGUI is called in the first place when the component is rendered.
Your answer
![](https://koobas.hobune.stream/wayback/20220612223935im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
PropertyDrawer: Texture2D preview not showing up 0 Answers
Why does my custom property drawer's DragAndDrop add items twice? 2 Answers
Using CustomPropertyDrawer in OnGUI of ScriptableWizard 0 Answers
How to prevent drawing DecoratorDrawer (like Header or Space) during EditorGUI.PropertyField? 1 Answer