Skip to content

Commit 760c923

Browse files
committed
Fix invalid child properties of edited managed references
1 parent 1f6d113 commit 760c923

File tree

5 files changed

+79
-39
lines changed

5 files changed

+79
-39
lines changed

Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerDataStorage.cs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,31 @@ namespace Toolbox.Editor.Drawers
77
/// Internal system responsible for keeping and clearing data between <see cref="UnityEditor.Editor"/>s.
88
/// This small system works only for attribute-based drawers and should be defined as a static field.
99
/// </summary>
10-
/// <typeparam name="T">Key-related object.</typeparam>
11-
/// <typeparam name="T1">Data to store.</typeparam>
12-
/// <typeparam name="T2">Any type needed for storage item creation. Pass <see cref="EventArgs.Empty"/> if no additional data is needed.</typeparam>
13-
public abstract class DrawerDataStorage<T, T1, T2> : DrawerDataStorageBase
10+
/// <typeparam name="TKey">Key-related object.</typeparam>
11+
/// <typeparam name="TData">Data to store.</typeparam>
12+
/// <typeparam name="TArgs">Any type needed for storage item creation. Pass <see cref="EventArgs.Empty"/> if no additional data is needed.</typeparam>
13+
public abstract class DrawerDataStorage<TKey, TData, TArgs> : DrawerDataStorageBase
1414
{
15-
protected readonly Dictionary<string, T1> items = new Dictionary<string, T1>();
15+
protected readonly Dictionary<string, TData> items = new Dictionary<string, TData>();
1616

17-
protected readonly Func<T, T2, T1> createMethod;
18-
protected readonly Action<T1> removeMethod;
17+
protected readonly Func<TKey, TArgs, TData> createMethod;
18+
protected readonly Action<TData> removeMethod;
1919

20-
21-
public DrawerDataStorage(Func<T, T2, T1> createMethod) : this(createMethod, null)
20+
public DrawerDataStorage(Func<TKey, TArgs, TData> createMethod) : this(createMethod, null)
2221
{ }
2322

24-
public DrawerDataStorage(Func<T, T2, T1> createMethod, Action<T1> removeMethod)
23+
public DrawerDataStorage(Func<TKey, TArgs, TData> createMethod, Action<TData> removeMethod)
2524
{
2625
this.createMethod = createMethod;
2726
this.removeMethod = removeMethod;
2827
}
2928

30-
31-
protected abstract string GetKey(T context);
32-
29+
protected abstract string GetKey(TKey context);
3330

3431
/// <summary>
3532
/// Returns and if needed creates new item.
3633
/// </summary>
37-
public virtual T1 ReturnItem(T context, T2 args)
34+
public virtual TData ReturnItem(TKey context, TArgs args)
3835
{
3936
var key = GetKey(context);
4037
if (items.TryGetValue(key, out var item))
@@ -47,12 +44,12 @@ public virtual T1 ReturnItem(T context, T2 args)
4744
}
4845
}
4946

50-
public virtual T1 CreateItem(T context, T2 args)
47+
public virtual TData CreateItem(TKey context, TArgs args)
5148
{
5249
return CreateItem(context, args, true);
5350
}
5451

55-
public virtual T1 CreateItem(T context, T2 args, bool append)
52+
public virtual TData CreateItem(TKey context, TArgs args, bool append)
5653
{
5754
var item = createMethod(context, args);
5855
if (append)
@@ -63,13 +60,13 @@ public virtual T1 CreateItem(T context, T2 args, bool append)
6360
return item;
6461
}
6562

66-
public virtual T1 AppendItem(T context, T1 item)
63+
public virtual TData AppendItem(TKey context, TData item)
6764
{
6865
var key = GetKey(context);
6966
return items[key] = item;
7067
}
7168

72-
public virtual void ClearItem(T context)
69+
public virtual void ClearItem(TKey context)
7370
{
7471
var key = GetKey(context);
7572
if (removeMethod != null)

Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerDataStorageBase.cs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
1-
using System.Collections.Generic;
2-
3-
namespace Toolbox.Editor.Drawers
1+
namespace Toolbox.Editor.Drawers
42
{
53
public abstract class DrawerDataStorageBase
64
{
7-
static DrawerDataStorageBase()
8-
{
9-
ToolboxEditorHandler.OnEditorReload += () =>
10-
{
11-
for (var i = 0; i < storages.Count; i++)
12-
{
13-
storages[i].ClearItems();
14-
}
15-
};
16-
}
17-
18-
195
protected DrawerDataStorageBase()
206
{
21-
storages.Add(this);
7+
DrawerStorageManager.AppendStorage(this);
228
}
239

2410
~DrawerDataStorageBase()
2511
{
26-
storages.Remove(this);
12+
DrawerStorageManager.RemoveStorage(this);
2713
}
2814

29-
30-
private static readonly List<DrawerDataStorageBase> storages = new List<DrawerDataStorageBase>();
31-
32-
3315
/// <summary>
3416
/// Called each time Editor is completely destroyed.
3517
/// </summary>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Toolbox.Editor.Drawers
5+
{
6+
internal static class DrawerStorageManager
7+
{
8+
static DrawerStorageManager()
9+
{
10+
ToolboxEditorHandler.OnEditorReload -= ClearStorages;
11+
ToolboxEditorHandler.OnEditorReload += ClearStorages;
12+
}
13+
14+
private static readonly List<DrawerDataStorageBase> storages = new List<DrawerDataStorageBase>();
15+
16+
internal static void ClearStorages()
17+
{
18+
ClearStorages(null);
19+
}
20+
21+
internal static void ClearStorages(Func<DrawerDataStorageBase, bool> predicate)
22+
{
23+
for (var i = 0; i < storages.Count; i++)
24+
{
25+
var storage = storages[i];
26+
if (predicate != null && !predicate(storage))
27+
{
28+
continue;
29+
}
30+
31+
storage.ClearItems();
32+
}
33+
}
34+
35+
internal static void AppendStorage(DrawerDataStorageBase storage)
36+
{
37+
storages.Add(storage);
38+
}
39+
40+
internal static bool RemoveStorage(DrawerDataStorageBase storage)
41+
{
42+
return storages.Remove(storage);
43+
}
44+
}
45+
}

Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerStorageManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ private void UpdateTypeProperty(SerializedProperty property, Type targetType, Re
7878
property.serializedObject.Update();
7979
property.managedReferenceValue = obj;
8080
property.serializedObject.ApplyModifiedProperties();
81+
82+
//NOTE: fix for invalid cached properties, e.g. changing parent's managed reference can change available children
83+
// since we cannot check if cached property is "valid" we need to clear the whole cache
84+
//TODO: reverse it and provide dedicated event when a managed property is changed through a dedicated handler
85+
DrawerStorageManager.ClearStorages();
8186
}
8287

8388
private Rect PrepareTypePropertyPosition(bool hasLabel, in Rect labelPosition, in Rect inputPosition, bool isPropertyExpanded)

0 commit comments

Comments
 (0)