- Home /
Why does my Unity Serialization not work? Do i miss something?
Like my Question, what am i doing wrong? I feel like i read every post so far but really nothing helped...
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
[Serializable]
public class QuestEditor : EditorWindow
{
//[SerializeField]
//public List<QuestEditorNode> Nodes;
[SerializeField]
private int x;
void OnEnable()
{
Debug.Log("OnEnable " + x);
x = 42;
}
void OnDisable()
{
Debug.Log("OnDisable");
x = 66;
}
#region Unity Menu Tab
[MenuItem("Quest Editor/Show Window")]
public static void ShowWindow()
{
EditorWindow.GetWindow<QuestEditor>();
}
#endregion
}
i compressed it hard now. like you see in the first Line my attempt is to save a list of Nodes. But since this x saving doesn't even work, i don't care about my Nodes for now...
My Code should do nothing more to just save this x when my EditorWindow is closed. I read that this should be done by just adding [Serializable]
and [SerializeField]
to the class/variables...
As EditorWindow is destroyed and serialized data goes nowhere (i.e. no asset representation), you're not able to load data back again. Serializing stuff for EditorWindows helps as it makes them maintain values after assembly reloads. You can use EditorPrefs (I saw Unity windows use it to store window position, etc)
maybe you could try to manually serialize your stuff in OnDisable, and then deserialize in OnEnable... You should be able to use .NET serializers if your QuestEditorNode
is not a UnityEngine.Object
... protobuf-net is very fast.
Or even better, you could use a ScriptableObject
here for your data - create an asset for it and then just write/read to that asset ins$$anonymous$$d... See.
Answer by jasperstocker · Mar 09, 2014 at 03:25 PM
I thought EditorWindow instances are destroyed when they are closed. There is nothing to serialise as it doesn't exist in the scene or the project.
Maybe look into custom editors of a GameObject?
Your answer
Follow this Question
Related Questions
Editor Window Serialization not Working as Expected 2 Answers
Creating persistent object in Edit mode 1 Answer
OnSerialize event 2 Answers
Serialization depth limit 7 exceeded 1 Answer
PropertyDrawer and EditorWindow 1 Answer