question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unity (Game Engine) Yaml parsing

See original GitHub issue

Hello,

I am trying to parse meta files from the game engine Unity to Json. (For a kind of self build level editor), now I try to parse the Yaml data with JS-Yaml, but I get an error.

The following Yaml:

%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &100000
GameObject:
  m_ObjectHideFlags: 0
  m_PrefabParentObject: {fileID: 0}
  m_PrefabInternal: {fileID: 100100000}
  serializedVersion: 4
  m_Component:
  - 4: {fileID: 400000}
  - 212: {fileID: 21200000}
  - 61: {fileID: 6100000}
  - 50: {fileID: 5000000}
  - 114: {fileID: 11400001}
  m_Layer: 14
  m_Name: CamelGreen
  m_TagString: Plane
  m_Icon: {fileID: 0}
  m_NavMeshLayer: 0
  m_StaticEditorFlags: 0
  m_IsActive: 1
--- !u!1 &100002
...

Throws the error:

JS-YAML: unknown tag !<tag:unity3d.com,2011:1> at line 22, column 1:
    --- !u!1 &100002
    ^

...\node_modules\js-yaml\lib\js-yaml\loader.js:154
    throw generateError(message);
          ^
YAMLException: JS-YAML: undeclared tag handle "!u!" at line 22, column 9:
    --- !u!1 &100002

But as far as my limited knowledge about Yaml goes, I do see a TAG declaration at the top of the Yaml file.

Then I tried to declare a custom tag:

var spaceYamlType = new yaml.Type('!u!', {
  loader: {
    kind: 'object',
    resolver: function (object) {
      return object;
    }
  },
  dumper: {
    kind: 'object',
    instanceOf: Object 
  }
});

var SPACE_SCHEMA = yaml.Schema.create([ spaceYamlType ]);

(Yes, that is code straight copied from the example) But that didn’t work either (same error).

Any idea what is going wrong here?

Arco

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:17 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
divillysausagescommented, Dec 29, 2020

If anyone happens to come across this, this is the code that I’m using (in 2020/2021) to load prefab and script files:

const fs = require( 'fs' );
const yaml = require( 'js-yaml' );

module.exports = async function ( path ) {
    const types = {};

    let file = await fs.promises.readFile( path, 'utf8' );

    // remove the unity tag line
    file = file.replace( /%TAG.+\r?\n?/, '' );

    // replace each subsequent tag with the full line + map any types
    file = file.replace( /!u!([0-9]+).+/g, ( match, p1 ) => {

        // create our mapping for this type
        if ( !( p1 in types ) ) {
            const type = new yaml.Type( `tag:unity3d.com,2011:${p1}`, {
                kind: 'mapping',
                construct: function ( data ) {
                    return data || {}; // in case of empty node
                },
                instanceOf: Object
            } );
            types[p1] = type;
        }

        return `!<tag:unity3d.com,2011:${p1}>`
    } );

    // create our schema    
    const schema = yaml.Schema.create( Object.values( types ) );

    // parse our yaml
    const objAr = yaml.loadAll( file, null, { schema } );

    // objAr will be something like:
    // [
    //     {
    //         "GameObject": {
    //             ...
    //         }
    //     },
    //     {
    //         "Transform": {
    //             ...
    //         }
    //     },
    //     ...
    // ]
    return objAr;
}

You can just import that anywhere, something like (assuming a file named readUnityFile.js):

const readUnityFile = require('./readUnityFile');
readUnityFile( pathToUnityFile )
1reaction
dervuscommented, Nov 5, 2013

You must use loadAll function to load multi-document sources. See the README.

Read more comments on GitHub >

github_iconTop Results From Across the Web

UnityYAML - Unity - Manual
Unity uses a custom-optimized YAML library called UnityYAML. ... the scalar returns This is a and might trigger an Asset into further parsing....
Read more >
YAML parser in Unity
There are yaml parsers out there that you can use. Unity does not have a C# yaml parser. The serialization happens on the...
Read more >
YAML Config File Reader for Unity 3D - Zsolt Balai
This is a package to read this type of file into unity objects. It is in C#, but can be called from Javascript...
Read more >
[Fixed] Unable to parse YAML file in Unity 3d project - Morioh
[Fixed] Unable to parse YAML file in Unity 3d project. ... Unity 3D is the trending game app development framework to serve the...
Read more >
Unity Developers Guide | Parse
You simply set whatever key-value pairs you want, and our backend will store it. For example, let's say you're tracking high scores for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found