Skip to content

Commit b66f6e7

Browse files
authored
Merge pull request #150 from brunomikoski/bugfix/fix-picker-property-drawer
Bugfix/fix picker property drawer
2 parents 9e7f397 + 5831213 commit b66f6e7

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

CHANGELOG.MD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
## Changed
9+
- Update PickerPropertyDrawer to use PopupList from property path cache to avoid issue when rendered inside a List/Array
810

911
## [2.3.3]
1012
## Added

Scripts/Editor/PropertyDrawers/CollectionItemPickerPropertyDrawer.cs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ 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;
28-
private List<ScriptableObject> availableItems = new List<ScriptableObject>();
26+
private List<ScriptableObject> availableItems = new();
27+
28+
private readonly HashSet<string> initializedPropertiesPaths = new();
29+
30+
private readonly Dictionary<string, PopupList<PopupItem>> propertyPathToPopupList = new();
2931

3032
private readonly struct PopupItem : IPopupListItem
3133
{
@@ -46,9 +48,12 @@ public override float GetPropertyHeight(SerializedProperty property, GUIContent
4648

4749
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
4850
{
51+
EditorGUI.BeginProperty(position, label, property);
52+
4953
Initialize(property);
5054

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

5459
Rect totalPosition = position;
@@ -57,24 +62,24 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
5762
totalPosition.height = buttonHeight;
5863
buttonRect.height = buttonHeight;
5964

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

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

6770
if (!popupList.IsOpen)
68-
SetSelectedValuesOnPopup(property);
71+
{
72+
SetSelectedValuesOnPopup(popupList, property);
73+
}
6974

7075
if (GUI.Button(totalPosition, "", buttonStyle))
7176
{
7277
EditorWindow inspectorWindow = EditorWindow.focusedWindow;
7378

7479
popupList.OnClosedEvent += () =>
7580
{
76-
GetValuesFromPopup(property);
77-
SetSelectedValuesOnPopup(property);
81+
GetValuesFromPopup(popupList, property);
82+
SetSelectedValuesOnPopup(popupList, property);
7883
};
7984
popupList.OnItemSelectedEvent += (x, y) => { inspectorWindow.Repaint(); };
8085
PopupWindow.Show(buttonRect, popupList);
@@ -156,7 +161,7 @@ private void CreatAndAddNewItems(SerializedProperty property)
156161
itemsProperty.serializedObject.ApplyModifiedProperties();
157162
}
158163

159-
private void GetValuesFromPopup(SerializedProperty property)
164+
private void GetValuesFromPopup(PopupList<PopupItem> popupList, SerializedProperty property)
160165
{
161166
SerializedProperty itemsProperty = property.FindPropertyRelative(ITEMS_PROPERTY_NAME);
162167
itemsProperty.ClearArray();
@@ -202,7 +207,7 @@ private void AssignItemGUIDToProperty(ScriptableObject scriptableObject, Seriali
202207
}
203208
}
204209

205-
private void SetSelectedValuesOnPopup(SerializedProperty property)
210+
private void SetSelectedValuesOnPopup(PopupList<PopupItem> popupList, SerializedProperty property)
206211
{
207212
popupList.DeselectAll();
208213

@@ -254,12 +259,11 @@ private LongGuid GetGUIDFromProperty(SerializedProperty property)
254259
return new LongGuid(itemGUIDValueASerializedProperty.longValue, itemGUIDValueBSerializedProperty.longValue);
255260
}
256261

257-
258262
private void Initialize(SerializedProperty property)
259263
{
260-
if (initialized)
264+
if (initializedPropertiesPaths.Contains(property.propertyPath))
261265
return;
262-
266+
263267
Type arrayOrListType = fieldInfo.FieldType.GetArrayOrListType();
264268
Type itemType = arrayOrListType ?? fieldInfo.FieldType;
265269

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

272-
popupList.Clear();
276+
propertyPathToPopupList.Add(property.propertyPath, new PopupList<PopupItem>());
277+
273278
availableItems.Clear();
274279
for (int i = 0; i < possibleCollections.Count; i++)
275280
{
@@ -280,16 +285,16 @@ private void Initialize(SerializedProperty property)
280285

281286
if (scriptableObjectType != itemType && !scriptableObjectType.IsSubclassOf(itemType))
282287
continue;
283-
288+
284289
availableItems.Add(scriptableObject);
285-
popupList.AddItem(new PopupItem(scriptableObject), false);
290+
propertyPathToPopupList[property.propertyPath].AddItem(new PopupItem(scriptableObject), false);
286291
}
287292
}
288293

289294
buttonStyle = EditorStyles.textArea;
290295
GUIStyle assetLabelStyle = new GUIStyle(EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).FindStyle("AssetLabel"));
291296
labelStyle = assetLabelStyle;
292-
initialized = true;
297+
initializedPropertiesPaths.Add(property.propertyPath);
293298
}
294299
}
295300
}

0 commit comments

Comments
 (0)