- Home /
How can I use my conversations system so I can start a conversation anywhere any time even some conversation one after one ?
The first script is called Dialogue Manager :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DialogueManager : MonoBehaviour
{
public Text dialogueText;
public Text nameText;
public float sentencesSwitchDuration;
public bool animateSentenceChars = false;
public GameObject canvas;
public static bool dialogueEnded = false;
public ConversationTrigger trigger;
private Queue<string> sentence;
// Use this for initialization
void Start()
{
sentence = new Queue<string>();
}
public void StartDialogue(Dialogue dialogue)
{
dialogueEnded = false;
canvas.SetActive(true);
nameText.text = dialogue.name;
if(sentence == null)
sentence = new Queue<string>();
sentence.Clear();
foreach (string sentence in dialogue.sentences)
{
this.sentence.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if (this.sentence.Count == 0)
{
EndDialogue();
return;
}
string sentence = this.sentence.Dequeue();
dialogueText.text = sentence;
StopAllCoroutines();
StartCoroutine(DisplayNextSentenceWithDelay(sentence));
}
public IEnumerator DisplayNextSentenceWithDelay(string sentence)
{
if (animateSentenceChars)
{
dialogueText.text = "";
foreach (char letter in sentence.ToCharArray())
{
dialogueText.text += letter;
yield return null;
}
}
yield return new WaitForSeconds(sentencesSwitchDuration);
DisplayNextSentence();
}
private void EndDialogue()
{
dialogueEnded = true;
}
}
The second script is 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>();
}
The next script is Conversation Trigger :
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class ConversationTrigger : MonoBehaviour
{
public List<Conversation> conversations = new List<Conversation>();
public GameObject canvas;
[HideInInspector]
public static int conversationIndex;
public bool conversationEnd = false;
private bool activateButton = false;
private DialogueManager dialoguemanager;
private bool startDialogue = false;
private void Start()
{
conversationIndex = 0;
dialoguemanager = FindObjectOfType<DialogueManager>();
}
public IEnumerator PlayConversation(int index)
{
if (conversations.Count > 0 &&
conversations[index].Dialogues.Count > 0)
{
for (int i = 0; i < conversations[index].Dialogues.Count; i++)
{
if (dialoguemanager != null)
{
dialoguemanager.StartDialogue(conversations[index].Dialogues[i]);
}
while (DialogueManager.dialogueEnded == false)
{
yield return null;
}
}
conversationIndex = index;
conversationEnd = true;
canvas.SetActive(false);
Debug.Log("Conversation Ended");
}
}
public void SaveConversations()
{
string jsonTransform = JsonHelper.ToJson(conversations.ToArray(), true);
File.WriteAllText(@"d:\json.txt", jsonTransform);
}
public void LoadConversations()
{
string jsonTransform = File.ReadAllText(@"d:\json.txt");
conversations.Clear();
conversations.AddRange(JsonHelper.FromJson<Conversation>(jsonTransform));
}
}
And last one is called 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>();
}
Today to start a conversation I'm making a reference to the Conversation Trigger script and then for example :
StartCoroutine(conversationTrigger.PlayConversation(1));
Or
StartCoroutine(conversationTrigger.PlayConversation(10));
This is working fine in general but then after starting playing a conversation ins some script I need to check when the conversation is ended and then start a new one or if a conversation not started yet and so on.
What I want to do is to make that I will be able to type in any script fro example in the Start or in the Update like this :
Start()
{
StartCoroutine(conversationTrigger.PlayConversation(1));
StartCoroutine(conversationTrigger.PlayConversation(10));
StartCoroutine(conversationTrigger.PlayConversation(3));
}
Or
Update()
{
StartCoroutine(conversationTrigger.PlayConversation(1));
StartCoroutine(conversationTrigger.PlayConversation(10));
StartCoroutine(conversationTrigger.PlayConversation(5));;
}
And it will play the conversation one by one. If in the Start then it will play first the 1 then when 1 is ended will start playing 10 when 10 ended start playing 3 and same if in Update first play 1 when ended play 10 when ended play 5
The idea is that I will only type anywhere for example :
StartCoroutine(conversationTrigger.PlayConversation(1));
And it will start playing it automatic and if there is another conversation in a queue or a List play it next if not finish playing.
The idea is to make it as much as easy to play conversation/s
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How can i rotate the camera to the player facing direction ? 1 Answer
Why when using LookAt to make the turret facing the target it's facing the other direction ? 1 Answer
How can i make an object to rotate in a circle smooth around another object nonstop ? 2 Answers
How can i use a button key click to switch/toggle between enum options ? 1 Answer