Unity中PotionEffect类出现Unsupported type错误的原因排查
Unity编辑器中“Unsupported type PotionEffect”错误原因解析
问题场景
我有一个标记了[Serializable]特性的PotionEffect类,需要在Unity编辑器及运行时编辑其参数。但每次修改枚举、整数或布尔值时,都会弹出“Unsupported type PotionEffect”错误,该错误不影响项目运行,但想了解其产生原因。
错误信息
Unsupported type PotionEffect UnityEngine.StackTraceUtility:ExtractStackTrace () UnityEditorInternal.ReorderableList/Defaults:DrawElement (UnityEngine.Rect,UnityEditor.SerializedProperty,object,bool,bool,bool,bool) UnityEditorInternal.ReorderableList:DoListElements (UnityEngine.Rect,UnityEngine.Rect) UnityEditorInternal.ReorderableList:DoList (UnityEngine.Rect,UnityEngine.Rect) UnityEditorInternal.ReorderableListWrapper:Draw (UnityEngine.GUIContent,UnityEngine.Rect,UnityEngine.Rect,bool) UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
同时会伴随一条相同的错误提示。
PotionEffect类代码
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.Linq; [Serializable] public class PotionEffect { public enum EffectTypes { none, stamina, strength, stun }; public EffectTypes effect; public int duration; public bool started; public PotionEffect(EffectTypes e, int d) { this.duration = d; this.effect = e; } public PotionEffect(EffectTypes e) { this.duration = 15; this.effect = e; } public IEnumerator StartEffect() { if (started) yield break; started = true; yield return new WaitForSeconds(duration); effect = EffectTypes.none; } }
使用环境与尝试过的操作
- 环境:Unity 2020.3.17f1、Visual Studio 2019、Windows 10
- 尝试操作:将类移至其他文件、嵌套在其他类中、重启编辑器均无效;重新挂载组件后错误暂时消失,但不确定是否复发。
错误原因
缺少无参构造函数:
Unity的序列化系统及编辑器内置的ReorderableList默认绘制逻辑,需要自定义序列化类具备无参构造函数来临时实例化对象,用于预览或编辑元素。你的PotionEffect类仅定义了带参数的构造函数,没有显式的无参构造函数,导致编辑器在尝试创建临时实例时识别失败,抛出类型不支持的错误。ReorderableList版本兼容性问题:
在Unity 2020.3.x这类较旧的LTS版本中,ReorderableList对包含嵌套枚举的自定义序列化类,在绘制时存在类型解析的小bug。虽然不影响功能运行,但会触发错误提示。重新挂载组件暂时生效的原因:
重新挂载组件后,列表中的PotionEffect实例是已序列化保存的对象,编辑器不需要临时创建新实例来绘制,因此错误暂时消失;但当你修改参数触发编辑器重新绘制列表元素时,编辑器会再次尝试创建临时对象,错误就会复发。
内容的提问来源于stack exchange,提问作者GeoStorm81
相关产品推荐
相关产品推荐

