public NodeEditorDeserializer(SerializedNodeEditor data, NodeBasedEditor editor)
        {
            scriptedNodes = new Queue <SerializedScriptedNode>(data.scriptedNodes);
            typedNodes    = new Queue <SerializedTypedNode>(data.typedNodes);
            optionNodes   = new Queue <SerializedOptionsNode>(data.optionNodes);
            this.editor   = editor;

            count = data.count;
        }
    private void LoadDialogueTree()
    {
        var path = EditorUtility.OpenFilePanel("Load Dialogue Tree", Application.dataPath, "json");

        if (nodes != null)
        {
            nodes.Clear();
        }
        else
        {
            nodes = new List <ConnectionNode>();
        }

        if (connections != null)
        {
            connections.Clear();
        }
        else
        {
            connections = new List <Connection>();
        }

        StreamReader reader = new StreamReader(path);
        string       json   = "";

        try
        {
            json = reader.ReadToEnd();
        }
        finally
        {
            reader.Close();
        }

        SerializedNodeEditor serializedData = JsonUtility.FromJson <SerializedNodeEditor>(json);

        NodeEditorDeserializer deserializer = new NodeEditorDeserializer(serializedData, this);

        ConnectionNode prevNode = entryNode;

        Queue <ConnectionNode> childrenNodes = new Queue <ConnectionNode>();

        for (int i = 0; i < deserializer.count; i++)
        {
            ConnectionNode node;
            bool           isChildNode = false;
            if (childrenNodes.Count > 0)
            {
                selectedOutPoint = childrenNodes.Dequeue().GetOutPoint();
                isChildNode      = true;
            }
            else
            {
                selectedOutPoint = prevNode.GetOutPoint();
            }

            node = deserializer.GetNode(i, childrenNodes);
            nodes.Add(node);

            selectedInPoint = node.GetInPoint();
            CreateConnection();

            if (!isChildNode)
            {
                prevNode = node;
            }
        }

        Debug.Log("Loading Done");
    }
    private void SaveDialogueTree()
    {
        var path = EditorUtility.SaveFilePanel("Save Dialogue Tree", Application.dataPath, convoTitle, "json");

        SerializedNodeEditor serializedData = new SerializedNodeEditor();

        TreeList <IDialogueContext> dialogueTree = new TreeList <IDialogueContext>();
        ConnectionNode          currentNode      = (ConnectionNode)GetConnectedNode(entryNode);
        Queue <ConnectionNode>  nodesToVist      = new Queue <ConnectionNode>();
        List <IDialogueContext> contexts         = new List <IDialogueContext>(nodes.Count);
        List <int> childCounts = new List <int>(nodes.Count);

        nodesToVist.Enqueue(currentNode);
        while (nodesToVist.Count > 0)
        {
            DialogueNode tmp        = (DialogueNode)nodesToVist.Dequeue();
            int          childCount = 0;

            if (tmp.GetType() == typeof(DialogueNode <OptionsDialogueNode>))
            {
                DialogueNode <OptionsDialogueNode> castNode = (DialogueNode <OptionsDialogueNode>)tmp;
                ConnectionNode[] children = castNode.Data.optionNodes.ToArray();
                childCount = children.Length;
                for (int i = 0; i < childCount; i++)
                {
                    ConnectionNode connectedNode = (ConnectionNode)GetConnectedNode(children[i]);
                    if (connectedNode != null)
                    {
                        nodesToVist.Enqueue(connectedNode);
                    }
                    else
                    {
                        Vector2 pos = castNode.rect.position;
                        DialogueNode <TypedDialogueNode> endNode = CreateTypedDialogueNode(new Vector2(pos.x + 500, pos.y + (i * 125.0f)));
                        endNode.Data.type = DialogueTypes.Type.Goodbye;
                        nodesToVist.Enqueue(endNode);
                    }
                }
            }
            else
            {
                ConnectionNode connectedNode = (ConnectionNode)GetConnectedNode(tmp);
                if (connectedNode != null)
                {
                    childCount = 1;
                    nodesToVist.Enqueue(connectedNode);
                }
                else
                {
                    childCount = 0;
                }
            }

            childCounts.Add(childCount);
            VisitNode(tmp.GetData(), childCount, dialogueTree, contexts);
            serializedData.Insert(tmp.GetData().GetTitle(), tmp);
        }

        ConversationLayout asset = CreateInstance <ConversationLayout>();

        asset.Init(convoTitle, contexts, childCounts);

        AssetDatabase.CreateAsset(asset, "Assets/ConversationLayouts/" + convoTitle + ".asset");
        AssetDatabase.SaveAssets();

        EditorUtility.FocusProjectWindow();

        Selection.activeObject = asset;

        string json = JsonUtility.ToJson(serializedData);

        StreamWriter writer = new StreamWriter(path);

        try
        {
            writer.Write(json);
        }
        finally
        {
            writer.Close();
        }
    }