Created
July 30, 2020 01:37
-
-
Save tarukosu/40302d6b67f67fc7121c877db86f645e to your computer and use it in GitHub Desktop.
Bolt フローの動的ロード
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 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