0
\$\begingroup\$

I'm using a simple text file to write to using StreamWriter but maybe it's better to use a json. For example how I used the json before:

Transform[] trnfrm = new Transform[tranformToSave.Length];
string jsonTransform = JsonHelper.ToJson(trnfrm, true);
File.WriteAllText(@"d:\json\json.txt", jsonTransform);

But now I'm using a text file and not sure how to use the json(for saving/loading) if it's better and how to read back from the text file and assign back:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

[ExecuteInEditMode]
public class DialogueTrigger : MonoBehaviour
{
    public List<Conversation> conversations = new List<Conversation>();

    [HideInInspector]
    public int dialogueNum = 0;

    [HideInInspector]
    public int dialogueIndex = 0;

    private bool triggered = false;
    private List<Dialogue> oldDialogue;
    private bool activateButton = false;

    public void TriggerDialogue(int dialogueIndex)
    {
        this.dialogueIndex = dialogueIndex;

        if (conversations.Count > 0 &&
            conversations[dialogueIndex].Dialogues.Count > 0)
        {
            if (triggered == false)
            {
                if (FindObjectOfType<DialogueManager>() != null)
                {
                    FindObjectOfType<DialogueManager>().StartDialogue(conversations[dialogueIndex].Dialogues[dialogueNum]);
                    dialogueNum += 1;
                }
                triggered = true;
            }
        }
    }

    private void Update()
    {
        ButtonActivation();

        if (DialogueManager.dialogueEnded == true)
        {
            if (dialogueNum == conversations[dialogueIndex].Dialogues.Count)
            {
                return;
            }
            else
            {
                FindObjectOfType<DialogueManager>().StartDialogue(conversations[dialogueIndex].Dialogues[dialogueNum]);
                DialogueManager.dialogueEnded = false;
                dialogueNum += 1;
            }
        }
    }

    public bool ActivateButton()
    {
        return activateButton;
    }

    private void ButtonActivation()
    {
        if (ConversationsChecks() == true)
        {
            foreach (string sentence in conversations[dialogueIndex].Dialogues[dialogueNum].sentences)
            {
                if (sentence != "")
                {
                    activateButton = true;
                }
                else
                {
                    activateButton = false;
                }
            }
        }
        else
        {
            activateButton = false;
        }
    }

    public void SaveConversations()
    {
        StreamWriter w = new StreamWriter(@"D:\conversation.txt");
        for (int i = 0; i < conversations.Count; i++)
        {
            w.WriteLine("Conversation Name " + conversations[i].name);

            for (int x = 0; x < conversations[i].Dialogues.Count; x++)
            {
                w.WriteLine("Dialogue Name " + conversations[i].Dialogues[x].name);

                foreach (string sentence in conversations[i].Dialogues[x].sentences)
                {
                    w.WriteLine("Dialogue Sentences " + sentence);
                }
            }

            if (i != conversations.Count - 1)
                w.WriteLine(Environment.NewLine);
        }
        w.Close();
    }

    public void LoadConversations()
    {
        string[] lines = File.ReadAllLines(@"D:\conversation.txt");


    }

    private bool ConversationsChecks()
    {
        bool IsConversationsReady = false;

        if (conversations.Count > 0 &&
            conversations[dialogueIndex].Dialogues.Count > 0 &&
            conversations[dialogueIndex].Dialogues[dialogueNum].sentences.Count > 0 &&
            conversations[dialogueIndex].Dialogues[dialogueNum].name != "" &&
            conversations[dialogueIndex].name != "")
        {
            IsConversationsReady = true;
        }
        else
        {
            IsConversationsReady = false;
        }

        return IsConversationsReady;
    }
}

I added a new public method:

 public void LoadConversations()
        {
            string[] lines = File.ReadAllLines(@"D:\conversation.txt");


        }

But I'm not sure how to assign it back to the conversations > name > dialogues > name > sentences

This is the class Conversation:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Conversation
{
    [Tooltip("Conversation Name")]
    public string name;

    public List<Dialogue> Dialogues = new List<Dialogue>();
}

This is the class Dialogue:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Dialogue
{
    [Tooltip("Dialogue Name")]
    public string name;

    [TextArea(1, 10)]
    public List<string> sentences = new List<string>();
}

And the editor script for the button/s now I'm using only the save button:

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(DialogueTrigger))]
public class SaveConversationsButton : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        DialogueTrigger dialoguetrigger = (DialogueTrigger)target;

        if(dialoguetrigger.ActivateButton() == true)
        {
            GUI.enabled = true;
        }
        else
        {
            GUI.enabled = false;
        }
        if (GUILayout.Button("Save Conversations"))
        {
            dialoguetrigger.SaveConversations();
        }
    }
}

But now I want to use the load button too.

This is example of how the text file format looks like after a saving:

Conversation Name Conversation 1
Dialogue Name Dialogue 1
Dialogue Sentences Hello World
Dialogue Sentences It's my time to make a magic


Conversation Name The openning scene
Dialogue Name NAVI
Dialogue Sentences Hello to you
Dialogue Sentences Lets hide quick
Dialogue Name PLAYER
Dialogue Sentences I'm the player now lets go

And this is a screenshot of how it looks like in the inspector in the editor and how it should looks like after loading:

Inspector

\$\endgroup\$
  • 1
    \$\begingroup\$ Json classes typically have two methods: serialize (the part you have) and deserialize. Unity has a built in JsonUtility that can do this for you, but is pretty basic. If you need something more powerful, I recommend Newtonsoft's Json Dot Net. \$\endgroup\$ – Draco18s Apr 11 at 23:00
  • \$\begingroup\$ I used JsonHelper and it was easy to save it to a Json file: string jsonTransform = JsonHelper.ToJson(conversations.ToArray(), true); File.WriteAllText(@"d:\json.txt", jsonTransform); but now how do I read it back to the conversations List ? Inside another method I did: string jsonTransform = File.ReadAllText(@"d:\json.txt"); var t = JsonHelper.FromJson<string>(jsonTransform); but var t is now string array and I need it to be assigned read back into the conversations List. \$\endgroup\$ – yochi le Apr 11 at 23:59
  • \$\begingroup\$ FromJson uses the same type of object as the thing you put in ToJson. If you convert a Conversation object to JSON then when you want to do the reverse, why would you read it back as a string? \$\endgroup\$ – Draco18s Apr 12 at 3:28
  • \$\begingroup\$ @Draco18s The problem is that if I'm doing: conversations = JsonHelper.FromJson<List<Conversation>>(jsonTransform); I'm getting error on the right side Cannot implicitly convert type 'System.Collections.Generic.List<Conversation>[]' to 'System.Collections.Generic.List<Conversation>' And the type of the conversations List I used to save is List<Conversation> but it give me the error. \$\endgroup\$ – yochi le Apr 12 at 6:14
  • \$\begingroup\$ Doesnt look like your text file is in json at all.. if you saved it as json, you'd be able to read back the json. \$\endgroup\$ – BugFinder Apr 12 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.