I’m trying to get to the bottom of some strange behavior inside a custom serialization callback. Here’s my test class:
using UnityEditor;
using UnityEngine;
public class SerializationCallbackTest : MonoBehaviour, ISerializationCallbackReceiver {
[SerializeField] private string _prefabAssetType = "default";
[SerializeField] private string _prefabInstanceStatus = "default";
public void OnBeforeSerialize() {
#if UNITY_EDITOR
_prefabAssetType = PrefabUtility.GetPrefabAssetType(this).ToString();
_prefabInstanceStatus = PrefabUtility.GetPrefabInstanceStatus(this).ToString();
#endif
}
public void OnAfterDeserialize() {
}
}
Pretty simple stuff. This started because I was trying to make some properties of a script instance only; I don’t want them serialized into the prefab assets. When that class is first placed on a game object in the scene, the two properties are serialized into the scene file as “NotAPrefab” like I would expect.
It gets strange when I drag the object into the library to create a prefab. The prefab file is serialized with “NotAPrefab” for both fields. But when the prefab is inspected in the editor, the asset type is “Regular” and the instance status is “NotAPrefab”. Can anyone explain what’s going on? Any suggestions for how to reliably change the serialization for prefab assets, but not instances?
Using 2019.1.14.