2 个版本

0.1.1 2022年5月26日
0.1.0 2022年5月25日

#15 in #unity

MIT/Apache

150KB
4.5K SLoC

unity-yaml-rust

unity-yaml-rust 是一个用于 Unity 的纯 Rust YAML 解析器。

基于 yaml-rust 添加了 Unity YAML 规范和可变访问基础

快速入门

将以下内容添加到您项目的 Cargo.toml 中

[dependencies]
unity-yaml-rust = "0.1"

使用 yaml::YamlLoader 加载 YAML 文档并将其作为 Vec/HashMap 访问

use unity_yaml_rust::{yaml::YamlLoader, emmitter::YamlEmitter};

fn main() {
    let s =
r#"
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!687078895 &4343727234628468602
SpriteAtlas:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_Name: atlas_launch
  m_EditorData:
    serializedVersion: 2
    textureSettings:
      serializedVersion: 2
      anisoLevel: 1
      compressionQuality: 50
      maxTextureSize: 2048
      textureCompression: 0
      filterMode: 1
      generateMipMaps: 0
      readable: 0
      crunchedCompression: 0
      sRGB: 1
    platformSettings: []
    packingSettings:
      serializedVersion: 2
      padding: 4
      blockOffset: 1
      allowAlphaSplitting: 0
      enableRotation: 0
      enableTightPacking: 0
    variantMultiplier: 1
    packables:
    - {fileID: 102900000, guid: c5a32d8209c314631bad292a32582dfc, type: 3}
    bindAsDefault: 1
  m_MasterAtlas: {fileID: 0}
  m_PackedSprites:
  - {fileID: 21300000, guid: 3083aff0bd42a4268a9cfe6e564ab247, type: 3}
  - {fileID: 21300000, guid: 054656e6c52c2425eb7c5ec764d03587, type: 3}
  - {fileID: 21300000, guid: 55aba929877c26747acf9ad10ee7989c, type: 3}
  m_PackedSpriteNamesToIndex:
  - login_ic_logo
  - launch_icon_service
  - login_ic_logo_bak1
  m_Tag: atlas_launch
  m_IsVariant: 0
"#;
    let mut docs = YamlLoader::load_from_str(s).unwrap();

    // Multi document support, doc is a yaml::Yaml
    for doc in docs.iter_mut() {
        // Debug support
        println!("{:?}", doc);

        if !matches!(doc, Yaml::Original(_)) {
            //IndexMut
            let sprite_atlas = &mut doc["SpriteAtlas"];
            
            assert_eq!(sprite_atlas["m_ObjectHideFlags"].as_i64(), Some(0i64));
            assert!(sprite_atlas["m_ObjectHideFlags"].replace_i64(3));
            assert_eq!(sprite_atlas["m_ObjectHideFlags"].as_i64(), Some(3i64));
            
            sprite_atlas["m_Name"].replace_string("launch".to_string());
            assert_eq!(sprite_atlas["m_Name"].as_str(), Some("launch"));

            sprite_atlas.insert("custom", Yaml::Boolean(true));
            assert_eq!(sprite_atlas["custom"].as_bool(), Some(true));

            sprite_atlas.remove("m_PackedSprites");
            assert!(sprite_atlas["m_PackedSprites"].is_badvalue());

            sprite_atlas["m_EditorData"]["packables"].remove_at(0);
            sprite_atlas["m_EditorData"]["packables"].push(Yaml::String("ppp".to_string()));
            sprite_atlas["m_MasterAtlas"].insert("plus", Yaml::Boolean(false));
            sprite_atlas["m_MasterAtlas"].remove("fileID");
        }

        // Dump the YAML object
        let mut out_str = String::new();
        {
            let mut emitter = YamlEmitter::new(&mut out_str);
            emitter.dump(doc).unwrap(); // dump the YAML object to a String
        }
        println!("{}", out_str);
    }
}

许可证

许可协议为以下之一

任选其一。

贡献

在 Github 上 Fork & PR。

除非您明确声明,否则根据 Apache-2.0 许可证定义,您提交的任何有意包含在作品中的贡献,将按照上述方式双许可,不附加任何额外条款或条件。

依赖项