Skip to content

Commit c2fc50e

Browse files
committed
fix: caching per property path
1 parent 9e7f397 commit c2fc50e

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

Scripts/Editor/PropertyDrawers/CollectionItemPickerPropertyDrawer.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ public class CollectionItemPickerPropertyDrawer : PropertyDrawer
1919

2020
private const string ITEMS_PROPERTY_NAME = "cachedIndirectReferences";
2121

22-
private bool initialized;
23-
private PopupList<PopupItem> popupList = new PopupList<PopupItem>();
2422
private static GUIStyle labelStyle;
2523
private static GUIStyle buttonStyle;
2624
private float buttonHeight = EditorGUIUtility.singleLineHeight;
2725
private List<ScriptableObjectCollection> possibleCollections;
2826
private List<ScriptableObject> availableItems = new List<ScriptableObject>();
2927

28+
private HashSet<string> initializedPropertiesPaths = new HashSet<string>();
29+
30+
private Dictionary<string, PopupList<PopupItem>> propertyPathToPopupList =
31+
new Dictionary<string, PopupList<PopupItem>>();
32+
3033
private readonly struct PopupItem : IPopupListItem
3134
{
3235
private readonly string name;
@@ -46,9 +49,12 @@ public override float GetPropertyHeight(SerializedProperty property, GUIContent
4649

4750
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
4851
{
52+
EditorGUI.BeginProperty(position, label, property);
53+
4954
Initialize(property);
5055

51-
EditorGUI.BeginProperty(position, label, property);
56+
PopupList<PopupItem> popupList = propertyPathToPopupList[property.propertyPath];
57+
5258
position = EditorGUI.PrefixLabel(position, label);
5359

5460
Rect totalPosition = position;
@@ -57,24 +63,24 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
5763
totalPosition.height = buttonHeight;
5864
buttonRect.height = buttonHeight;
5965

60-
// Calculate the rect for the + button
6166
float buttonWidth = 20f;
6267
Rect plusButtonRect = new Rect(position.xMax - buttonWidth, position.y, buttonWidth, buttonHeight);
6368

64-
// Adjust the total position rect to exclude the + button area
6569
totalPosition.width -= buttonWidth;
6670

6771
if (!popupList.IsOpen)
68-
SetSelectedValuesOnPopup(property);
72+
{
73+
SetSelectedValuesOnPopup(popupList, property);
74+
}
6975

7076
if (GUI.Button(totalPosition, "", buttonStyle))
7177
{
7278
EditorWindow inspectorWindow = EditorWindow.focusedWindow;
7379

7480
popupList.OnClosedEvent += () =>
7581
{
76-
GetValuesFromPopup(property);
77-
SetSelectedValuesOnPopup(property);
82+
GetValuesFromPopup(popupList, property);
83+
SetSelectedValuesOnPopup(popupList, property);
7884
};
7985
popupList.OnItemSelectedEvent += (x, y) => { inspectorWindow.Repaint(); };
8086
PopupWindow.Show(buttonRect, popupList);
@@ -156,7 +162,7 @@ private void CreatAndAddNewItems(SerializedProperty property)
156162
itemsProperty.serializedObject.ApplyModifiedProperties();
157163
}
158164

159-
private void GetValuesFromPopup(SerializedProperty property)
165+
private void GetValuesFromPopup(PopupList<PopupItem> popupList, SerializedProperty property)
160166
{
161167
SerializedProperty itemsProperty = property.FindPropertyRelative(ITEMS_PROPERTY_NAME);
162168
itemsProperty.ClearArray();
@@ -202,7 +208,7 @@ private void AssignItemGUIDToProperty(ScriptableObject scriptableObject, Seriali
202208
}
203209
}
204210

205-
private void SetSelectedValuesOnPopup(SerializedProperty property)
211+
private void SetSelectedValuesOnPopup(PopupList<PopupItem> popupList, SerializedProperty property)
206212
{
207213
popupList.DeselectAll();
208214

@@ -254,12 +260,11 @@ private LongGuid GetGUIDFromProperty(SerializedProperty property)
254260
return new LongGuid(itemGUIDValueASerializedProperty.longValue, itemGUIDValueBSerializedProperty.longValue);
255261
}
256262

257-
258263
private void Initialize(SerializedProperty property)
259264
{
260-
if (initialized)
265+
if (initializedPropertiesPaths.Contains(property.propertyPath))
261266
return;
262-
267+
263268
Type arrayOrListType = fieldInfo.FieldType.GetArrayOrListType();
264269
Type itemType = arrayOrListType ?? fieldInfo.FieldType;
265270

@@ -269,7 +274,8 @@ private void Initialize(SerializedProperty property)
269274
if (!CollectionsRegistry.Instance.TryGetCollectionsOfItemType(itemType, out possibleCollections))
270275
throw new Exception($"No collection found for item type {itemType}");
271276

272-
popupList.Clear();
277+
propertyPathToPopupList.Add(property.propertyPath, new PopupList<PopupItem>());
278+
273279
availableItems.Clear();
274280
for (int i = 0; i < possibleCollections.Count; i++)
275281
{
@@ -280,16 +286,16 @@ private void Initialize(SerializedProperty property)
280286

281287
if (scriptableObjectType != itemType && !scriptableObjectType.IsSubclassOf(itemType))
282288
continue;
283-
289+
284290
availableItems.Add(scriptableObject);
285-
popupList.AddItem(new PopupItem(scriptableObject), false);
291+
propertyPathToPopupList[property.propertyPath].AddItem(new PopupItem(scriptableObject), false);
286292
}
287293
}
288294

289295
buttonStyle = EditorStyles.textArea;
290296
GUIStyle assetLabelStyle = new GUIStyle(EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).FindStyle("AssetLabel"));
291297
labelStyle = assetLabelStyle;
292-
initialized = true;
298+
initializedPropertiesPaths.Add(property.propertyPath);
293299
}
294300
}
295301
}

0 commit comments

Comments
 (0)