Search Unity

Different serialization layout when loading

Discussion in 'Editor & General Support' started by ddalacu, May 4, 2018.

  1. ddalacu

    ddalacu

    Joined:
    May 13, 2014
    Posts:
    23
    Our team has recently encountered with a bug that took about two days of debugging, but we finaly managed to figure out the issue and i have prepared a simple test scenario to reproduce the error wich was like this:

    A script behaviour (probably TestBehaviour?) has a different serialization layout when loading. (Read 32 bytes but expected 36 bytes)
    Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts?
    A script behaviour (probably TestBehaviour?) has a different serialization layout when loading. (Read 32 bytes but expected 40 bytes)
    Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TestGeneric<T> {
    6. #if UNITY_EDITOR
    7.     public bool testBool;
    8.     public int testInt;
    9. #endif
    10. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TestBehaviour : MonoBehaviour {
    6.  
    7.     [System.Serializable]
    8.     public class TestGenericInstance : TestGeneric<int> { }
    9.  
    10.     public TestGenericInstance test;
    11. }
    All you need to to do for the error to apear is to put the TestBehaviour on a game object in a scene and make a build with that scene, in the build you will get the error :(

    What we don't understand is that our understanding of unity s serialization was that during the build process unity recompiles the scripts then reserializes the assets that go in the build, if so why do we get this strange error in our builds, if someone from unity can answer this would be great . We currently work around this problem but this isn't really a fix.

    ps this error exists from 2017.4 to 2018.2 beta and on android the build crashes, on windows don't
     
  2. MOH1301

    MOH1301

    Joined:
    Dec 3, 2012
    Posts:
    1
    Did you figure out the problem? Unity does not handle variables inside a #if UNITY_EDITOR very well, and it should never be done. A solution is to instead only make the values accessable in the editor.
     
  3. ddalacu

    ddalacu

    Joined:
    May 13, 2014
    Posts:
    23
    Well the problem is with unity s serialization and i think this bug i described should not be here or we should be warned in a more explicit manner about this and not something like "don;t use serialized fields in conditional compiler because it might not work"
     
    hippocoder likes this.
  4. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    777
    Sorry for the bump but I just ran into this myself.

    The strange thing is, I've only been able to get this error to occur when I strip out serialised fields from plain old data classes that are inherited, and then used in mono behaviours. Basically, same as @ddalacu's example, but no generics needed.

    So it works.. in some cases. It is just really convenient for stripping out junk from editor specific code on behaviours.