Skip to content

Commit 6fb2c93

Browse files
committed
Add confirmation box support for the ReferencePickerAttribute
1 parent 1631b60 commit 6fb2c93

File tree

4 files changed

+46
-601
lines changed

4 files changed

+46
-601
lines changed

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,41 @@ private static Type GetCurrentManagedReferenceType(SerializedProperty property,
7575

7676
private void CreateTypeProperty(SerializedProperty property, Type parentType, ReferencePickerAttribute attribute, Rect position)
7777
{
78+
var searchFieldRequired = attribute.AddTextSearchField;
79+
var confirmationRequired = attribute.AddConfirmationBox;
80+
7881
var currentType = GetCurrentManagedReferenceType(property, out var hasMixedValues);
7982
var hadMixedValues = EditorGUI.showMixedValue;
8083
EditorGUI.showMixedValue = hasMixedValues;
81-
typeField.OnGui(position, attribute.AddTextSearchField, (type) =>
84+
typeField.OnGui(position, searchFieldRequired, (type) =>
8285
{
8386
try
8487
{
85-
if (!property.serializedObject.isEditingMultipleObjects)
88+
var isOperationAllowed = true;
89+
if (confirmationRequired)
8690
{
87-
UpdateTypeProperty(property, type, attribute);
91+
isOperationAllowed = EditorUtility.DisplayDialog("Confirm Assignment",
92+
"Are you sure you want to assign a new reference? This action will replace the current one.",
93+
"Assign",
94+
"Cancel");
8895
}
89-
else
96+
97+
if (isOperationAllowed)
9098
{
91-
var targets = property.serializedObject.targetObjects;
92-
foreach (var target in targets)
99+
if (!property.serializedObject.isEditingMultipleObjects)
100+
{
101+
UpdateTypeProperty(property, type, attribute);
102+
}
103+
else
93104
{
94-
using (var so = new SerializedObject(target))
105+
var targets = property.serializedObject.targetObjects;
106+
foreach (var target in targets)
95107
{
96-
var sp = so.FindProperty(property.propertyPath);
97-
UpdateTypeProperty(sp, type, attribute);
108+
using (var so = new SerializedObject(target))
109+
{
110+
var sp = so.FindProperty(property.propertyPath);
111+
UpdateTypeProperty(sp, type, attribute);
112+
}
98113
}
99114
}
100115
}

Assets/Editor Toolbox/Runtime/Attributes/Property/Toolbox/PropertySelfAttributes/ReferencePickerAttribute.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public ReferencePickerAttribute(Type parentType, TypeGrouping typeGrouping)
4242
/// Indicates if created popup menu should have an additional search field.
4343
/// </summary>
4444
public bool AddTextSearchField { get; set; } = true;
45+
46+
/// <summary>
47+
/// Indicates if confirmation box should appear after picking a new reference.
48+
/// </summary>
49+
public bool AddConfirmationBox { get; set; }
4550
}
4651
}
4752
#endif

0 commit comments

Comments
 (0)