Unity含派生类的List使用[SerializeReference]触发序列化错误
Unity [SerializeReference] 序列化列表操作触发空引用异常
为自定义抽象类的List添加[SerializeReference]特性后,Unity编辑器内部触发无法查看调用栈细节的空引用异常。触发条件:仅在Inspector窗口中操作该序列化列表时报错,单纯查看列表内容不会触发错误。
核心代码结构
数据容器类
[System.Serializable] public class SingleSpellManager { [SerializeReference] public List<ModifierClass> OnCast_Modifier = new List<ModifierClass>(); //[SerializeReference] do this one later //public List<ModifierClass> OnHit_Modifier = new List<ModifierClass>(); }
抽象基类与派生类
[System.Serializable] public abstract class ModifierClass { // 内部代码与错误无关 } // 派生类 [System.Serializable] public class Projectile : ModifierClass { // 内部代码与错误无关 } [System.Serializable] public class Delay : ModifierClass { // 内部代码与错误无关 }
GUI列表引用逻辑
为跨脚本操作方便,在UI管理脚本中直接引用该列表,疑似错误诱因:
public class SCC_GUI_Manager : MonoBehaviour { public void Load_ModifierList_OnCast() { // 清空面板 foreach (Transform child in ModifierList_OnCast_Space.transform) {GameObject.Destroy(child.gameObject);} // 引用目标列表 OnCast_ModifierList = SelectedSpell.OnCast_Modifier; // 加载面板元素 foreach(ModifierClass CC in OnCast_ModifierList) { GameObject newElm = Instantiate(Modifier_elm, ModifierList_OnCast_Space.transform); newElm.GetComponent<ModifierElm_UI>().SCC_GUI_Manager_scpt = this; newElm.GetComponent<ModifierElm_UI>().Modifier = CC; } } }
列表操作逻辑
移动列表元素的操作会稳定触发错误:
public class SCC_GUI_Manager : MonoBehaviour { // 将列表元素移动到新索引位置 public void Move(int oldIndex, int newIndex, int onWhatTrigger) { if(onWhatTrigger == 0) { // 执行移动 ModifierClass item = OnCast_ModifierList[oldIndex]; OnCast_ModifierList.RemoveAt(oldIndex); OnCast_ModifierList.Insert(newIndex, item); } } }
控制台错误信息
NullReferenceException: SerializedObject of SerializedProperty has been Disposed. UnityEditor.SerializedProperty.FindPropertyRelativeInternal (System.String propertyPath) (at <88872b21b1e746a7ad699974a2be8304>:0) UnityEditor.SerializedProperty.FindPropertyRelative (System.String relativePropertyPath) (at <88872b21b1e746a7ad699974a2be8304>:0) UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindPropertyRelative (UnityEngine.UIElements.IBindable field, UnityEditor.SerializedProperty parentProperty) (at <68890c016c7a40d7ab0d5ba9aecf643f>:0) UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindTree (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at <68890c016c7a40d7ab0d5ba9aecf643f>:0) UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.ContinueBinding (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at <68890c016c7a40d7ab0d5ba9aecf643f>:0) UnityEditor.UIElements.Bindings.DefaultSerializedObjectBindingImplementation+BindingRequest.Bind (UnityEngine.UIElements.VisualElement element) (at <68890c016c7a40d7ab0d5ba9aecf643f>:0) UnityEngine.UIElements.VisualTreeBindingsUpdater.Update () (at <6350d5f72ec34638985fbaab06c4c559>:0) UnityEngine.UIElements.VisualTreeUpdater.UpdateVisualTreePhase (UnityEngine.UIElements.VisualTreeUpdatePhase phase) (at <6350d5f72ec34638985fbaab06c4c559>:0) UnityEngine.UIElements.Panel.UpdateBindings () (at <6350d5f72ec34638985fbaab06c4c559>:0) UnityEngine.UIElements.UIElementsUtility.UnityEngine.UIElements.IUIElementsUtility.UpdateSchedulers () (at <6350d5f72ec34638985fbaab06c4c559>:0) UnityEngine.UIElements.UIEventRegistration.UpdateSchedulers () (at <6350d5f72ec34638985fbaab06c4c559>:0) UnityEditor.RetainedMode.UpdateSchedulers () (at <68890c016c7a40d7ab0d5ba9aecf643f>:0)
内容的提问来源于stack exchange,提问作者Gilang Agung
相关产品推荐
相关产品推荐

