I add some test code:
using System;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
public class TestBehaviour : MonoBehaviour
{
public Weapon weapon1;
[SerializeReference]
public IWeapon weapon2 = new Weapon();
}
public interface IWeapon
{
}
[Serializable]
public sealed class Weapon : IWeapon
{
public string name;
public int damage;
}
[CustomPropertyDrawer(typeof(Weapon))]
public sealed class WeaponDrawer : PropertyDrawer
{
/// <inheritdoc/>
public override VisualElement CreatePropertyGUI(
SerializedProperty property)
{
var root = new VisualElement { pickingMode = PickingMode.Ignore };
root.Add(new TextField("Name") { bindingPath = "name" });
root.Add(new IntegerField("Damage") { bindingPath = "damage" });
root.TrackPropertyValue(property, _ =>
{
UnityEngine.Debug.Log("TrackPropertyValue");
});
return root;
}
}
The ‘TestBehaviour’ has two weapon: weapon1 is normal SerializedField, and weapon2 is SerializeReference.
In the WeaponDrawer, I add a TrackPropertyValue to log information whenever any property changed.
When modify weapon1, the log will show in the console. But modify weapon2, nothing happen.
I am using the Unity 2022.2.0b8