diff --git a/Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerDataStorage.cs b/Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerDataStorage.cs index 467fb8e2..a52a3036 100644 --- a/Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerDataStorage.cs +++ b/Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerDataStorage.cs @@ -7,34 +7,31 @@ namespace Toolbox.Editor.Drawers /// Internal system responsible for keeping and clearing data between s. /// This small system works only for attribute-based drawers and should be defined as a static field. /// - /// Key-related object. - /// Data to store. - /// Any type needed for storage item creation. Pass if no additional data is needed. - public abstract class DrawerDataStorage : DrawerDataStorageBase + /// Key-related object. + /// Data to store. + /// Any type needed for storage item creation. Pass if no additional data is needed. + public abstract class DrawerDataStorage : DrawerDataStorageBase { - protected readonly Dictionary items = new Dictionary(); + protected readonly Dictionary items = new Dictionary(); - protected readonly Func createMethod; - protected readonly Action removeMethod; + protected readonly Func createMethod; + protected readonly Action removeMethod; - - public DrawerDataStorage(Func createMethod) : this(createMethod, null) + public DrawerDataStorage(Func createMethod) : this(createMethod, null) { } - public DrawerDataStorage(Func createMethod, Action removeMethod) + public DrawerDataStorage(Func createMethod, Action removeMethod) { this.createMethod = createMethod; this.removeMethod = removeMethod; } - - protected abstract string GetKey(T context); - + protected abstract string GetKey(TKey context); /// /// Returns and if needed creates new item. /// - public virtual T1 ReturnItem(T context, T2 args) + public virtual TData ReturnItem(TKey context, TArgs args) { var key = GetKey(context); if (items.TryGetValue(key, out var item)) @@ -47,12 +44,12 @@ public virtual T1 ReturnItem(T context, T2 args) } } - public virtual T1 CreateItem(T context, T2 args) + public virtual TData CreateItem(TKey context, TArgs args) { return CreateItem(context, args, true); } - public virtual T1 CreateItem(T context, T2 args, bool append) + public virtual TData CreateItem(TKey context, TArgs args, bool append) { var item = createMethod(context, args); if (append) @@ -63,13 +60,13 @@ public virtual T1 CreateItem(T context, T2 args, bool append) return item; } - public virtual T1 AppendItem(T context, T1 item) + public virtual TData AppendItem(TKey context, TData item) { var key = GetKey(context); return items[key] = item; } - public virtual void ClearItem(T context) + public virtual void ClearItem(TKey context) { var key = GetKey(context); if (removeMethod != null) diff --git a/Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerDataStorageBase.cs b/Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerDataStorageBase.cs index 3193004f..9e1e7d1b 100644 --- a/Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerDataStorageBase.cs +++ b/Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerDataStorageBase.cs @@ -1,35 +1,17 @@ -using System.Collections.Generic; - -namespace Toolbox.Editor.Drawers +namespace Toolbox.Editor.Drawers { public abstract class DrawerDataStorageBase { - static DrawerDataStorageBase() - { - ToolboxEditorHandler.OnEditorReload += () => - { - for (var i = 0; i < storages.Count; i++) - { - storages[i].ClearItems(); - } - }; - } - - protected DrawerDataStorageBase() { - storages.Add(this); + DrawerStorageManager.AppendStorage(this); } ~DrawerDataStorageBase() { - storages.Remove(this); + DrawerStorageManager.RemoveStorage(this); } - - private static readonly List storages = new List(); - - /// /// Called each time Editor is completely destroyed. /// diff --git a/Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerStorageManager.cs b/Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerStorageManager.cs new file mode 100644 index 00000000..a9669e9c --- /dev/null +++ b/Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerStorageManager.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; + +namespace Toolbox.Editor.Drawers +{ + internal static class DrawerStorageManager + { + static DrawerStorageManager() + { + ToolboxEditorHandler.OnEditorReload -= ClearStorages; + ToolboxEditorHandler.OnEditorReload += ClearStorages; + } + + private static readonly List storages = new List(); + + internal static void ClearStorages() + { + ClearStorages(null); + } + + internal static void ClearStorages(Func predicate) + { + for (var i = 0; i < storages.Count; i++) + { + var storage = storages[i]; + if (predicate != null && !predicate(storage)) + { + continue; + } + + storage.ClearItems(); + } + } + + internal static void AppendStorage(DrawerDataStorageBase storage) + { + storages.Add(storage); + } + + internal static bool RemoveStorage(DrawerDataStorageBase storage) + { + return storages.Remove(storage); + } + } +} diff --git a/Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerStorageManager.cs.meta b/Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerStorageManager.cs.meta new file mode 100644 index 00000000..4d5efe9e --- /dev/null +++ b/Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerStorageManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9883037c28784ae448758dde3e5c5cbd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelf/ReferencePickerAttributeDrawer.cs b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelf/ReferencePickerAttributeDrawer.cs index 6f7de470..fd7a04a6 100644 --- a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelf/ReferencePickerAttributeDrawer.cs +++ b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelf/ReferencePickerAttributeDrawer.cs @@ -78,6 +78,11 @@ private void UpdateTypeProperty(SerializedProperty property, Type targetType, Re property.serializedObject.Update(); property.managedReferenceValue = obj; property.serializedObject.ApplyModifiedProperties(); + + //NOTE: fix for invalid cached properties, e.g. changing parent's managed reference can change available children + // since we cannot check if cached property is "valid" we need to clear the whole cache + //TODO: reverse it and provide dedicated event when a managed property is changed through a dedicated handler + DrawerStorageManager.ClearStorages(); } private Rect PrepareTypePropertyPosition(bool hasLabel, in Rect labelPosition, in Rect inputPosition, bool isPropertyExpanded)