Last active
August 8, 2024 05:53
-
-
Save arakaki-asdf/68457cba39ed5a0ef1b909397f4e7dc4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using YamlDotNet; | |
using YamlDotNet.RepresentationModel; | |
// 使用例 AnimationCurveのプリセットにペナーイージングを加えるから.curves ファイルをお借りして | |
// .curvesファイル(yaml)から 直で値を引っ張ってきてcurveを生成する。 | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment