Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 McGravity · Dec 05, 2015 at 09:52 AM · c#serializationinheritancepolymorphism

C# serialization with polymorphism/inheritance.

Hello,

on the one hand, I read a couple of times that a class derived from ScriptableObject can not be serialized. And indeed I experience the Exception myself which gets thrown when trying to serialize a SO.

 SerializationException: Type ScriptableObject in assembly Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null is not marked as serializable.

On the other hand I always read that inheritance/polymorphism can only be handled correctly on serialization when deriving from SO. I'm confused.

I don't get how you have to build the class structure in order to serialize objects which use inheritance. Atm my structure is like this:

 public class Model : ScriptableObject {
 }
 
 [Serializable]
 public class CreatureModel : Model {
     // A couple of primitive types to serialize.
 }
 
 [Serializable]
 public class CitizenModel : CreatureModel {
      // A couple of primitive types to serialize.
 }

This of course throws an exception that Model isn't marked as serializable. Marking it as serializable will throw the excpetion from above. There is something I don't get about this, what is it? :)

Comment
Add comment · Show 2
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 frarees · Dec 05, 2015 at 11:37 AM 0
Share

What do you actually want to achieve? Runtime serialization? ScriptableObjects will help you handle serialization so you don't have to use C# to serialize yourself. Here's a quick overview of it.

avatar image McGravity · Dec 05, 2015 at 12:07 PM 0
Share

I want to use serialization for saving and loading game states. I seperated data and logic and want to serialize the data. I don't care if my model objects are SOs or not, but I definitely need inheritance. So I read about that you need to derive from SO in order to deserialize a subclass correctly. But it's not possible to serialize a SO. What do you mean by "SOs will help you serialization..."?

1 Reply

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

Answer by Bunny83 · Dec 05, 2015 at 12:47 PM

You have to distinguish two things:

  • C# / .NET serialization

  • Unity's built-in asset serialization

The ScriptableObject class can only be used for asset serialization inside the Unity editor. "Normal" .NET serialization only works with classes which are marked as Serializable.

You can't create an instance of ScriptableObject with "new". It has similar limitations as the MonoBehaviour class. ScriptableObjects have to be created with "CreateInstance" while MonoBehaviours can only be created with AddComponent. Both can also be created by cloning an existing object with Instantiate. Asset serialization can only be done inside the Unity editor while "asset deserialization" is also done in the runtime. So you can't use Unity's serialization system to save something at runtime. You have to use other methods. Either build your own system or use .NET serialization.

With .NET serialization you can only serialize classes which can be serialized. A lot built-in types like MonoBehaviour, ScriptableObject, Mesh, .... are not serializable as they have a native counterpart on the C++ side. But also some seemingly pure managed types as Vector3 aren't marked as serializable. So you have to use wrappers for those.

The point of ScriptableObjects inside the Unity editor is to provide a way to serialize a custom class on it's own (again, only inside the editor) as seperate asset. Unity's serialization system doesn't store type information in the serialized data except for built-in types. That's why inheritance works for those. If you use a custom serializable class inside a MonoBehaviour, the fields of that custom class are serialized along with the MonoBehaviour. This serialization happens based on the field type inside the MonoBehaviour class. That's why you get an instance of the baseclass even when you stored a derived class inside the field. This feature is ment to allow a MonoBehaviour / ScriptableObject to structure it's data. References to built-in Unity types are actually serialized as asset-reference. You can only reference assets which are serialized as seperate asset. So a field that holds a ScriptableObject reference just contains an asset reference. That's why the field can have a baseclass type and the actual asset (which is stored seperately) can have a derived type.

If you want to understand Unity's system better, go to your editor project settings and switch the asset serialization to "force text". That way you can look at scene files or prefab files with a texteditor. It helps to understand how the system works. Though since you mainly seem to be interested in runtime serialization you can juse ignore everything that is related to Untiy serialization as it doesn't work at runtime. At least up until now. Maybe we get such a feature in the future.

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 McGravity · Dec 05, 2015 at 12:56 PM 0
Share

Aah great, thank you very much for clearing up that there are two types of serialization. As you said I'm only using C# runtime serialization. Thus, I guess that the problem of objects of type sublass being of type superclass after deserialization doesn't apply to my case.

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

35 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Distribute terrain in zones 3 Answers

Copy reference from one component to another derived component., 2 Answers

ScriptableObject error with Inheritance 2 Answers

How to change variables from another script? 1 Answer


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