当 SerializedProperty 有任何访问器时,Unity 自定义编辑器 NullReferenceException

问题描述

我正在尝试在 Unity 中为 ScriptableObject 类创建一个自定义编辑器,该类具有这样的私有字段:

ScriptableObject class

但是一旦我的一个字段有一个访问器(我也尝试过使用 get; 的属性),当我尝试在检查器中查看我的 ScriptableObject 时,我收到以下错误

NullReferenceException

我做了一些测试,它在没有访问器的情况下也能完美运行。例如,我可以看到“测试”字段。 这是我的自定义编辑器的代码

Custom editor script

有什么想法吗?我不相信不支持具有访问器的类的自定义编辑器。 谢谢!

解决方法

这与“访问器”无关。请注意 properties are not serialized by Unity


错误很简单:您的字段名称不是 ingredients 而是_ingredients

因此 FindProperty("ingredients") 返回 null,因为它没有找到任何名为 ingredients 的字段。

(当然,这是假设 Item[Serializable] 类型。)