- Home /
How should I serialize data that is also editable in the Inspector?
I have successfully achieved both of the requirements in isolation:
Using a `BinaryFormatter` to serialize an arbitrary object, converting it to Base64, and storing it in the cloud. I can later download, convert to byte array, deserialize and cast to my type.
Implementing a custom type that extends `ScriptableObject` that is fully editable in custom Inspector. Values are saved with the Scene, and generally working great. (Thanks to the Serialization Best Practices Megapost)
My problem is that I would like to take an instance of this `Class` that extends `ScriptableObject` and pass it through my serializer to store online. However I get the error that `ScriptableObject` is not marked as serializable.
SerializationException: Type UnityEngine.ScriptableObject in assembly UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null is not marked as serializable.
Has anyone come up with a method / pattern to achieve this functionality? Should I forgo the Unity serialization entirely and use my mechanism directly from the Editor? It seems like a very ugly solution to me, but its the only solution I can think of at the moment.
Thanks in advance
Can you post the code you're using? And the full error?
Answer by vexe · Apr 21, 2014 at 09:41 AM
Edit: See VFW for a better/advanced serialization system
The thing is, you can't use BinaryFormatter
for UnityEngine.Object
s because they're not mocked with Serializable
which is required by BinaryFormatter
. I am also seeking optimal solutions for this - The problem is, even if say, you use an annotation-free serializer (like Migrant for example) you're not guaranteed to get the object back safely upon deserialization - Because, for example let's talk about Components, why doesn't unity allow us to create them with the new
operator but only with AddComponent
? obviously, because Unity does some prepping/internal home cooking to build the component, things that not any serializer could figure out on its own.
The best way I found to serialize UnityEngine.Objects
is to leave their serialization to unity itself, don't bother.
If you want to serialize them to a readable format upload them to some server and download later, you could check out @whydoidoit's awesome Unity Serializer.
Answer by frarees · Apr 01, 2014 at 08:25 AM
A custom non-generic class marked with [System.Serializable]
will be serialized by Unity, and you can use a BinaryFormatter
for runtime serialization.
E.g.
Player.cs
[System.Serializable]
public class Player {
public string name;
public int level;
[SerializeField]
int hp;
}
...
public class MyScript : MonoBehaviour {
public Player player;
}
OP's problem seem to be the same as $$anonymous$$e: An object derived from ScriptableObject which is saved as an asset and thereby editable in the inspector, will not serialize even though it is marked as [Serializable]. Attempting to serialize the object gives the error that OP describes in his post.
Your answer
Follow this Question
Related Questions
[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer
Why doesn't my ScriptableObject save using a custom EditorWindow? 3 Answers
How to make a serialized editor only field 2 Answers
Select a readonly value from dropdown using PropertyDrawers 0 Answers
Custom Inspector; adding to list not getting saved 2 Answers