To save the state of the game to a file, you need to save / load the state of the behavior tree and its variables, how to do it? (not work in play mode)
public class TestBehaviorTreeSerialize : MonoBehaviour
{
public string Json;
public BehaviorTree behaviorTree;
private void Awake()
{
behaviorTree = GetComponent<BehaviorTree>();
}
[ContextMenu("Save")]
private void Save()
{
var source = behaviorTree.GetBehaviorSource();
Json = JsonUtility.ToJson(source);
}
[ContextMenu("Load")]
private void Load()
{
var source = JsonUtility.FromJson<BehaviorSource>(Json);
behaviorTree.enabled = false;
source.Initialize(behaviorTree);
if (source.CheckForSerialization(true))
{
behaviorTree.SetBehaviorSource(source);
}
behaviorTree.enabled = true;
}
}
public class TestBehaviorTreeSerialize : MonoBehaviour
{
public string Json;
public BehaviorTree behaviorTree;
private void Awake()
{
behaviorTree = GetComponent<BehaviorTree>();
}
[ContextMenu("Save")]
private void Save()
{
var source = behaviorTree.GetBehaviorSource();
Json = JsonUtility.ToJson(source);
}
[ContextMenu("Load")]
private void Load()
{
var source = JsonUtility.FromJson<BehaviorSource>(Json);
behaviorTree.enabled = false;
source.Initialize(behaviorTree);
if (source.CheckForSerialization(true))
{
behaviorTree.SetBehaviorSource(source);
}
behaviorTree.enabled = true;
}
}