11namespace TypeReferences . Editor . Drawers
22{
3+ using System . Collections . Generic ;
34 using TypeReferences ;
45 using UnityEditor ;
56 using UnityEngine ;
@@ -14,6 +15,8 @@ namespace TypeReferences.Editor.Drawers
1415 [ CustomPropertyDrawer ( typeof ( TypeOptionsAttribute ) , true ) ]
1516 public class TypeReferencePropertyDrawer : PropertyDrawer
1617 {
18+ private static readonly Dictionary < ( SerializedObject serializedObject , string path ) , string > _valuesCache = new Dictionary < ( SerializedObject serializedObject , string path ) , string > ( ) ;
19+
1720 public override float GetPropertyHeight ( SerializedProperty property , GUIContent label )
1821 {
1922 return EditorStyles . popup . CalcHeight ( GUIContent . none , 0f ) ;
@@ -48,6 +51,13 @@ private void DrawTypeReferenceField(Rect position, SerializedProperty property)
4851 serializedTypeRef . TypeNameAndAssembly = string . Empty ;
4952 }
5053
54+ // This is needed for the 'value changed' event to propagate up, so that if someones subscribes to
55+ // value changed events through UI Elements, they will receive a notification.
56+ // We can determine that the value changed only by comparing it with the previous value because the dropdown
57+ // opens a new window and when the type is changed, the focus is on the window, not on this property drawer.
58+ if ( ! PreviousCurrentValuesEqual ( serializedTypeRef . TypeNameProperty ) )
59+ GUI . changed = true ;
60+
5161 var dropdownDrawer = new TypeDropdownDrawer ( selectedType , typeOptionsAttribute , fieldInfo ? . DeclaringType ) ;
5262
5363 var fieldDrawer = new TypeFieldDrawer (
@@ -58,5 +68,24 @@ private void DrawTypeReferenceField(Rect position, SerializedProperty property)
5868
5969 fieldDrawer . Draw ( ) ;
6070 }
71+
72+ private static bool PreviousCurrentValuesEqual ( SerializedProperty typeNameProperty )
73+ {
74+ var key = ( typeNameProperty . serializedObject , typeNameProperty . propertyPath ) ;
75+
76+ if ( _valuesCache . TryGetValue ( key , out string previousValue ) )
77+ {
78+ if ( typeNameProperty . stringValue == previousValue )
79+ {
80+ return true ;
81+ }
82+
83+ _valuesCache [ key ] = typeNameProperty . stringValue ;
84+ return false ;
85+ }
86+
87+ _valuesCache . Add ( key , typeNameProperty . stringValue ) ;
88+ return true ;
89+ }
6190 }
6291}
0 commit comments