LoginSignup
18
13

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-09-28

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

18
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
13