1
1
namespace TypeReferences . Editor . Drawers
2
2
{
3
+ using System . Collections . Generic ;
3
4
using TypeReferences ;
4
5
using UnityEditor ;
5
6
using UnityEngine ;
@@ -14,6 +15,8 @@ namespace TypeReferences.Editor.Drawers
14
15
[ CustomPropertyDrawer ( typeof ( TypeOptionsAttribute ) , true ) ]
15
16
public class TypeReferencePropertyDrawer : PropertyDrawer
16
17
{
18
+ private static readonly Dictionary < ( SerializedObject serializedObject , string path ) , string > _valuesCache = new Dictionary < ( SerializedObject serializedObject , string path ) , string > ( ) ;
19
+
17
20
public override float GetPropertyHeight ( SerializedProperty property , GUIContent label )
18
21
{
19
22
return EditorStyles . popup . CalcHeight ( GUIContent . none , 0f ) ;
@@ -48,6 +51,13 @@ private void DrawTypeReferenceField(Rect position, SerializedProperty property)
48
51
serializedTypeRef . TypeNameAndAssembly = string . Empty ;
49
52
}
50
53
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
+
51
61
var dropdownDrawer = new TypeDropdownDrawer ( selectedType , typeOptionsAttribute , fieldInfo ? . DeclaringType ) ;
52
62
53
63
var fieldDrawer = new TypeFieldDrawer (
@@ -58,5 +68,24 @@ private void DrawTypeReferenceField(Rect position, SerializedProperty property)
58
68
59
69
fieldDrawer . Draw ( ) ;
60
70
}
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
+ }
61
90
}
62
91
}
0 commit comments