Make lists duplicate last element when adding new element

Issue #480 resolved
Tim Williams created an issue

In the default Unity inspector, if you increment the size of an array all of the added elements are deep copies of the last element of the array. This would be great to see in Odin as well.

Currently I accomplish this by returning the SerializationUtility.CreateCopy() of the last element in the array in the custom add function.

It would be great if this was integrated as a default feature of the add element button.

Comments (7)

  1. Bjarke Elias
    • changed status to open

    I was surprisingly not aware that Unitys lists worked like this. Sounds like something we could add as an option which people could toggle if they preferred this behavior.

    Thanks for the suggestion!

  2. Tim Williams reporter

    No problem! Here's my custom add function which makes a list operate similarly to Unity's default behavior:

    [SerializeField, ListDrawerSettings(CustomAddFunction = "AddFooList")]
    private Foo[] fooList;
    
    private Foo AddFooList() 
    { 
        return (fooList.Length > 0 && fooList[fooList.Length - 1] != null)
                                   ? (Foo)SerializationUtility.CreateCopy(fooList[fooList.Length - 1])
                                   : new Foo();
    }
    
  3. Tim Williams reporter

    Any plan to fix this? This makes creating long lists of duplicate values very difficult with Odin lists

  4. Tor Esa Vestergaard

    This has been added with patch 2.0.17 of Odin which was just released, along with an oft-requested feature letting you set the number of elements that you want to be in a list directly, instead of having to add/remove elements manually to achieve a desired element count. You can achieve the effect via [ListDrawerSettings(AddCopiesLastElement = true)].

  5. Tor Esa Vestergaard

    I should add that if you want this to apply to all lists globally, you can make an attribute processor which adds or modifies ListDrawerSettings attributes on all lists, so that all lists have ListDrawerSettings attributes with AddCopiesLastElement set to true.

  6. Log in to comment