0
\$\begingroup\$

enter image description here

So in Unity 2021.x.x I cannot change value of array item but on 2020.x.x it can change. Below the simple example to explain the problem, I can add an item of listChildren but I cannot add an item of children. Also I cannot edit string of testString

[System.Serializable]
public class Test
{
    public string testString;
    public List<GameObject> children;
}

public class BaseResources : ScriptableObject
{

}

public class SimpleTest : BaseResources
{
    public List<Test> listChildren;
}

This is my editor window

public class ResourcesEditorWindow : EditorWindow
{
    protected SerializedObject serializedObject;
    protected SerializedProperty serializedProperty;
    private BaseResources[] resources;
    protected string selectedPropertyPach;
    protected string selectedProperty;

    [MenuItem("Window/Resources")]
    static void Init()
    {
        // Get existing open window or if none, make a new one:
        EditorWindow window = GetWindow(typeof(ResourcesEditorWindow));
        window.Show();

    }

    protected virtual void OnGUI()
    {
        resources = GetAllInstances<BaseResources>();
        serializedObject = new SerializedObject(resources[0]);

        EditorGUILayout.BeginHorizontal();

        EditorGUILayout.BeginVertical("box", GUILayout.MaxWidth(150), GUILayout.ExpandHeight(true));

        DrawSliderBar(resources);

        EditorGUILayout.EndVertical();
        EditorGUILayout.BeginVertical("box", GUILayout.ExpandHeight(true));

        if (selectedProperty != null)
        {

            for (int i = 0; i < resources.Length; i++)
            {
                if (resources[i].name == selectedProperty)
                {
                    serializedObject = new SerializedObject(resources[i]);
                    serializedProperty = serializedObject.GetIterator();
                    serializedProperty.NextVisible(true);
                    DrawProperties(serializedProperty);

                }
            }
        }
        else
        {
            EditorGUILayout.LabelField("select an item from the list");
        }
        EditorGUILayout.EndVertical();
        EditorGUILayout.EndHorizontal();
        Apply();


    }

    protected void DrawProperties(SerializedProperty property)
    {
        while (property.NextVisible(false))
        {
            EditorGUILayout.PropertyField(property, true);
        }
    }


    public static T[] GetAllInstances<T>() where T : BaseResources
    {
        string[] guids = AssetDatabase.FindAssets("t:" + typeof(T).Name);
        T[] a = new T[guids.Length];
        for (int i = 0; i < guids.Length; i++)
        {
            string path = AssetDatabase.GUIDToAssetPath(guids[i]);
            a[i] = AssetDatabase.LoadAssetAtPath<T>(path);
        }

        return a;

    }



    protected void DrawSliderBar(BaseResources[] prop)
    {
        foreach (BaseResources p in prop)
        {
            if (GUILayout.Button(p.name))
            {
                selectedPropertyPach = p.name;
            }
        }

        if (!string.IsNullOrEmpty(selectedPropertyPach))
        {
            selectedProperty = selectedPropertyPach;
        }

    }

    protected void Apply()
    {
        serializedObject.ApplyModifiedProperties();
    }
}
\$\endgroup\$

0

You must log in to answer this question.

Browse other questions tagged .