1
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Make a custom editor! Using the EditorGUILayout.ObjectField() Something like this:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(SaveManager))]

public class SaveManagerEditor : Editor {

SaveData Data;

    public override void OnInspectorGUI(){

       Data = (SaveData)EditorGUILayout.ObjectField(Data, typeof(SaveData),true);
    }


}

Oh! Almost forgot the second question! You can save your data as ScriptableObject using this from the wiki.

Hope that helps

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .