Serializable System.Type, get it while it's hot!

Recently I had the displeasure to find out that Unity does not in fact serialize System.Type objects. While most people will never need to encounter this inconvenience, some of us do (mostly tool programmers who want to store System.Type and have it serialized so that it exists between runs of Unity).

As a work-around, I wrote a simple little class that can be constructed given a System.Type object and which can give you back a matching System.Type object after de-serialization. It's really very simple in that it's essentially just a convenient wrapper around some fields with the SerializeField attribute and a call to System.Type.GetType(). Nevertheless, it may come in handy for some of us, which is why I'm sharing it here.

Download link here

An example use-case would be:

You wish to store data for each component that an arbitrary GameObject has. You can't know beforehand which components it will have and you want to be able to add/remove components at any time without having to rewrite your data-keeping class, but you'd also like direct access to the System.Type objects for each component. As a solution you decide to use a dictionary-like approach where the keys are the component types themselves. You can't use System.Type because Unity won't serialize it and you will lose your data.

SerializableSystemType to the rescue! The class already does the hard work for you, all you need to do is use it and access it's SystemType property to gain access to the System.Type you love!


Hey thank you for this, I might end up using it.

Awesome, just what I wanted. Thanks!

Looks a bit overkill to serialize the Name and the AssemblyName since you only need the AssemblyQualifiedName to retrieve a Type.

Oh well, I just stumbled on this one, thanks a lot you just saved me some time.

Thanks a lot! Saved my day.

Need to add this to use as key in hashed tables like Dictionary:

    public override int GetHashCode()
        return SystemType != null ? SystemType.GetHashCode() : 0;
1 Like

I found choosing types from a drop-down list more convenient in most scenarios when you need to serialize System.Type. Moreover, this way you will be able to limit the list of classes by taking only the classes derived from one abstract class or interface. Here's an implementation of such an interface: ClassTypeReference


Thanks a lot! Works for me just fine.

1 Like

20 years later and still usefull :smile:

[quote=“Kubak_cz”, post:9, topic: 508053]
20 years later and still usefull :smile:
Well, this solution can only store “simple” types. If you’re looking for a general solution, I’ve written a SerializableType wrapper that can serialize any type, even complex nested generic types in Unity. It actually uses a simple custom binary format to encode the actual type names as well as all the generic arguments which themselfs are System.Types.

The class itself can be serialized by Unity. Though it also has the two static Read and Write methods which converts the System.Type into a byte array or vice versa.