Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Phobetor · Jul 26, 2019 at 02:07 PM · serializationscriptableobjectabstract

ISerializationCallbackReceiver not working on ScriptableObject

Hi! I use Unity 2019.1.8f1.

My problem is that I can't serialize a list of instances of objects that derive from an abstract class. I wrote this code to make it work:

 [Serializable]
 public abstract class MyAction { }
 
 [Serializable]
 public class MyActionA : MyAction { }
 
 [Serializable]
 public class MyActionB : MyAction { }
 
 [Serializable]
 public class MyActionsDatabase : ScriptableObject, ISerializationCallbackReceiver
 {
     [SerializeField]
     private List<MyAction> actions = new List<MyAction>();
 
     [SerializeField]
     byte[] actionsBuffer;
 
     void ISerializationCallbackReceiver.OnAfterDeserialize()
     {
         this.actions = GenericSerializer.BinarySerializer<List<MyAction>>.Deserialize(this.actionsBuffer);
     }
 
     void ISerializationCallbackReceiver.OnBeforeSerialize()
     {
         this.actionsBuffer = GenericSerializer.BinarySerializer<List<MyAction>>.Serialize(this.actions);
     }
 }


PS: GenericSerializer.BinarySerializer is a wrapper for the "BinaryFormatter" of .NET


Even though this works for MonoBehaviours, it doesn't for ScriptableObjects. I simply Add my actions to the list from an editor script and it seems ok (the buffer is serialized), then i close and reopen unity (or just change scene and return to the previous) but my buffer is empty.


The question is: does "ISerializationCallbackReceiver" work for ScriptableObjects? What could I do in order to serialize my list?


Thank you.

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

2 Replies

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

Answer by Bunny83 · Jul 26, 2019 at 02:24 PM

The ISerializationCallbackReceiver works for all serialized objects, even custom serializable classes, so not only MonoBehaviour and ScriptableObject derived classes.


Though it might depends on if you actually serialize your ScriptableObject correctly. How and where do you create the instances of your classes? Did you communicate that change to Unity? Did you use the Undo class?


Note since you serialize your class tree manually into a byte array you should remove the SerializeField attribute on your "actions " list since Unity can not serialize this properly and this will most likely cause all sorts of issues with the serializer.


ps: You don't have to mark ScriptableObject as Serializable as they are always serializable.

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 Phobetor · Jul 26, 2019 at 02:56 PM

Ok, You told me important things I didn't know!


As you suggested, I removed the "Serializable" attribute from the scriptable object, then I removed the "SerializeField" attribute from the list, but nothing changed...


Instead, I think I was not saving the "MyActionsDatabase" script from its customeditor script. Now it works well, it was my fault! xD


Thank you very much!

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

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

Related Questions

Scriptable Object List values are always null 1 Answer

Abstract classes in a ScriptableObject 1 Answer

How do I save scripts as variables in the inspector? 0 Answers

Scriptable Object's Data Gets Lost After Re-opening Unity!!! 1 Answer

ScriptableObject error with Inheritance 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