- Home /
Cannot serialize System.Type field
I've run into an issue serializing a System.Type field.
in a scriptable object, I have the following code:
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
[Serializable]
public class TypeStorage : ScriptableObject
{
[SerializeField]
public Type type;
[SerializeField]
public string typeName;
protected void OnEnable() { hideFlags = HideFlags.HideAndDontSave; }
public virtual void OnGUI()
{
if(type != null)
GUILayout.Label(type.ToString(), EditorStyles.whiteLabel);
else
GUILayout.Label("no type set");
GUILayout.Label(typeName);
}
}
the typeName field serializes properly, but the Type field gets lost on reload. Is there a way around this? I'm trying to avoid string comparisons for type verification.
Answer by Bunny83 · Sep 14, 2013 at 06:48 PM
Unity can only serialize a few types which are listed here (Serializable types) The only way you have is to use the string representation of the Type. Make sure you use the AssemblyQualifiedName.
Since the question got already bumped I like to add that i've written a System.Type wrapper some time ago. It should support any System.Type, even generic types. I've also made a serializable method info which depends on the SerializableType. It basically stores the whole type definition in a compact binary format which is then stored as byte array
ah, ok. I didn't realize there were limited serializable types. Thanks for the quick response!
Answer by numberkruncher · Oct 10, 2014 at 07:01 PM
I have created a serializable wrapper for System.Type
along with a couple of attributes and property drawer for editor interfaces:
Excellent work and really nice touch on the selection menu. However, I noticed that anything derived from UnityEngine.Object can be serialized. Therefore $$anonymous$$onoScript fields can be serialized normally. In turn $$anonymous$$onoScript instances give you access to the System.Type (ie. my$$anonymous$$onoScript.GetClass()) of a class, along with other useful info. Note that a my$$anonymous$$onoScript instance can be obtained with AssetDatabase methods (ie. AssetDatabase.LoadAssetAtPath(...)). Perhaps your menu system can be adapted to the $$anonymous$$onoScript type. Am I correct in my assessment?
@aidesigner $$anonymous$$y class allows you to select any class including from .NET assemblies or the UnityEngine/UnityEditor libraries. Not just asset representations of scripts ($$anonymous$$onoScript).
@aidesigner Also it should be pointed out that $$anonymous$$onoScript
is an editor-only class.
Thanks for your very useful utility! Do you know how I can use your ClassTypeReference/PropertyDrawer to directly display a pop-up selection in my custom editor window. Basically it would be something like below, but greetingLoggerType is not of type SerializedProperty.
ClassTypeReference greetingLoggerType;
EditorGUILayout.PropertyField(greetingLoggerType);
@aidesigner: You need to use serializedObject.FindProperty
if you want to use EditorGUILayout.PropertyField
. Believe it or not you can actually create a SerializedObject
for an editor window since it contains serializable properties!
Answer by SolidAlloy · May 09, 2020 at 04:52 PM
You can try this wrapper for newer versions of Unity since numberkruncher's one doesn't work properly in Unity 2019+: https://github.com/SolidAlloy/ClassTypeReference-for-Unity
Your answer
Follow this Question
Related Questions
pass child class to ScriptableObject.CreateInstance<> ? 1 Answer
[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer
Select custom object from a list via PopUp in custom editor for ScriptableObject 0 Answers
Serialize problem with Unity Editor, values reset on game execution! 1 Answer