Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Custom inspector with a list containing multiple types that inherit from an abstract class

Discussion in 'Scripting' started by Afropenguinn, Dec 24, 2019.

  1. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    I have several classes which inherit an abstract class. I have a list of type 'List<AbstractClass>' that can contain any of these classes that inherit 'AbstractClass'. Works great, but I'm having some issues building a reorderable list for it in the inspector. For one, FindProperty doesn't seem to want to find a list with an abstract type, and returns null. Even if it did, I'm not sure how to check which type the current element is. There's always just accessing the element directly, instead of using Unity's serialization system, but I want it to work with Undo/Redo. The last problem is adding new elements to the list. Normally you'd just add 1 to the SerializedProperty's array size and then set the default values, but that copies the last element in the list, where as I want multiple types. I was going to have a dropdown when adding where you select which type you would like to add.

    Both the abstract class and all classes that inherit from it are serializable, all fields within them that need to be saved in the editor are also serializable.

    Anyone have any tips or resources to look at on how to handle this?
     
  2. bszalapski

    bszalapski

    Joined:
    Feb 21, 2020
    Posts:
    4
    Did you find a way to do this?