Search Unity

How to convert some asset to "Asset Serialization Mode"?

Discussion in 'Editor & General Support' started by Takafumi_Ogihara, Mar 7, 2017.

  1. Takafumi_Ogihara

    Takafumi_Ogihara

    Joined:
    Mar 7, 2017
    Posts:
    2
    I have a question regarding "Asset Serialization Mode".
    How can I convert the serialization mode of some assets?

    "Asset Serialization Mode" is set to "Force Text" in our current project but I would like to convert some assets to binary format to reduce the file size and increase loading speed.

    I figured out that I can mix Text format and Binary format by selecting "Mixed” from [Edit]->[PROject Settigns]->[Editor].
    I couldn’t find how to make certain assets Binary format (or Text format).

    I want to keep some assets like Prefab to text format so that changes are noticeable and convert resource assets such as FBX to Binary format to reduce file size.
    I looked up community sites and UnityDocumentation, but I couldn’t find a solution.
    If you have any information, please share.

    I use Unity5.4.4f1.

    Thanks in advance.

    Takafumi Ogihara
    ACQUIRE Corp.
     
  2. Diki_Thomas

    Diki_Thomas

    Joined:
    Sep 8, 2016
    Posts:
    108
    Asset serialization mode only affects assets that you create inside Unity (prefabs, scenes, materials) not imported assets (FBXes, textures).

    Have you profiled your loading time with "Force binary" mode to confirm that is causing your problem?
     
  3. Takafumi_Ogihara

    Takafumi_Ogihara

    Joined:
    Mar 7, 2017
    Posts:
    2
    Hi Diki,

    Thank you for your reply.
    What I wanted to change to “Force Binary” was “Animation Clip”.
    (I gave FBX as an example by mistake.)

    Since “Animation Clip” is created inside Unity, Asset serialization will affect it.

    >Have you profiled your loading time with "Force binary" mode to confirm that is causing your problem?

    In fact, I would like to know how to convert assets in Text format to Binary format for this.

    I changed "Force Text" mode to "Mixed" mode but .anim file contents remained in Text format.
    I could use "Force Binary" mode for verification but it takes so much time so I would like to convert only certain files to Binary format.

    Is there a way to control from script for example import settings?

    Takafumi Ogihara
    ACQUIRE Corp.
     
  4. moco2k

    moco2k

    Joined:
    Apr 29, 2015
    Posts:
    294
    I have a similar question; Basically, I want to change serialization for a given set of assets only.
    The problem is, if I change serialization mode in the settings, it seems that ALL relevant assets are re-serialized, which is not want I need. Is there any way to set the mode only for a specific set of assets? If not, I think this option should be added.
     
    Last edited: Jun 25, 2018
  5. Enderous

    Enderous

    Joined:
    Nov 7, 2018
    Posts:
    1
    When I use Asset Serialization Mode in Unity it converts all assets. BUT I need to control this process and choose assets that i need. Is there any API to invoke resealize to particular Asset Serialization Mode for particular assets?
     
    DungDajHjep and JeremyLvChy like this.
  6. Scott-Steffes

    Scott-Steffes

    Joined:
    Dec 31, 2013
    Posts:
    59
    I would even settle for being able to change the setting per asset type. We need to serialize all animations as binary, but the scenes are fine as text.
     
    shangtao2012 likes this.
  7. shangtao2012

    shangtao2012

    Joined:
    Aug 24, 2019
    Posts:
    1
    Can we set some files as text now in Asset serialization "mixed" or "force binary" mode?
    I have a lot of animation files and some xml files. I need keep xml files as text mode.
     
  8. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    I have a large scene in unity that even when "force text" is set will only save as binary. Smaller scenes in the same project save as text. Very strange.
     
  9. JeremyLvChy

    JeremyLvChy

    Joined:
    Jul 23, 2019
    Posts:
    10
    Can we do it ? only change .anim file to binary mode ?
     
  10. fwalker

    fwalker

    Joined:
    Feb 5, 2013
    Posts:
    255
    Did anyone find an answer to this?
     
  11. tim12332000

    tim12332000

    Joined:
    Jun 15, 2017
    Posts:
    20
    Did anyone find an answer to this?
     
  12. DaseinPhaos

    DaseinPhaos

    Joined:
    Nov 20, 2017
    Posts:
    7
    bump since per-asset serialization type override can be REALLY useful:
    we want our scenes/big prefabs to be serialized as binary files to save size/saveload time.
    But for project setting file, text serialization is WAY better since changes applied under different build configs can be easily merged by any text merger
     
    DungDajHjep likes this.
  13. soulburner

    soulburner

    Joined:
    Mar 25, 2012
    Posts:
    169
    I also wish we had this feature. For example, we have large scenes (200+ Mb) that we don't want to be text assets, but also have a lot of other assets that would be great to have in text mode.

    So, custom serialization format for each file would be really helpful.
     
    DungDajHjep likes this.
  14. tantk_nc

    tantk_nc

    Joined:
    Aug 4, 2020
    Posts:
    1
    bump for per-asset serialization mode
     
  15. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    130
    This would be very helpful. Don't think it's currently possible but giving this a bump!
     
  16. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    130
    I did find a workaround some might find helpful. You can create an empty container ScriptableObject class with the PreferBinarySerialization attribute, like this -

    [PreferBinarySerialization]
    public class BinaryAssetContainer : ScriptableObject { }

    And then package the assets you want into a BinaryAssetContainer asset. Not the prettiest solution but if you need to save the file space (eg. get under Github 100 MB per file limit) it works!
     
    Last edited: Sep 1, 2020
    DungDajHjep likes this.
  17. daniFMdev

    daniFMdev

    Joined:
    Jun 21, 2017
    Posts:
    7
    Is there a way to do the opposite? Sadly, there's no "[PreferTextSerialization]"
     
    learner_CL likes this.
  18. JoNax97

    JoNax97

    Joined:
    Feb 4, 2016
    Posts:
    611
    @Peter77 weren't you after something like this?
     
    Peter77 likes this.
  19. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Yes, thanks for the tag. Unfortunately, [PreferBinarySerialization] isn't working for me, because I generate an UnityEngine.Mesh rather than a custom asset where I would have the opportunity to add the attribute.
     
  20. JoNax97

    JoNax97

    Joined:
    Feb 4, 2016
    Posts:
    611
    Maybe I understood this incorrectly but, wouldn't saving the mesh inside this "BinaryAssetContainer" (via AssetDatabase.AddObjectToAsset) save the whole thing as binary? Maybe :p
     
    thebarryman and Peter77 like this.
  21. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Ah I see. Yes this probably works. Not particular nice, but could be a workaround. I let this sink in for a while :)
     
    JoNax97 likes this.
  22. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    202
    Does anyone have guidance for this problem?
     
  23. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    202
    Does someone have detailed instructions to follow this? Maybe I didn't understand clearly?
     
  24. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    130
    The BinaryAssetContainer is an empty script that lets you create a ScriptableObject asset in your project that will always have binary serialization, even if your project is configured to use text-based serialization.

    You then will need to add child assets to your empty BinaryAssetContainer (otherwise the BinaryAssetContainer will remain empty).

    Eg -

    Code (CSharp):
    1. BinaryAssetContainer assetContainer = ScriptableObject.CreateInstance<BinaryAssetContainer>();
    2. AssetDatabase.CreateAsset(assetContainer, assetPath);
    3.  
    4. AssetDatabase.AddObjectToAsset(modelIWantToSerialize, assetPath);
    5. AssetDatabase.AddObjectToAsset(textureIWantToSerialize, assetPath);
    This is just my way of hacking around the problem. Unity's [PreferBinarySerialization] attribute is what makes it work. You could probably come up with other ways of using it for your particular application. But, this works well enough as a quick and dirty fix.
     
    funkyCoty likes this.
  25. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    202
    Can you give me a full example instead of a code snippet?
     
  26. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    202
    Please help !
     
  27. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    130
    That is all the code that is required for it to work - which part do you need more information about?
     
  28. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    202
    I need to throw it into a class or what, I mean with that code I don't know how to implement it.
     
  29. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    202
  30. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    202
    Just to inform everyone, I came up with a new trick.
    That is exporting heavy prefabs to an empty project with the force binary setting and importing it again later.
    You need to set the main project's serialzie format to mixed.
     
    mitaywalle likes this.
  31. phantom-loong

    phantom-loong

    Joined:
    Nov 19, 2019
    Posts:
    1
    oh. same question....
     
  32. funkyCoty

    funkyCoty

    Joined:
    May 22, 2018
    Posts:
    727
    This is clever. Unfortunately, it does not work with Prefabs (you cannot put a prefab as a sub asset of another asset, dang).