Search Unity

How to use build-in serializer?

Discussion in 'Editor & General Support' started by mishakozlov74, Feb 4, 2019.

  1. mishakozlov74

    mishakozlov74

    Joined:
    Aug 8, 2018
    Posts:
    143
    Hello.

    I'm developing level editor for my game so I want it to work with native Unity scene format - .unity. In fact, scenes are essentially text files in YAML-based format. So, we definitely are able to parse it and create at runtime.

    The problem is, I don't understand, how can I serialize a simple GameObject into this format by code?

    I mean, is it possible to use build-in serializer by code? I found tons of references about it, but I don't see any topic covers it's usage.
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    I could be wrong, but I'm pretty sure you cannot use the YAML serializer built into unity. When Unity builds your project, all YAML files get compiled into your game, and are no longer accessible as standalone files.

    This is only true if you select the non-default Editor serialization option "Force Text", as opposed to "Forced Binary" or "Mixed". The text files are mostly to help your source control, I think it's always saved and loaded as binary data in a built executable. For that reason, the text-based serializer is likely an editor-only feature that resides in the UnityEditor namespace, which doesn't get compiled during a build.

    There is a built-in JSON serialize that allows you to easily convert serializable classes to and from strings. You can save and load those strings from files at runtime. https://docs.unity3d.com/ScriptReference/JsonUtility.html

    Just a side note: Anything you save to your assets folder will get compiled into your program, and will not be accessible as a separate file in a build of your game. The only exception is the StreamingAssets folder; anything in StreamingAssets will not get compiled, and will be available as a standalone file in the data folder of your built game.
     
  3. harryr

    harryr

    Unity Technologies

    Joined:
    Nov 14, 2017
    Posts:
    38
    Just wanted to confirm Gambit's message, we don't expose the YAML serializer to users from C#.

    For something like a level editor I'd suggest you create your own level format or (as suggested above) use JsonUtility.

    How possible this is really depends on what kind of level editor you're looking to create.
    If this is something that you'll also expose to your users then you'll have to create your own format and think of it as custom data your game can load to re-create the level, as opposed to being Unity specific data.

    If it's something that's only for yourself during development then you may well be able to create your level editor out of editor scripts, in this case you'd be able to save your scene out as your would any other Unity scene.