- Home /
When OnDeserialized (UnitySerializer) is meant to be called?
I'm using UnitySerializer 2.48, downloaded from here. Using JSON deserializer.
As it's documented on it's website:
Every script that is loaded can contain an OnDeserialized() function that will be called after the level has been loaded and before it starts to run. This function is exactly like the normal Unity functions and can be private or public as you wish. You would use it to do any final initialization.
From the first line, I interpret that it should be called for every object that has been potentially loaded, however, the code that seems responsible for this is:
internal static void InvokeDeserialized()
{
_suspensionCount = 0;
if (Deserialized != null)
{
Deserialized();
}
foreach (var go in UnityEngine.Object.FindObjectsOfType(typeof (GameObject)).Cast<GameObject>())
{
go.SendMessage("OnDeserialized", null, SendMessageOptions.DontRequireReceiver);
}
}
AND, the only call to this method is commented:
//Tell the world that the level has been loaded
//LevelSerializer.InvokeDeserialized();
So, not a GameObject
is noticed at all.
However, the way I interpret the self-descriptive OnDeserialize name, it makes me think that every deserialized GameObject
(those having a StoreInformation
or derived component) should get an OnDeserialized message sent to each component on it, just after it has been deserialized.
The thing is that currently I only get OnDeserialized messages when an object that has been saved/serialized is not present on the scene when is first loaded. That is, marking a GameObject
on the scene with a StoreInformation
or derived, running the game, saving game state, stop running, delete that GameObject
from scene, running again, and loading previously saved game state.
The code related to this "message broadcasting" is listed here:
//Flag that we aren't deserializing
foreach(var obj in flaggedObjects)
{
obj.IsDeserializing = false;
obj.SendMessage("OnDeserialized", SendMessageOptions.DontRequireReceiver);
}
Is the way it's currently working the proper way? Am I misunderstanding something?
In case it works as it should, how can I get notified when an Object has been deserialized? I want a way to serialize my managers, and want them to be deserialized following a kind of priority, so that my managers get deserialized after each of the other objects I want to de/serialize.
@whydoidoit there's an inconsistence between how LevelLoader and JSONLevelLoader send the OnDeserialized message. The former does send it when an object is deserialized (good), but the latter only does that when the object does not initially exist on the scene.
Answer by frarees · May 09, 2013 at 10:06 AM
I've spotted a bug that's found on UnitySerializer 2.48, causing OnDeserialized messages not to be sent properly for JSON level loader.
Add this after line 198 in JSONLevelLoader.cs:
flaggedObjects.AddRange(Data.StoredObjectNames.Select(c=>UniqueIdentifier.GetByName(c.Name)).Where(c=>c!=null).Select(c=>c.GetComponent<UniqueIdentifier>()));
Thank you so much this was very helpful and fixed the same problem i was having.
Your answer
Follow this Question
Related Questions
Fast, easy serialization library for Unity? 0 Answers
Serialize the text 2 Answers
Serialized data show information after load 0 Answers
How to serialized a variable in a c# method? 1 Answer
Unity still references file moved to different folder 1 Answer