Skip to content

Instantly share code, notes, and snippets.

@tarukosu
Created July 30, 2020 01:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarukosu/40302d6b67f67fc7121c877db86f645e to your computer and use it in GitHub Desktop.
Save tarukosu/40302d6b67f67fc7121c877db86f645e to your computer and use it in GitHub Desktop.
Bolt フローの動的ロード
using Bolt;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using YamlDotNet.RepresentationModel;
public class FlowTextLoader : MonoBehaviour
{
public TextAsset textAsset;
void Start()
{
var json = ConvertYamlToJson(textAsset.text);
Debug.Log(json);
var a = ScriptableObject.CreateInstance<FlowMacro>();
JsonUtility.FromJsonOverwrite(json, a);
var flow = gameObject.AddComponent<FlowMachine>();
flow.nest.SwitchToMacro(a);
}
string ConvertYamlToJson(string text)
{
var input = new StringReader(text);
// Load the stream
var yaml = new YamlStream();
yaml.Load(input);
var root = (YamlMappingNode)yaml.Documents[0].RootNode;
// Examine the stream
var monoBehaviourNode = (YamlMappingNode)root.Children[new YamlScalarNode("MonoBehaviour")];
var dataNode = (YamlMappingNode)monoBehaviourNode.Children[new YamlScalarNode("_data")];
var jsonNode = (YamlScalarNode)dataNode.Children[new YamlScalarNode("_json")];
var flowJson = new FlowJson()
{
_data = new FlowData()
{
_json = jsonNode.Value
}
};
var json = JsonUtility.ToJson(flowJson);
return json;
}
}
[Serializable]
public class FlowJson
{
public FlowData _data;
}
[Serializable]
public class FlowData
{
public string _json;
public List<string> _objectReferences = new List<string>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment