Serializing a custom class

Hey guys,
I’m currently making a custom editor and I would like to serialize a class depending of the selected Type. How do you do that? I want to be able to edit all public variables of this class so that when I instantiate an object of type ScriptType, I can set all of it’s values to the one’s set in the inspector.

public Type scriptType; // This is the class type to be serialized

// This method gets called to draw the custom class with all of it's public variables in the inspector
private void SerializeCustomClass(Type scriptType, GUIContent label)
{
    //Do something like: Editor.SerializeProprety(scriptType);
}

Well you can use the System.Activator class to make an instance of a class based on a type.

Of course you need a manner in which you can select said type, too.

It’s great to create the class instance, but it creates it under the type System.Object. See the following lines of code to understand…

private void SerializeBlockScript(Type value, GUIContent label)
{
    var obj = System.Activator.CreateInstance(value);
    SerializedObject sObj = new SerializedObject(obj); //There is an error here since the constructor need a 'UnityEngine.Object' paramater, not a 'System.Object'
    EditorGUI.ObjectField(boxRect, sObj); //sObj needs to be a 'UnityEditor.SerializedProperty'
}

How do you serialize this System.Object?

And if you were wondering how I select the ScriptType:

public string ScriptTypeName;
public Type ScriptType
{
    get
    {
        string name = ScriptTypeName.Replace(" ", String.Empty); //Removes all the whitespaces since a script's name never has spaces
        return Type.GetType(name + ",Assembly CSharp");
    }
}

Well for one, SerializedObject requires a UnityEngine.Object in its constructor, of which you cannot create in the same manner as plain objects.

If you want a Unity Object, then that depends on the object type.

So do you want to instance a Unity object, or a plain object?

I want a plain class object, I dont want a Unity Object

Well you probably want to look at this: Unity - Scripting API: SerializedProperty.managedReferenceValue

The value probably needs to be serialised with SerializeReference, and there’s various ways to draw this such as the example.

I don’t really know much about IGMUI drawing myself, admittedly.

Your help is soooooooooo valuable!! You are a legend :sunglasses::sunglasses::sunglasses: