|
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.
(comments are locked)
|
|
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:
Usually I work around this by:
Thanks for the response and the alternate suggestion (which I'll try now). :)
Apr 12 at 05:14 PM
auzette
(comments are locked)
|
|
Additionally, as I just discovered, you can't Serialize IList, only List, which I suppose is related to the polymorphism thing. 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)
|
Unity Answers has moved to a new system, and some users may have trouble logging in.