Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

YamlDotNet for Unityを使ってUnity上でYamlを読み込ませてみる

More than 5 years have passed since last update.

YamlDotNet for Unity(https://www.assetstore.unity3d.com/jp/#!/content/36292)
これを使ってYAMLを読み込んでC#クラス化(デシリアライズ)してみます。

1. YAMLの用意

Example.yaml
---
            name: slime
            status:
                attack: 7.0
                defense: 4.0

            spell:
                - name: heal
                  power: 20.0
                  mp: 4
                - name: fire
                  power: 15.0
                  mp: 3
                - name: ice
                  power: 10.0
                  mp: 2
... 

※YAMLを作成する上での注意点
・ (12+インデント回数*4)文字分の半角スペースを入れないとデシリアライズに失敗します。
YamlDotNetだと発生していた問題ですが、YamlDotNet For Unityで試したところ発生しませんでした。。

2. C#クラスの作成

public class DeserializedObject
{
    public string name { get; set; }

    public Status status { get; set; }

    [YamlAlias("spell")]
    public List<MagicSpell> spells { get; set; }

    public class Status
    {
        public float attack { get; set; }
        public float defense { get; set; }
    }
    public class MagicSpell
    {
        public string name { get; set; }
        public float power { get; set; }
        public int mp { get; set; }
    }
}

3. デシリアライズ

public class YamlImporter
{
    public static DeserializedObject Deserialize(string yamlName)
    {
        StreamReader sr = new StreamReader(yamlName);
        string text = sr.ReadToEnd();
        var input = new StringReader(text);
        var deserializer = new Deserializer(namingConvention: new CamelCaseNamingConvention());
        DeserializedObject deserializeObject = deserializer.Deserialize<DeserializedObject>(input);
        return deserializeObject;
    }
}

使い方

DeserializedObject obj = YamlImporter.Deserialize("Assets/Example.yaml");
Debug.Log(obj.name);

ソースコード全体

1~3を1つのソースコードにまとめると以下のようになります。

using System.Collections.Generic;
using System.IO;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace YamlExample
{
    public class DeserializedObject
    {
        public string name { get; set; }

        public Status status { get; set; }

        [YamlAlias("spell")]
        public List<MagicSpell> spells { get; set; }

        public class Status
        {
            public float attack { get; set; }
            public float defense { get; set; }
        }
        public class MagicSpell
        {
            public string name { get; set; }
            public float power { get; set; }
            public int mp { get; set; }
        }
    }

    public class YamlImporter
    {
        public static DeserializedObject Deserialize(string yamlName)
        {
            StreamReader sr = new StreamReader(yamlName);
            string text = sr.ReadToEnd();
            var input = new StringReader(text);
            var deserializer = new Deserializer(namingConvention: new CamelCaseNamingConvention());
            DeserializedObject deserializeObject = deserializer.Deserialize<DeserializedObject>(input);
            return deserializeObject;
        }
    }
}



2015-09-30
YamlAliasを使うとwarningが出る問題について
[YamlAlias("spell")]の代わりに[YamlMember(Alias = "spell")]と書くことでwarningが出なくなるみたいです。

関連

YamlDotNetを使って詰まったところなどメモ
http://qiita.com/r-ngtm/items/a7164c60035335bdfb4e

YamlDotNet for Unityを使って詰まったところなどメモ
http://qiita.com/r-ngtm/items/154c30007f85600a1ac1

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away