Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by TheOrangeMan · Feb 23, 2013 at 02:22 AM · serializationserialize

Properly serialize array of derived classes.

Consider the following situation:

 [System.Serializable]
 public class CustomClassBase
 {
     ...
 }
 
 [System.Serializable]
 public class CustomClassA: CustomClassBase
 {
     ...
 }
 
 [System.Serializable]
 public class CustomClassB: CustomClassBase
 {
     ...
 }
 
 public class CustomComponent: MonoBehavior
 {
     public CustomClassBase[] customClasses;
 }

If customClasses contains instances of both CustomClassA and CustomClassB, is there anyway to make sure the customClasses gets serialized properly? I've tried using Object as a base class, and I'd rather not use ScriptableObject, due to the file name restriction. If there isn't a solution, can anyone think of a way to get around the problem?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

Answer by Bunny83 · Feb 26, 2013 at 12:03 AM

I answered the same question a couple of times, however i can't find one at the moment ^^

Unfortunately Unity doesn't support inheritance in custom classes when it comes to serialization. Unity can only serialize it's own types eg, types derived from UnityEngine.Object, most native types (int, float, string, ...) and some special types like Vector2/3/4, NetworkViewID, Quaternion, ... . The only classes you can use to derive your own classes from are MonoBehaviour and ScriptableObject.

MonoBehaviours can be part of a scene serialization, so when it's attached to an object in a scene, or as part of a prefab / asset serialization. ScriptableObjects can only be serialized as assets. These types can be serialized on it's own.

Custom serializable classes (classes with the System.Serializable attribute) are serialized along with a referencing MonoBehaviour or ScriptableObject. This behaves more like a struct-relationship. The custom class becomes a part of the MonoBehaviour / ScriptableObject.

Unity serialized / deserializes those classes based on the variable type that references the class. So if your MonoBehaviour contains a List of CustomClassBase references, all instances will be serialized / deserialized as CustomClassBase.

Unity serializes custom classes into the referencing MonoBehaviour, so when you have an instance of your custom class and have two or more references in MonoBehaviour classes to this instance, you will end up with multiple seperate instances upon deserialization.

The only way to support inheritance is to use MonoBehaviours or ScriptableObjects. ScriptableObjects are usually the first choice, however they need to be saved as an asset, so there's some editor code required. MonoBehaviours can easily put on scene objects or saved along with prefabe (might also require some editor code).

Keep in mind that every UnityEngine.Object has hideFlags which are quite handy when it comes to hide certain objects or to prevent the serialization of temporal scene objects. The best example is the SceneView camera. You'll never see the camera but it's attached to an invisible GameObject in the scene. This GO and all it's components are marked with HideAndDontSave

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheOrangeMan · Feb 26, 2013 at 01:14 AM 1
Share

Thanks for the breakdown. I wish Unity provided this much information in its official documentation.

avatar image Tortuap · Sep 19, 2015 at 10:24 PM 0
Share

Thanks, as said by TheOrange$$anonymous$$an, this is cleary the kind of information we need to find in Unity official documentation. What a pity they don't work on it to include such essential piece of info.

avatar image
1

Answer by kornel-l-varga · Apr 22, 2021 at 11:22 AM

I have found a great answer (not the accepted one) here by @Ruzihm : https://answers.unity.com/questions/1247233/derived-class-serialization.html

Use the [SerializeReference] decorator on the fields you want to keep their derivate values and type.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by nventimiglia · Feb 24, 2013 at 01:43 AM

If you derive from abstract class, the class does not 'contain' the class it derives from. it 'is' the class it derives from.

Try using the [SerializeField] attribute on the customClassess property.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheOrangeMan · Feb 25, 2013 at 06:44 PM 0
Share

I've tried [SerializeField]. I think you misunderstood what I meant when I said 'contain'. I was referring to the array, not to the classes.

avatar image
0

Answer by Xarbrough · Sep 19, 2015 at 11:42 PM

The question has been answered, but Unity does provide the necessary information: Manual - Script Serialization

Blog - Serialization

There are also a couple of videos recordings from Unity developers which talk about the issue live. Just google for Unity Serialization In-Depth and related videos from Unite Conference.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

When OnDeserialized (UnitySerializer) is meant to be called? 1 Answer

"Asset Serialization Mode" - Is that only during development or runtime as well? 1 Answer

Serializer Script 3 Answers

insert more objects at once in a SerializeField,How can i insert more objects at once in a SerializeField 1 Answer

SerializedProperty with children class fields 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges