VIDEUIManager3.cs
3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using VIDE_Data; //<--- Import to use VD2 class
/*
* This is another example of a dialogue UImanager
* But this one uses the VD2 class that has no static members
* You can use the VD2 to create multiple instances and have multiple dialogues running at the same time
*/
public class VIDEUIManager3 : MonoBehaviour
{
public Text[] npcText; //References to UI elements
IEnumerator[] textAnim = new IEnumerator[4];
public int animatingText = -1; //With this we'll know if and what we are animating
//A list that will contain instances of VD2, which will handle dialogue data
public List<VD2> dialogueDataInstances = new List<VD2>();
void Start()
{
//Let's just add 4 instances of VD2 for our dialogues
for (int i = 0; i < 4; i++)
dialogueDataInstances.Add(new VD2());
}
//Called by UI buttons, sends the dialogue index
//Will start the convo or update it
public void ButtonAction(int dialogueIndex)
{
var data = dialogueDataInstances[dialogueIndex];
if (!data.isActive)
{
data.OnEnd += End; //Required events
data.BeginDialogue(GetComponent<VIDE_Assign>());
//This will begin the dialogue for that instance
}
else
{
if (animatingText != -1)
{
CutTextAnim();
return;
} else
{
data.Next();
//Next for that instance
}
}
if (data.isActive) //Check again if loaded as we might reach the end when calling Next
UpdateDialogue(dialogueIndex);
}
//This will update the interface, which is NPC text only
void UpdateDialogue(int dialogueIndex)
{
textAnim[dialogueIndex] = ShowNPCText(dialogueIndex);
StartCoroutine(textAnim[dialogueIndex]);
}
//Animate the npc text
IEnumerator ShowNPCText(int dialogueIndex)
{
animatingText = dialogueIndex;
var data = dialogueDataInstances[dialogueIndex];
string text = string.Empty;
npcText[dialogueIndex].text = text;
while (text.Length < data.nodeData.comments[data.nodeData.commentIndex].Length)
{
text += data.nodeData.comments[data.nodeData.commentIndex][text.Length];
npcText[dialogueIndex].text = text;
yield return new WaitForSeconds(0.01f);
}
animatingText = -1;
}
//Stop animating the text
void CutTextAnim()
{
var data = dialogueDataInstances[animatingText];
StopCoroutine(textAnim[animatingText]);
npcText[animatingText].text = data.nodeData.comments[data.nodeData.commentIndex]; //Now just copy full text
animatingText = -1;
}
//OnEnd will trigger this which will end the conversation for that instance
void End(VD2 data)
{
//You can use the returned VD2 to find it within a list and get its index.
npcText[dialogueDataInstances.IndexOf(data)].text = string.Empty;
data.OnEnd -= End;
data.EndDialogue();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.T))
{
VD.SetCurrentLanguage("English");
}
}
}