如何解决错误:A script behaviour has a different serialization layout when loading

在实际开发中碰到一个问题,加载一个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包住整个类!


Found 1 interfaces on host : 0) 192.168.2.9 Multi-casting "[IP] 192.168.2.9 [Port] 55000 [Flags] 2 [Guid] 1413451664 [EditorId] 1822263808 [Version] 1048832 [Id] WindowsPlayer(2,DESKTOP-3OH9VP2) [Debug] 1 [PackageName] WindowsPlayer [ProjectName] AcademyApp" to [225.0.0.222:54997]... Starting managed debugger on port 56664 AS: AutoStreaming module initializing. [Physics::Module] Initialized MultithreadedJobDispatcher with {0} workers. Initialize engine version: 2021.3.45f1c1 (9d8da11b98d3) [Subsystems] Discovering subsystems at path D:/Gameproject/xjxy/xjxy20250824/AcademyApp_Data/UnitySubsystems GfxDevice: creating device client; threaded=1; jobified=1 Direct3D: Version: Direct3D 11.0 [level 11.1] Renderer: NVIDIA Quadro P600 (ID=0x1cbc) Vendor: NVIDIA VRAM: 4008 MB Driver: 32.0.15.7342 <RI> Initializing input. <RI> Input initialized. <RI> Initialized touch support. The referenced script (AcademyApp.Game.UI.Panel.Textcontrol) on this Behaviour is missing! The referenced script (AcademyApp.AI.XunfeiTTSClient) on this Behaviour is missing! The referenced script (AcademyApp.Game.Net.NetManager) on this Behaviour is missing! The referenced script (AcademyApp.Game.Net.LoginManager) on this Behaviour is missing! The referenced script (AcademyApp.Game.Net.GameServerManager) on this Behaviour is missing! The referenced script (AcademyApp.Game.Game.GameLogicManager) on this Behaviour is missing! The referenced script (AcademyApp.Game.UI.Panel.SettingsPanel) on this Behaviour is missing! The referenced script (AcademyApp.Game.GameEntry) on this Behaviour is missing! The referenced script (AgreementPanelController) on this Behaviour is missing! The referenced script (LoginPanelController) on this Behaviour is missing! The referenced script (RegisterPanelController) on this Behaviour is missing! The referenced script on this Behaviour (Game Object 'Text (Legacy)') is missing! A scripted object (probably AcademyApp.Game.UI.Panel.Textcontrol?) has a different serialization layout when loading. (Read 32 bytes but expected 352 bytes) Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts? The referenced script on this Behaviour (Game Object 'Text (Legacy)') is missing! A scripted object (probably AcademyApp.AI.XunfeiTTSClient?) has a different serialization layout when loading. (Read 32 bytes but expected 152 bytes) Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts? The referenced script on this Behaviour (Game Object 'NetManager') is missing! The referenced script on this Behaviour (Game Object 'NetManager') is missing! The referenced script on this Behaviour (Game Object 'NetManager') is missing! The referenced script on this Behaviour (Game Object 'NetManager') is missing! The referenced script on this Behaviour (Game Object 'SettingsPanel') is missing! A scripted object (probably AcademyApp.Game.UI.Panel.SettingsPanel?) has a different serialization layout when loading. (Read 32 bytes but expected 152 bytes) Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts? The referenced script on this Behaviour (Game Object 'GameEntry') is missing! A scripted object (probably AcademyApp.Game.GameEntry?) has a different serialization layout when loading. (Read 32 bytes but expected 104 bytes) Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts? The referenced script on this Behaviour (Game Object 'AgreementPanel') is missing! A scripted object (probably AgreementPanelController?) has a different serialization layout when loading. (Read 32 bytes but expected 92 bytes) Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts? The referenced script on this Behaviour (Game Object 'RegisterPanel') is missing! A scripted object (probably RegisterPanelController?) has a different serialization layout when loading. (Read 32 bytes but expected 92 bytes) Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts? The referenced script on this Behaviour (Game Object 'LoginPanel') is missing! A scripted object (probably LoginPanelController?) has a different serialization layout when loading. (Read 32 bytes but expected 176 bytes) Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts? UnloadTime: 371.123800 ms PlayerConnection::Cleanup
最新发布
08-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值