I already asked that question on the forum (1) but I got no answer.
I created my own asset type by deriving from ScriptableObject. Because my class contains data that Unity can’t serialize I implemented ISerializationCallbackReceiver as well. I do pretty much the same thing than in the example from the documentation.
It looks like this
public partial class Graph : ScriptableObject, ISerializationCallbackReceiver
{
// Data that Unity can't serialize because INode is an interface
private List<INode> mNodes;
// Data that Unity can serialize because Connection is a serializable struct
[SerializeField] [HideInInspector] private List<Connection> mConnections;
// For serialization only
[SerializeField] [HideInInspector] private List<SpecificNode> mSpecificNodes;
public void OnBeforeSerialize()
{
// Move the nodes from mNodes to mSpecificNodes so they can be serialized
mSpecificNodes.Clear();
foreach (INode node in mNodes)
{
SpecificNode specificNode = node as SpecificNode;
if (specificNode != null)
mSpecificNodes.Add(specificNode);
}
}
public void OnAfterDeserialize()
{
// Move the nodes back from mSpecificNodes to mNodes
mNodes.Clear();
foreach (SpecificNode node in mSpecificNodes)
mNodes.Add(node);
mSpecificNodes.Clear();
}
}
Serialization works perfectly. I made my own editor to edit my assets and within it I use Undo.RecordObject() to record the changes on the undo stack and that works perfectly as well.
What doesn’t work is that the changes I make to my asset into the OnBeforeSerialize() method are not saved. In my example above, the content of mConnections is saved, but not the content of mSpecificNodes. That mean if I quit Unity and then open my project again, I get my connections back but no my nodes (mSpecificNodes is empty).
I tried setting my object dirty with EditorUtility.SetDirty() after I modify it but it doesn’t change anything. Unity doesn’t seem to notice that anything needs to be saved.
What did I miss ?