- Home /
How to use "SerializeObject" with an object which doesn't derive from Object?
I want to design a tool to simulate the functionality of inspector, and I have some problems. Due to the original data is not an "Unity.Object", I try to use the "ScriptableObject" to solve the problem. My code is as follows:
class MyData
{
public int propertyInt;
}
class MySerializeObject<T> : ScriptableObject
{
public T value;
}
class MySerializeTypeData : MySerializeObject<MyData>
{
}
usage:
private void Draw(object obj)
{
MySerializeTypeData mySerializeObject = ScriptableObject.CreateInstance<MySerializeTypeData>();
mySerializeObject.value = obj as MyData;
SerializedObject serializedObject = new SerializedObject(mySerializeObject);
SerializedProperty property = serializedObject.FindProperty("value.propertyInt");
EditorGUILayout.PropertyField(property);
}
It works, but when I want to make this more general, it failed. I change the value to type "object":
class MySerializeObject : ScriptableObject
{
public object value;
}
private void Draw(object obj)
{
MySerializeObject mySerializeObject = ScriptableObject.CreateInstance<MySerializeObject>();
mySerializeObject.value = obj ;
SerializedObject serializedObject = new SerializedObject(mySerializeObject);
SerializedProperty property = serializedObject.FindProperty("value.propertyInt");
EditorGUILayout.PropertyField(property);
}
I guess that unity has reflect the type "MySerializeObject " before, so it cannot recognize the specific type of "value". How should I modify my code to solve the problem, or is there any other ways to implement my requirement?
Answer by YetAnotherKen · Sep 09, 2019 at 07:44 PM
If you are trying to save data to disk you could just implement the ISerializable interface in C#. That tends to simplify things a little.
Your answer
Follow this Question
Related Questions
How do I associate my custom object for the serializedProperty objectReferenceValue 1 Answer
Scriptable Objects, how to force Include in Compile / Build without referencing it in the scene? 1 Answer
Is there any way to associate a "custom asset" with a particular file type? 1 Answer
cannot change variables on scriptableobject asset in editor 1 Answer
How should I serialize data that is also editable in the Inspector? 2 Answers