Skip to content

Commit 72212e2

Browse files
committed
feat: Started setting GUI.changed to true when a different type is chosen in the dropdown so that one can register for 'value changed' events
1 parent 161324e commit 72212e2

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

Editor/Drawers/TypeReferencePropertyDrawer.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace 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
}

Editor/Util/SerializedTypeReference.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
/// </summary>
1111
internal readonly struct SerializedTypeReference
1212
{
13+
public readonly SerializedProperty TypeNameProperty;
1314
private readonly SerializedObject _parentObject;
14-
private readonly SerializedProperty _typeNameProperty;
1515
private readonly SerializedProperty _guidProperty;
1616
private readonly SerializedProperty _guidAssignmentFailedProperty;
1717

1818
public SerializedTypeReference(SerializedProperty typeReferenceProperty)
1919
{
2020
_parentObject = typeReferenceProperty.serializedObject;
21-
_typeNameProperty = typeReferenceProperty.FindPropertyRelative(nameof(TypeReference._typeNameAndAssembly));
21+
TypeNameProperty = typeReferenceProperty.FindPropertyRelative(nameof(TypeReference._typeNameAndAssembly));
2222
_guidProperty = typeReferenceProperty.FindPropertyRelative(nameof(TypeReference.GUID));
2323
_guidAssignmentFailedProperty = typeReferenceProperty.FindPropertyRelative(nameof(TypeReference.GuidAssignmentFailed));
2424

@@ -27,11 +27,11 @@ public SerializedTypeReference(SerializedProperty typeReferenceProperty)
2727

2828
public string TypeNameAndAssembly
2929
{
30-
get => _typeNameProperty.stringValue;
30+
get => TypeNameProperty.stringValue;
3131
set => SetTypeNameAndAssembly(value);
3232
}
3333

34-
public bool TypeNameHasMultipleDifferentValues => _typeNameProperty.hasMultipleDifferentValues;
34+
public bool TypeNameHasMultipleDifferentValues => TypeNameProperty.hasMultipleDifferentValues;
3535

3636
private bool GuidAssignmentFailed
3737
{
@@ -47,14 +47,14 @@ private bool GuidAssignmentFailed
4747
Justification = "The method is used by TypeFieldDrawer in C# 7")]
4848
public void SetTypeNameAndAssembly(string value)
4949
{
50-
_typeNameProperty.stringValue = value;
50+
TypeNameProperty.stringValue = value;
5151
_guidProperty.stringValue = GetClassGuidFromTypeName(value);
5252
_parentObject.ApplyModifiedProperties();
5353
}
5454

5555
public void SetType(Type type)
5656
{
57-
_typeNameProperty.stringValue = TypeReference.GetTypeNameAndAssembly(type);
57+
TypeNameProperty.stringValue = TypeReference.GetTypeNameAndAssembly(type);
5858
_guidProperty.stringValue = TypeReference.GetClassGUID(type);
5959
_parentObject.ApplyModifiedProperties();
6060
}

0 commit comments

Comments
 (0)