[Unity]解决错误:A script behaviour has a different serialization layout when loading
2020/04
18
08:04
在实际开发中碰到一个问题,加载一个Prefab, Unity会报以下错误:
A script behaviour has a different serialization layout when loading. (Read 24 bytes but expected 40 bytes), Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts?
这是示例代码
using UnityEngine;
public class ObjectPreview : MonoBehaviour
{
#if UNITY_EDITOR
public GameObject obj;
void Start ()
{
// code ...
}
#endif
}
也就是说, ObjectPreview在编辑器模式下和非编辑器模式下, 序列化出来的内容是不同的
那么, 由于美术(或者其它团队成员)做的Prefab是在编辑器环境下产生的, 所以是带有obj的序列化数据
但是在运行时, 由于这个UNITY_EDITOR宏,导致运行时与编辑器 序列化内容不同!
正确的做法是去掉 #if UNITY_EDITOR
或者把#if UNITY_EDITOR包住整个类!
CopyRights: The Post by BY-NC-SA For Authorization,Original If Not Noted,Reprint Please Indicate From 老刘@开发笔记
Post Link: [Unity]解决错误:A script behaviour has a different serialization layout when loading
Post Link: [Unity]解决错误:A script behaviour has a different serialization layout when loading