hayateasdf's blog

Unity,C#, javascript,C++,python,batなど

unity YamlDotNet for Unityを使いやすくするオレオレスクリプト

http://qiita.com/asus4/items/bac121c34cd3169116c0

上記の記事を見てもらいたい。 わたしが言いたいことも同じで、YamlDotNetを使っていると型キャストがめんどくさい。

ので、上記記事のJsonNodeの作りを丸パクリしてYamlNode用のYamlInstant(即席yaml)を作成しました。

using System.Collections;
using System.Collections.Generic;
using System;
using YamlDotNet;
using YamlDotNet.RepresentationModel;

// 即席Yaml
public class YamlInstant : IEnumerable<YamlInstant>, IDisposable
{
    YamlNode obj;
    public YamlInstant(YamlNode obj) { this.obj = obj; }
    public void Dispose() { obj = null; }

    public YamlInstant this [YamlNode key] { get { return new YamlInstant(toMap()[key]); } }
    public YamlScalarNode toValue() { 
        if (obj is YamlScalarNode) {
            return (YamlScalarNode)obj; 
        } else {
            throw new Exception("YamlScalarNode != " + obj.GetType().Name);
        }
    }
    public YamlMappingNode toMap() { 
        if (obj is YamlMappingNode) {
            return (YamlMappingNode)obj; 
        } else {
            throw new Exception("YamlMappingNode != " + obj.GetType().Name);
        }
    }
    public YamlSequenceNode toList() {
        if (obj is YamlSequenceNode) {
            return (YamlSequenceNode)obj;
        } else {
            throw new Exception("YamlSequenceNode != " + obj.GetType().Name);
        }
   }
    public string Value { get { return toValue().Value; } }
    public IEnumerator<YamlInstant> GetEnumerator()
    {
        var list = toList();
        foreach (YamlMappingNode o in list)
        {
            yield return new YamlInstant(o);
        }
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

使用例

AnimationCurveのプリセットにペナーイージングを加えるから.curves ファイルをお借りして.curvesファイル(yaml)から 直で値を引っ張ってきてcurveを生成する。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using YamlDotNet;
using YamlDotNet.RepresentationModel;

public class YamlTest : MonoBehaviour {
    public List<AnimationCurve> curve = new List<AnimationCurve>();

    void Start () {
        var path = "Assets/Editor/EasingCurves.curves";
        var yaml = new YamlStream();
        using (var file = new StreamReader(path, System.Text.Encoding.UTF8))
        {
            yaml.Load(file);
        }

        var yi = new YamlInstant(yaml.Documents[0].RootNode);
        var presets = yi["MonoBehaviour"]["m_Presets"];

        foreach (var item in presets)
        {
            var name = item["m_Name"].Value;
            Debug.Log("name: " + name);

            var ary = item["m_Curve"]["m_Curve"];
            var keys = new List<Keyframe>();
            foreach (var data in ary)
            {
                var time = TryFloat(data["time"].Value);
                var value = TryFloat(data["value"].Value);
                var inslope = TryFloat(data["inSlope"].Value);
                var outslope = TryFloat(data["outSlope"].Value);

                keys.Add(new Keyframe(time, value, inslope, outslope));
            }
            curve.Add(new AnimationCurve(keys.ToArray()));
        }

    }
    public static float TryFloat(string s)
    {
        var f = -1f;
        float.TryParse(s, out f);
        return f;
    }
}