- Home /
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 i said in the post, SaveData or that any Serialized class will be created in the "Game Project" by the user, not in the Save$$anonymous$$anager project.
So since there is no serialized class named "SaveData" in the Save$$anonymous$$anager project, i can't reference to that.
Basicly what i want to do is, get "any" serialized class as reference. Like
public SerializedClass _SaveData;
or something like
public Script _SaveData;
So user could create any Serialized class in the game project, and drag&drop it to Save$$anonymous$$anager inspector. Save$$anonymous$$anager doesn't need to know the exact type at the beggining. Because BinaryFormatter or X$$anonymous$$L Serializer works for "any" serialized class. So should Save$$anonymous$$anager. User should be able to create a class and feed the Save$$anonymous$$anager, as long as it is Serialized.
And since when i drag&drop the serialized class to inspector, the Unity should be able to know what type it is, so i need to get that type from unity.
If i do
public Object _SaveData;
and then use
_SaveData.GetType();
i can get get that user created type. But i couldn't find how to cast (convert) Object to that type. I might be able to cast if that was an object and not "Object".
but if i use
public object _SaveData
unity doesn't show it in the inspector.
Hope i could explain it, i know its a little bit complicated.
Use Object type and convert it to object like this:
object _SaveObject = (object)_SaveData;
//or
object _SaveObject = System.Convert.ChangeType(_SaveData, _SaveData.GetType());
Answer by RudyTheDev · Dec 28, 2014 at 06:02 PM
As you correctly say, Unity will not serialize generic object
s (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.