简体   繁体   English

Unity3D。 使用自定义检查器实例化和显示枚举对象

[英]Unity3D. Instantiating and displaying enum objects using custom inspector

My script extracts all child objects from a parent object. 我的脚本从父对象中提取所有子对象。 I need to assign materials individually to child objects or material to all objects via parent object. 我需要将材料分别分配给子对象,或者将材料通过父对象分配给所有对象。

Currently when I change a child material, all other child object materials get changed aswell. 目前,当我更改子材质时,所有其他子对象材质也将更改。

How it looks in Unity when I change a child material 当我更改子材质时,它在Unity中的外观

Here is my code 这是我的代码

if (includeChildObj == true)
    {
        EditorGUILayout.Foldout(includeChildObj, "List of Child Objects", includeChildObj);
        for (int i = 0; i < Geometry.getChildNames().Count; i++) // loop through all child objects
        {
            GUILayout.BeginHorizontal();
            GUILayout.Label(Geometry.getChildNames()[i]); // display object name
       EditorGUILayout.PropertyField(serializedObject.FindProperty("SelectMaterial")); // find an enum which allows the dropdown list
            GUILayout.EndHorizontal();
            serializedObject.ApplyModifiedProperties();
        }              
    }

How is see it is that inside the FOR loop, for every Geometry object, I need to instantiate a new object of the corresponding enum. 可以看到在FOR循环中,对于每个Geometry对象,我都需要实例化对应枚举的新对象。 I am not sure how to do this as I am using CustomEditor not Monobehaviour. 我不确定如何执行此操作,因为我使用的是CustomEditor而不是Monobehaviour。

Thank you! 谢谢!

It appears that you are referencing the same serialized object in each iteration of your for loop. 似乎在for循环的每次迭代中都引用相同的序列化对象。 So there's only one value being read and assigned to in each iteration, which explains the behaviour you're seeing. 因此,在每次迭代中只有一个值被读取并分配给它,这解释了您所看到的行为。 It's not clear to me what your requirements are, but perhaps you need multiple instances of that serialized object, one per Geometry child? 我不清楚您的要求是什么,但也许您需要该序列化对象的多个实例,每个Geometry子对象一个?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM