Skip to content

Commit c2d7aaf

Browse files
committed
Fix types selection in multiple fields
1 parent 7f71b6f commit c2d7aaf

3 files changed

Lines changed: 34 additions & 23 deletions

File tree

Assets/Editor Toolbox/Editor/Drawers/Regular/SerializedTypeDrawer.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Toolbox.Editor.Drawers
1111
[CustomPropertyDrawer(typeof(SerializedType))]
1212
public sealed class SerializedTypeDrawer : PropertyDrawerBase
1313
{
14-
private static readonly TypeConstraintStandard sharedConstraint = new TypeConstraintStandard();
14+
private static readonly TypeConstraintContext sharedConstraint = new TypeConstraintStandard();
1515
private static readonly TypeAppearanceContext sharedAppearance = new TypeAppearanceContext(sharedConstraint, TypeGrouping.None, true);
1616
private static readonly TypeField typeField = new TypeField(sharedConstraint, sharedAppearance);
1717

@@ -51,9 +51,12 @@ private TypeConstraintAttribute GetDefaultConstraint()
5151
private void UpdateConstraint(TypeConstraintAttribute attribute)
5252
{
5353
sharedConstraint.ApplyTarget(attribute.AssemblyType);
54-
sharedConstraint.AllowAbstract = attribute.AllowAbstract;
55-
sharedConstraint.AllowObsolete = attribute.AllowObsolete;
56-
sharedConstraint.Settings = attribute.TypeSettings;
54+
if (sharedConstraint is TypeConstraintStandard constraint)
55+
{
56+
constraint.AllowAbstract = attribute.AllowAbstract;
57+
constraint.AllowObsolete = attribute.AllowObsolete;
58+
constraint.Settings = attribute.TypeSettings;
59+
}
5760
}
5861

5962
private void UpdateAppearance(TypeConstraintAttribute attribute)
@@ -69,8 +72,6 @@ protected override float GetPropertyHeightSafe(SerializedProperty property, GUIC
6972

7073
protected override void OnGUISafe(Rect position, SerializedProperty property, GUIContent label)
7174
{
72-
var referenceProperty = property.FindPropertyRelative("typeReference");
73-
7475
label = EditorGUI.BeginProperty(position, label, property);
7576
label = property.name != "data" ? label : GUIContent.none;
7677
position = EditorGUI.PrefixLabel(position, label);
@@ -79,14 +80,22 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
7980
var addSearchField = validAttribute.AddTextSearchField;
8081
UpdateConstraint(validAttribute);
8182
UpdateAppearance(validAttribute);
82-
typeField.OnSelect = (type) =>
83-
{
84-
referenceProperty.serializedObject.Update();
85-
referenceProperty.stringValue = SerializedType.GetReferenceValue(type);
86-
referenceProperty.serializedObject.ApplyModifiedProperties();
87-
};
83+
84+
var referenceProperty = property.FindPropertyRelative("typeReference");
8885
var activeType = SerializedType.GetReferenceType(referenceProperty.stringValue);
89-
typeField.OnGui(position, addSearchField, activeType);
86+
typeField.OnGui(position, addSearchField, (type) =>
87+
{
88+
try
89+
{
90+
referenceProperty.serializedObject.Update();
91+
referenceProperty.stringValue = SerializedType.GetReferenceValue(type);
92+
referenceProperty.serializedObject.ApplyModifiedProperties();
93+
}
94+
catch (Exception e) when (e is ArgumentNullException || e is NullReferenceException)
95+
{
96+
ToolboxEditorLog.LogWarning("Invalid attempt to update disposed property.");
97+
}
98+
}, activeType);
9099

91100
EditorGUI.EndProperty();
92101
}

Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelf/ReferencePickerAttributeDrawer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private void CreateTypeProperty(SerializedProperty property)
1818
TypeUtilities.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFullTypename, out var currentType);
1919
var position = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
2020
position = EditorGUI.IndentedRect(position);
21-
typeField.OnSelect = (type) =>
21+
typeField.OnGui(position, true, (type) =>
2222
{
2323
try
2424
{
@@ -43,8 +43,7 @@ private void CreateTypeProperty(SerializedProperty property)
4343
{
4444
ToolboxEditorLog.LogWarning("Invalid attempt to update disposed property.");
4545
}
46-
};
47-
typeField.OnGui(position, true, currentType, propertyType);
46+
}, currentType, propertyType);
4847
}
4948

5049
private void UpdateTypeProperty(SerializedProperty property, Type referenceType)

Assets/Editor Toolbox/Editor/Internal/TypeField.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ private Type RetriveSelectedType(IReadOnlyList<Type> types, int selectedIndex, b
3939
}
4040

4141

42-
public void OnGui(Rect position, bool addSearchField, Type activeType)
42+
public void OnGui(Rect position, bool addSearchField, Action<Type> onSelect)
43+
{
44+
OnGui(position, addSearchField, onSelect, null);
45+
}
46+
47+
public void OnGui(Rect position, bool addSearchField, Action<Type> onSelect, Type activeType)
4348
{
4449
var collection = TypeUtilities.GetCollection(AppearanceContext);
4550
var values = collection.Values;
@@ -53,7 +58,7 @@ public void OnGui(Rect position, bool addSearchField, Type activeType)
5358
ToolboxEditorGui.DrawSearchablePopup(position, buttonLabel, index, labels, (i) =>
5459
{
5560
var type = RetriveSelectedType(values, i, addEmptyValue);
56-
OnSelect?.Invoke(type);
61+
onSelect?.Invoke(type);
5762
});
5863
}
5964
else
@@ -65,16 +70,16 @@ public void OnGui(Rect position, bool addSearchField, Type activeType)
6570
if (EditorGUI.EndChangeCheck())
6671
{
6772
var type = RetriveSelectedType(values, index, addEmptyValue);
68-
OnSelect?.Invoke(type);
73+
onSelect?.Invoke(type);
6974
}
7075
}
7176
}
7277
}
7378

74-
public void OnGui(Rect position, bool addSearchField, Type activeType, Type parentType)
79+
public void OnGui(Rect position, bool addSearchField, Action<Type> onSelect, Type activeType, Type parentType)
7580
{
7681
ConstraintContext.ApplyTarget(parentType);
77-
OnGui(position, addSearchField, activeType);
82+
OnGui(position, addSearchField, onSelect, activeType);
7883
}
7984

8085

@@ -97,7 +102,5 @@ public TypeAppearanceContext AppearanceContext
97102
ConstraintContext = appearanceContext.Constraint;
98103
}
99104
}
100-
101-
public Action<Type> OnSelect { get; set; }
102105
}
103106
}

0 commit comments

Comments
 (0)