- Home /
Using System.Object in ScriptableObject
Is there any way to use a System.Object class in a class which extends ScriptableObject for use in AssetDatabase.CreateAsset? Try as I might, unity does not want to serialize the System.Object and anything with an System.Object type gets thrown away on serialization.
Answer by rutter · Apr 12, 2012 at 06:59 PM
All classes inherit from System.Object
(usually indirectly).
You can certainly create a class which inherits directly from System.Object
or UnityEngine.ScriptableObject
, but not both at once (you can only directly inherit from one class). I'm not sure if that's the question you're asking, though.
Ignoring questions of namespace and scope, an object can certainly contain references to objects of any other type. That says nothing about whether or not those references will be serialized, of course, which seems to be what you're after.
I've had some luck getting Unity to serialize custom classes like so:
Create some class which inherits from
MonoBehaviour
Create some data class which doesn't explicitly inherit from anything, and give the class the
System.Serializable
attributeYour behaviour script should contain a list of data nodes, exposed to the inspector (`public`,
SerializeField
, etc)Your behaviour script should only use
new
to create the list if it doesn't already exist (from serialization)
I assume it's possible to do more than that, but I haven't had many chances to explore this in depth. ;)
One important caveat with Unity serialization: it is not polymorphic. If the list you're serializing has mixed types, Unity will serialize every node as the list's generic type.
In my experience, this is one of the trickier topics with Unity dev. You're sort of "going off the map".
"Off the map" is the perfect explanation of where I am currently.
For some reason, though I can serialize things like ints, and even gameObjects, I can't serialize a System.Object, and since I don't have the type until runtime and it can be different each time it has to be a System.Object.
Thanks for the response. If I explicitly strongly type my fields, such as a data type containing strings and ints etc. Unity happily serializes everything, and I can perform an AssetDatabase.CreateAsset, and LoadAssetAtPath to restore that object later. Unfortunately, this topic isn't quite so simple. I am using reflection to pull the data out of all public properties of a script which extends monobehaviour at runtime, so it has to be absolutely dynamic, as there is no telling what public properties the other developers will add. Hence I must create an array of name value pairs, and the value has to be an object. What is weirder, is that Unity will chug along just fine on datatypes that are not an object, it absolutely will not serialize something with a data type of object.
Hmm. Tricky.
If you're willing to do some legwork, Unity should be albe to serialize an array of bytes (whether on an object or to some file), which you could read using BitConverter to restore state from your objects. I've considered using that method a few times, but never actually gone far enough down the rabbit hole to need it.
Unfortunately I have pseudo solved this problem and ran into another. Since unity won't serialize System.Objects and BinaryFormatter won't serialize UnityEngine.Objects, I am doing two serializations: one for the base data types, and one for the Unity data types: ($$anonymous$$onoBehaviours, GameObjects etc) and I ran into your polymorphism caveat. Now that the array that is to be serialized through Unity is made of UnityEngine.Objects, Unity can serialize them but when they come out they have lost all semblance of originating type. Any suggestions for that?
Your answer
Follow this Question
Related Questions
Serialization issue with ScriptableObject 0 Answers
Saving nested data with AssetDatabase.CreateAsset 1 Answer
Serializing a collection of ScriptableObjects to an ASSET file? 1 Answer
Creating ScriptableObject Asset "Failed to write meta file" 1 Answer
Unity loses association to ScriptableObject after exiting playmode 0 Answers