在Unity中,我决定为我的组件创建一个自定义编辑器。
该组件本身有一个我已声明为List的对象列表。

编辑器的目标是这样的:

myCustomList = serializedObject.FindProperty ("myCustomList");

问题是,当我尝试使用myCustomList来获取/设置myCustomList .objectReferenceValue = modifiedCustomList as List< MyCustomObject >的值时,它告诉我List 不能强制转换为Object。

我试图通过myCustomList =(目标作为TargetClass).myCustomList来简单地设置值,但是(当然)当我按下播放按钮时,对象实例被重置为新的新列表。

如何将列表转换为对象?或者如何使用serializedObject来获取/设置诸如List之类的数据?

最佳答案

您需要像这样遍历对象...

 SerializedProperty myCustomList = serializedObject.FindProperty ("myCustomList");

    for (int i = 0; i < myCustomList .arraySize; i++)
    {
        SerializedProperty elementProperty = myCustomList.GetArrayElementAtIndex(i);

        //Since this the object is not UnityEngine.Object you can not convert them the unity way.  The compiler can determine the type that way so.....

       MyCustomList convertedMCL = elementProperty.objectReferenceValue as System.Object as MyCustomList;
    }

由于SerializedProperty不是UnityEngine.Object,因此您无法统一转换它们。编译器无法通过这种方式确定类型。

可以在here上找到关于该主题的讨论。

10-06 05:41