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.
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()*); // display object name*
EditorGUILayout.PropertyField(serializedObject.FindProperty(“SelectMaterial”)); // find an enum which allows the dropdown list GUILayout.EndHorizontal(); serializedObject.ApplyModifiedProperties(); } } [134376-materialselectionperobject.png|134376]* This is how it looks in Unity. When I select one child object’s material, everything else changes but the selection is supposed to be independent.
*
First of all. serializedObject points to the object(s) you currently have selected in the hierarchy. So calling EditorGUILayout.PropertyField(serializedObject.FindProperty("SelectMaterial"));a lot of times will just display a dropdown of the item you selected in the hierarchy a lot of times, it won’t show a dropdown for each of its parent’s childs. If you want this to work, you need to try and modify the other child values instead of your own
On a sidenote, I don’t think you use EditorGUILayout.Flodout properly. Because location inside if statement and the third parameter you provide don’t make a lot of sense to me.
Below is a simple example of how you could do that, without a lot of testing. So check whether this persists properly and whether Undo/Redo functionality works as intended. As persisting changes when changing scenes or restarting unity can be tricky if you are modifying objects that you don’t have selected in the hierarchy.
[CustomEditor(typeof(Example))]
public class ExampleEditor : Editor
{
private bool includeChildObj;
public override void OnInspectorGUI()
{
// Display dropdown for self
EditorGUILayout.PropertyField(serializedObject.FindProperty("SelectMaterial"));
serializedObject.ApplyModifiedProperties();
// Display dropdown for each child
Transform parent = (target as Example).transform.parent;
includeChildObj = EditorGUILayout.Foldout(includeChildObj, "List of Child Objects", true);
if (includeChildObj)
{
foreach (var child in parent.GetComponentsInChildren<Example>())
{
GUILayout.BeginHorizontal();
GUILayout.Label(child.name);
SerializedObject so = new SerializedObject(child);
EditorGUILayout.PropertyField(so.FindProperty("SelectMaterial"));
GUILayout.EndHorizontal();
so.ApplyModifiedProperties();
}
}
}
}
Naturally you can’t add CanEditMultipleObjects to this. Because we are using Target which point to just one object instead of all selected objects
If you have a different setup, where you only have this script added to the parent object and not the child objects, then this won’t work, in that case, there needs to be some place where you store the value for each child so you can edit it. In your question it isn’t exactly clear to me where that is, so I made assumptions…