x


Can a custom Inspector serialize a List<> of derived classes?

I'm creating a custom inspector for one of the monobehaviour components in my game.

This class stores a List of behaviors that derive from a base behavior. These behaviors are marked serializable, and the custom editor is wired up to show the list of these derived behaviors and their custom properties (via reflection) that each behavior exposes.

Example:

List with these in the list: NPCWanderBehavior NPCMoveToBehavior NPCIdleBehaviour

The inspector shows each subclass with their properties, and modifying them seems to save those values (at least ,when I set the values on them, and I switch between nodes or run the game, those values persist). The problem is that when I save the scene, exit, and restart, those values are gone and the list only has the base class for these as serialized.

So if I have this list of commands:

NPCWanderBehavior NPCMoveToBehavior NPCIdleBehaviour

added to a game object component in the scene through my custom editor, when I save and reload my list is:

NPCCommandBase NPCCommandBase NPCCommandBase

So it looks like even though all the classes are marked serializable, that when saved only the base class is serialized, none of the subclasses.

Is there a solution to this? The custom inspector would go a LONG way to managing the command list for the level designer, and it would be quite elegant if I could get this to work.

more ▼

asked Apr 11 at 08:22 PM

auzette\'s gravatar image

auzette
99 4 6 12

(comments are locked)
10|3000 characters needed characters left
 moderation talk

2 answers:

We use the same serializer for all Unity object types (textures,materials,gameobjects,components,audioclips), as we do for script instances. While this is great in many aspects, it falls short in some others, which you have hit. The serializer does not support polymorphism. so if you make a:

List<Animal>, and you put a dog, giraffe, and a cat in there, when that list gets serialized and deserialized, you'll end up with 3 animal objects. (assuming Animal is not abstract. if it is, you should get an error). Only fields from the animal class will be serialized during serialization, and during deserialization, the deserializer only looks at the type of the declared field (List<Animal>) when deciding what kind of objects to put in there. It does not look at the type of objects that were serialized.

Usually I work around this by:

  • coming up with a solution where I don't need to serialize these objects
  • serialize three seperate fields: List<Dog>, List<Cat>, and List<Giraffe>
  • put the logic of all three animals inside a single animal class, with an enum describing which animal it represents, and some if statements in class methods to select the correct behaviour based on the enum.
more ▼

answered Apr 11 at 09:30 PM

Lucas Meijer 1\'s gravatar image

Lucas Meijer ♦♦
6.6k 17 41 83

Thanks for the response and the alternate suggestion (which I'll try now). :)

Apr 12 at 05:14 PM auzette
(comments are locked)
10|3000 characters needed characters left
 moderation talk

Additionally, as I just discovered, you can't Serialize IList, only List, which I suppose is related to the polymorphism thing.

more ▼

answered Oct 07 at 04:46 AM

George\'s gravatar image

George
240 9 11 18

In addition, while you can derive from List, derived list classes are not serializable either :-(

Jan 28 at 09:00 PM yoyo
(comments are locked)
10|3000 characters needed characters left
 moderation talk
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x225
x96

Asked: Apr 11 at 08:22 PM

Seen: 2673 times

Last Updated: Apr 11 at 08:22 PM

powered by Qato - Enterprise Q&A