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\$

1 Answer 1

0
\$\begingroup\$

I have read somewhere that Unity move all editor UI from UGUI to UIToolkit. Which make reorderable list not supported when using OnGUI(). You can use UIToolkit to make editor and it's less codes than using UGUI.

\$\endgroup\$
2
  • \$\begingroup\$ This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review \$\endgroup\$
    – liggiorgio
    Jun 11, 2023 at 15:18
  • \$\begingroup\$ I don't think this is a fundamental limitation of the engine version, but an error in your code that's preventing the size change from being detected/serialized. Previous versions of the engine may simply have been more tolerant of this error (so you may be a victim of Hyrum's Law - depending on an observable behaviour of the engine's API that was not part of its official contract, and so subject to change between versions). \$\endgroup\$
    – DMGregory
    Jun 11, 2023 at 20:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .