如何将 List<T> 转换为对象

Cre*_*gic 5 c# unity-game-engine

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

编辑器的目标是这样的:

myCustomList = serializedObject.FindProperty ("myCustomList");
Run Code Online (Sandbox Code Playgroud)

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

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

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

Ray*_*ner 3

您需要像这样迭代该对象......

 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;
    }
Run Code Online (Sandbox Code Playgroud)

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

可以在此处找到有关该主题的讨论。