VIDEUIManager2.cs
4.2 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using VIDE_Data;
/*
* This is another example script that handles the data obtained from nodeData
* It handles localization data.
* Refer to Example3Dialogue dialogue in the VIDE Editor.
* It is simpler and focused on showing both NPC and Player text at the same time
* It doesn't require any VIDE_Data or VIDE_Assign component to be in the scene
*/
public class VIDEUIManager2 : MonoBehaviour
{
public string dialogueNameToLoad;
public Text[] playerChoices;
public Text npcText;
public Image flag;
public AudioSource audioPlayer;
void Start()
{
//Sets the temp VIDE_Assign’s variables for the given dialogue.
VD.SetAssigned(dialogueNameToLoad, "LocalizationTest", -1, null, null);
}
//Called by UI button
public void Begin()
{
if (!VD.isActive)
{
transform.GetChild(1).gameObject.SetActive(true); //UI stuff
transform.GetChild(0).gameObject.SetActive(false); //UI stuff
VD.OnNodeChange += NodeChangeAction; //Required events
VD.OnEnd += End; //Required events
VD.BeginDialogue(dialogueNameToLoad);
}
}
//Called by UI buttons, every button sends a different choice index
public void ButtonChoice(int choice)
{
VD.nodeData.commentIndex = choice; //Set commentIndex as it acts as the picked choice
if (VD.nodeData.extraVars.ContainsKey("loadLang"))
{
if (VD.nodeData.commentIndex < (int) VD.nodeData.extraVars["loadLang"]) //Don't count index 3 as language
{
VD.OnLanguageChange += UpdateWithNewLanguage;
VD.SetCurrentLanguage(VD.GetLanguages()[VD.nodeData.commentIndex]);
}
else
{
VD.Next();
}
}
}
void OnDisable()
{
//If the script gets destroyed, let's make sure we force-end the dialogue to prevent errors
End(null);
}
//This will trigger with the OnLanguageChange event
//It will make sure the current text being displayed will be updated with the new localization
void UpdateWithNewLanguage() {
npcText.text = VD.GetNodeData(0).comments[0];
flag.sprite = VD.GetNodeData(0).sprite;
SetPlayerChoices();
audioPlayer.clip = VD.GetNodeData(0).audios[0];
audioPlayer.Play();
VD.OnLanguageChange -= UpdateWithNewLanguage;
}
//Called by the OnNodeChange event
void NodeChangeAction(VD.NodeData data)
{
if (data.isPlayer)
{
SetPlayerChoices();
}
else
{
WipePlayerChoices();
StartCoroutine(ShowNPCText());
}
}
void WipePlayerChoices()
{
for (int i = 0; i < playerChoices.Length; i++)
{
playerChoices[i].transform.parent.gameObject.SetActive(false);
}
}
void SetPlayerChoices()
{
for (int i = 0; i < playerChoices.Length; i++)
{
if (i < VD.nodeData.comments.Length)
{
playerChoices[i].transform.parent.gameObject.SetActive(true);
playerChoices[i].text = VD.nodeData.comments[i];
}
else
{
playerChoices[i].transform.parent.gameObject.SetActive(false);
}
}
}
IEnumerator ShowNPCText()
{
if (VD.GetExtraVariables(VD.nodeData.nodeID).ContainsKey("flag"))
flag.sprite = VD.nodeData.sprite;
string text = string.Empty;
npcText.text = text;
while (text.Length < VD.nodeData.comments[VD.nodeData.commentIndex].Length)
{
text += VD.nodeData.comments[VD.nodeData.commentIndex][text.Length];
npcText.text = text;
yield return new WaitForSeconds(0.01f);
}
//Automatically call next.
yield return new WaitForSeconds(1f);
VD.Next();
}
void End(VD.NodeData data)
{
WipePlayerChoices();
npcText.text = string.Empty;
transform.GetChild(1).gameObject.SetActive(false);
transform.GetChild(0).gameObject.SetActive(true);
VD.OnNodeChange -= NodeChangeAction;
VD.OnEnd -= End;
VD.EndDialogue();
}
}