From df091cb845ec8617b2df5119e588d52ee31702d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E5=8D=B4?= <479813005@qq.com> Date: Mon, 12 Feb 2024 13:01:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8B=96=E5=8A=A8=E5=85=83li?= =?UTF-8?q?st=E5=85=83=E7=B4=A0=E6=97=B6=EF=BC=8C=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E5=BC=95=E8=B5=B7=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NewClass/Attribute/NewButtonAttribute.cs | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/Megumin.UnityPackage/Packages/megumin.explosion4unity/Runtime/Scripts/NewClass/Attribute/NewButtonAttribute.cs b/Megumin.UnityPackage/Packages/megumin.explosion4unity/Runtime/Scripts/NewClass/Attribute/NewButtonAttribute.cs index c0ffac4..dd0b6df 100644 --- a/Megumin.UnityPackage/Packages/megumin.explosion4unity/Runtime/Scripts/NewClass/Attribute/NewButtonAttribute.cs +++ b/Megumin.UnityPackage/Packages/megumin.explosion4unity/Runtime/Scripts/NewClass/Attribute/NewButtonAttribute.cs @@ -131,10 +131,11 @@ public class SaveTask int DropMenuIndex = 0; /// - /// 为每个序列化对象的每个path缓存index,同一个path共享index。 - /// TODO,这里BUG,当list拖动改变元素顺序时,缓存导致错误。要重新选择才能正确显示。 + /// 这个缓存要是静态的,否则不生效。虽然会导致内存泄露,注意定时清除。 + /// 为每个序列化对象的每个path缓存index,同一个path共享index。 + /// 必须也使用object instance作为key的一部分,否则当list拖动改变元素顺序时,缓存导致错误。要重新选择才能正确显示。 /// - public static Dictionary<(UnityEngine.Object Target, string Path), int> SelectedIndex = new(); + public static Dictionary<(UnityEngine.Object Target, string Path, object instance), int> SelectedIndex = new(); /// /// 代码中声明的成员类型 @@ -249,8 +250,24 @@ protected void DrawReference(SerializedProperty property, GUIContent label, Rect popPosition.width += 2; popPosition.x -= 2; - var indexCacheKey = (property.serializedObject.targetObject, property.propertyPath); - SelectedIndex.TryGetValue(indexCacheKey, out var index); + var indexCacheKey = (property.serializedObject.targetObject, property.propertyPath, property.managedReferenceValue); + if (!SelectedIndex.TryGetValue(indexCacheKey, out var index)) + { + //当前对象没用缓存时,尝试匹配类型 + if (property.managedReferenceValue != null) + { + var instanceType = property.managedReferenceValue.GetType(); + for (int i = 0; i < SupportTypes.Length; i++) + { + if (instanceType == SupportTypes[i]) + { + index = i; + SelectedIndex[indexCacheKey] = i; + break; + } + } + } + } var oldSelectedIndex = index; @@ -563,7 +580,7 @@ public void CacheSupportType(SerializedProperty property) var currentType = property.managedReferenceValue?.GetType(); - var indexCacheKey = (property.serializedObject.targetObject, property.propertyPath); + var indexCacheKey = (property.serializedObject.targetObject, property.propertyPath, property.managedReferenceValue); SelectedIndex[indexCacheKey] = allTypes.Count; foreach (var item in allTypes)