Getting any type in Unity Inspector

Edit: Added some more detail.

I am developing a SaveManager system for my games. Basicly what it should do is;

  1. Gets the “ANY” user created Serializable class by Drag&Drop to inspector. (save data).

  2. Writes that class to a file using either BinaryFormatter or XMLSerializer.

I want to be able to use it in my future games. So it should be save data class(type) independant (like serializers). I will use it like an asset, import the SaveManager, give serialized class as parameter, and let it do the rest. But as i said, it should be able to use any user created serialized class.

So the problem is, since that “serialized class” will be written by the user in the “game project” (not in the SaveManager project), and i don’t have that “serialized” class type in my SaveManager project, i don’t know how to make the SaveManager, reference the class from inspector.

So lets say i create this class in the game project (not in the SaveManager project)

[System.Serializable]
public class SaveData
{
    public string Name = "Name";
    public int Number = 5;
}

Now when i import the SaveManager project (asset) into my “game” project, i should be able to drag and drop the script to the SaveManager inspector and let it do the rest. Since BinaryFormatter works for every Serializable type, the name of the type is actually doesn’t matter. I mean BinaryFormatter is type free as long as the type is serializable. I want to use that feature, and make the SaveManager type free too.

So in future if i want to use, say “MyNewSaveData” serialized class, “in an another game”, i want to be able to drag and drop this to SaveManager inspector and let it do the rest.

Now the thing is, if i use

public object _SaveData;

in SaveManager script, unity inspector doesn’t show it.

if i use

public Object _SaveData

it casts the user created type to an Object, and i lose the type i need.

I tried to use System.Type, but unity doesn’t show it too.

Basicly what i want to do is, get “any” serialized class as reference. Like

public SerializedClass _SaveData;

or something like

public Script _SaveData;

That reference should accept “any” serialized class. Gets it’s type, then casts it to that type.

When i use

public Object _SaveData

if i use _SaveData.GetType(), i get the type the “user created”.
I have the type, in my hands, but i can’t cast Object to that type. I don’t know how to use that type for casting.

What ever i do, it either doesn’t show up in inspector, or it casted to another type and i lose the data.

What should i do?

As you correctly say, Unity will not serialize generic objects (they will never show in inspector), it will only serialize Object-based classes and compatible [Serializable] marked classes (and even then without inheritance). And there is really no way to directly connect these, because Unity won’t be able to deserialize. Unity expects to know ahead of time what kind of classes/data it will be deserializing for non-Object classes. Casting won’t help you, because Unity won’t do any casting during deserialization and lose such type-dependent data anyway (even if you somehow “linked” the class).

If I had to do it this way (arbitrary custom classes), I would use reflection. I would reference a GameObject and let the user select a class by name (via custom editor). Then store the class name as a string. And store all compatible data in name-value pairs (field name and field value), converting/storing data to strings. And this would essentially become a (much-slower-than-Unity) serializer. But this is a lot of a convoluted work.

It sounds like you want a designer-friendly game saver/serializer. And this is probably possible with some effort. But I don’t think there are any silver bullets here.