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);
}
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");
}
}