In some script in the Update I’m doing for example :
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class NaviDialogue : MonoBehaviour
{
public ConversationTrigger conversationTrigger;
private void Start()
{
}
private void Update()
{
conversationTrigger.StartConversations().Add(0);
conversationTrigger.StartConversations().Add(1);
conversationTrigger.StartConversations().Add(2);
}
Or if in the Start :
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class NaviDialogue : MonoBehaviour
{
public ConversationTrigger conversationTrigger;
private void Start()
{
conversationTrigger.StartConversations().Add(0);
conversationTrigger.StartConversations().Add(1);
conversationTrigger.StartConversations().Add(2);
}
What I want to do is either in the Update or in the Start that it will play the conversations 0, 1, 2 first it will play conversation 0 when conversation 0 ended start playing 1 when 1 ended start 2 when 2 ended and there is no more conversations in the List stop playing.
The script of the conversations is Conversation Trigger :
In this script I added now this part :
public List<int> StartConversations()
{
var conversations = new List<int>();
for (int i = 0; i < conversations.Count; i++)
{
PlayConversation(conversations[i]);
}
return conversations;
}
Then tried in other script for testing to call it in the Update 3 times adding 3 indexs 0 1 2
But it’s not working does nothing. No errors or exceptions just it does nothing.
Before that I was just calling just PlayConversation function but I want to play now number of conversations one by one automatic.
I added the other script too since they are connected.
In the manager script there is the EndDialogue method.
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;
public bool conversationEnd = false;
[HideInInspector]
public static int conversationIndex;
private DialogueManager dialoguemanager;
private void Start()
{
conversationIndex = 0;
dialoguemanager = FindObjectOfType<DialogueManager>();
}
public List<int> StartConversations()
{
var conversations = new List<int>();
for (int i = 0; i < conversations.Count; i++)
{
StartCoroutine(PlayConversation(conversations[i]));
}
return conversations;
}
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));
}
}
The second script is the Dialogue Manaher :
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;
}
}