Strange behaviour on OnBeforeSerialize (or i just don't know about that)

Okay so as far as I understood the lecture, OnBeforeSerialize is a callback when it will get executed before serialization occurs, on of the moment when serialization occurs is when we save our project (Ctrl + S).

And I’m kinda curious when it’s exactly the callback is getting called outside saving project, so I put Debug.Log in OnBeforeSerialize and I realized it happened too when hot reload.

But a strange thing happened, it happened too when I opened the dialogue editor window, and I clicked the area inside but not the node. It got called repeatedly (maybe per a half-second) but when I click the node, it stops calling. And I have no idea about that. What’s really happened? How many times and when the serialization will occur? @sampattuzzi

Even though it’s called OnBeforeSerialize, it’s called pretty much any time the editor thinks something might have changed. This can make it called by anything you do within the editor window.

If you say so then why this code

#if UNITY_EDITOR
			if (dialogueNodes.Count == 0)
			{
				CreateNode(null);
			}

			if (AssetDatabase.GetAssetPath(this) != "")
			{
				foreach (var node in dialogueNodes)
				{
					if (AssetDatabase.GetAssetPath(node) == "")
					{
						AssetDatabase.AddObjectToAsset(node, this);
					}
				}
			}
#endif

had been suggested to be placed in OnBeforeSerialized, is it good enough and much more predictable for its behaviour to be placed inside OnValidate? Seriously, I didn’t get quite a strong reason why did this code should be in OnBeforeSerialized that caused the strange behaviour as I mention on the post?

And I want to make things more clear on what happened exactly, I clicked the editor window and after then I did nothing but strangely, it keeps logging repeatedly. I didn’t even move my cursor, but it just keeps logging itself, I need a clear explanation for that cause.

This is the most relevant thing I could find.
https://answers.unity.com/questions/785557/onbeforeserialize-called-constantly.html

This topic was automatically closed after 25 hours. New replies are no longer allowed.

Privacy & Terms