TagField won’t repaint when the value of its bound SerializedProperty changed.
Unity version: 2022.3.10f1.
Code:
using System;
using System.Diagnostics;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
[Conditional("UNITY_EDITOR")]
[AttributeUsage(AttributeTargets.Field)]
public class TagPopupAttribute : PropertyAttribute { }
[CustomPropertyDrawer(typeof(TagPopupAttribute))]
internal class TagPopupPropertyDrawer : PropertyDrawer
{
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
VisualElement container = new VisualElement()
{
style = { flexDirection = FlexDirection.Row }
};
TagField tagField = new TagField()
{
style = { flexGrow = 1 }
};
tagField.BindProperty(property);
tagField.TrackPropertyValue(property, OnPropertyValueChanged);
container.Add(tagField);
Button clearButton = new Button(OnClearButtonClicked) { text = "Clear" };
container.Add(clearButton);
return container;
void OnClearButtonClicked()
{
// TODO FIXME: Property value cleared, but the tagField will not refresh until re-open its inspector
property.stringValue = string.Empty;
property.serializedObject.ApplyModifiedProperties();
tagField.MarkDirtyRepaint();
// TODO FIXME: Not work at all, even can't clear the value of the property
//tagField.value = null;
//tagField.MarkDirtyRepaint();
}
void OnPropertyValueChanged(SerializedProperty p)
{
// TODO FIXME: Not work
//tagField.SetValueWithoutNotify(p.stringValue);
//tagField.MarkDirtyRepaint();
}
}
}
public class DrawerTest : MonoBehaviour
{
[TagPopup]
public string tag;
}