Template_UIManager.cs
16.4 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
* This is a template verison of the VIDEUIManager1.cs script. Check that script out and the "Player Interaction" demo for more reference.
* This one doesn't include an item popup as that demo was mostly hard coded.
* Doesn't include reference to a player script or gameobject. How you handle that is up to you.
* Doesn't save dialogue and VA state.
* Player choices are not instantiated. You need to set the references manually.
* You are NOT limited to what this script can do. This script is only for convenience. You are completely free to write your own manager or build from this one.
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using VIDE_Data; //<--- Import to use VD class
public class Template_UIManager : MonoBehaviour
{
#region VARS
//These are the references to UI components and containers in the scene
[Header("References")]
public GameObject dialogueContainer;
public GameObject NPC_Container;
public GameObject playerContainer;
public Text NPC_Text;
public Text NPC_label;
public Image NPCSprite;
public Image playerSprite;
public Text playerLabel;
public List<Button> maxPlayerChoices = new List<Button>();
[Tooltip("Attach an Audio Source and reference it if you want to play audios")]
public AudioSource audioSource;
[Header("Options")]
public KeyCode interactionKey;
public bool NPC_animateText;
public bool player_animateText;
public float NPC_secsPerLetter;
public float player_secsPerLetter;
public float choiceInterval;
[Tooltip("Tick this if using Navigation. Will prevent mixed input.")]
public bool useNavigation;
bool dialoguePaused = false; //Custom variable to prevent the manager from calling VD.Next
bool animatingText = false; //Will help us know when text is currently being animated
int availableChoices = 0;
IEnumerator TextAnimator;
#endregion
#region MAIN
void Awake()
{
VD.LoadDialogues(); //Load all dialogues to memory so that we dont spend time doing so later
//An alternative to this can be preloading dialogues from the VIDE_Assign component!
}
//Call this to begin the dialogue and advance through it
public void Interact(VIDE_Assign dialogue)
{
//Sometimes, we might want to check the ExtraVariables and VAs before moving forward
//We might want to modify the dialogue or perhaps go to another node, or dont start the dialogue at all
//In such cases, the function will return true
var doNotInteract = PreConditions(dialogue);
if (doNotInteract) return;
if (!VD.isActive)
{
Begin(dialogue);
}
else
{
CallNext();
}
}
//This begins the conversation.
void Begin(VIDE_Assign dialogue)
{
//Let's reset the NPC text variables
NPC_Text.text = "";
NPC_label.text = "";
playerLabel.text = "";
//Subscribe to events
VD.OnActionNode += ActionHandler;
VD.OnNodeChange += UpdateUI;
VD.OnEnd += EndDialogue;
VD.BeginDialogue(dialogue); //Begins dialogue, will call the first OnNodeChange
dialogueContainer.SetActive(true); //Let's make our dialogue container visible
}
//Calls next node in the dialogue
public void CallNext()
{
//Let's not go forward if text is currently being animated, but let's speed it up.
if (animatingText) { CutTextAnim(); return; }
if (!dialoguePaused) //Only if
{
VD.Next(); //We call the next node and populate nodeData with new data. Will fire OnNodeChange.
}
else
{
//Stuff we can do instead if dialogue is paused
}
}
//If not using local input, then the UI buttons are going to call this method when you tap/click them!
//They will send along the choice index
public void SelectChoice(int choice)
{
VD.nodeData.commentIndex = choice;
if (Input.GetMouseButtonUp(0))
{
Interact(VD.assigned);
}
}
//Input related stuff (scroll through player choices and update highlight)
void Update()
{
//Lets just store the Node Data variable for the sake of fewer words
var data = VD.nodeData;
if (VD.isActive) //If there is a dialogue active
{
//Scroll through Player dialogue options if dialogue is not paused and we are on a player node
//For player nodes, NodeData.commentIndex is the index of the picked choice
if (!data.pausedAction && !animatingText && data.isPlayer && !useNavigation)
{
if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
{
if (data.commentIndex < availableChoices - 1)
data.commentIndex++;
}
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
{
if (data.commentIndex > 0)
data.commentIndex--;
}
//Color the Player options. Blue for the selected one
for (int i = 0; i < maxPlayerChoices.Count; i++)
{
maxPlayerChoices[i].transform.GetChild(0).GetComponent<Text>().color = Color.white;
if (i == data.commentIndex) maxPlayerChoices[i].transform.GetChild(0).GetComponent<Text>().color = Color.yellow;
}
}
//Detect interact key
if (Input.GetKeyDown(interactionKey))
{
Interact(VD.assigned);
}
if (Input.GetMouseButtonDown(0))
{
if (animatingText)
{
Interact(VD.assigned);
}
else if (!data.isPlayer)
{
Interact(VD.assigned);
}
}
}
//Note you could also use Unity's Navi system, in which case you would tick the useNavigation flag.
}
//When we call VD.Next, nodeData will change. When it changes, OnNodeChange event will fire
//We subscribed our UpdateUI method to the event in the Begin method
//Here's where we update our UI
void UpdateUI(VD.NodeData data)
{
//Reset some variables
NPC_Text.text = "";
foreach (Button b in maxPlayerChoices) { b.transform.GetChild(0).GetComponent<Text>().text = ""; b.transform.GetChild(0).GetComponent<Text>().color = Color.white; }
NPC_Container.SetActive(false);
playerContainer.SetActive(false);
playerSprite.sprite = null;
NPCSprite.sprite = null;
//Look for dynamic text change in extraData
PostConditions(data);
//If this new Node is a Player Node, set the player choices offered by the node
if (data.isPlayer)
{
//Set node sprite if there's any, otherwise try to use default sprite
if (data.sprite != null)
playerSprite.sprite = data.sprite;
else if (VD.assigned.defaultPlayerSprite != null)
playerSprite.sprite = VD.assigned.defaultPlayerSprite;
SetChoices(data.comments);
//If it has a tag, show it, otherwise let's use the alias we set in the VIDE Assign
if (data.tag.Length > 0)
playerLabel.text = data.tag;
//Sets the player container on
playerContainer.SetActive(true);
}
else //If it's an NPC Node, let's just update NPC's text and sprite
{
//Set node sprite if there's any, otherwise try to use default sprite
if (data.sprite != null)
{
//For NPC sprite, we'll first check if there's any "sprite" key
//Such key is being used to apply the sprite only when at a certain comment index
//Check CrazyCap dialogue for reference
if (data.extraVars.ContainsKey("sprite"))
{
if (data.commentIndex == (int)data.extraVars["sprite"])
NPCSprite.sprite = data.sprite;
else
NPCSprite.sprite = VD.assigned.defaultNPCSprite; //If not there yet, set default dialogue sprite
}
else //Otherwise use the node sprites
{
NPCSprite.sprite = data.sprite;
}
} //or use the default sprite if there isnt a node sprite at all
else if (VD.assigned.defaultNPCSprite != null)
NPCSprite.sprite = VD.assigned.defaultNPCSprite;
if (NPC_animateText)
{
//This coroutine animates the NPC text instead of displaying it all at once
TextAnimator = AnimateNPCText(data.comments[data.commentIndex]);
StartCoroutine(TextAnimator);
} else
{
NPC_Text.text = data.comments[data.commentIndex];
}
if (data.audios[data.commentIndex] != null)
{
audioSource.clip = data.audios[data.commentIndex];
audioSource.Play();
}
//If it has a tag, show it, otherwise let's use the alias we set in the VIDE Assign
if (data.tag.Length > 0)
NPC_label.text = data.tag;
else
NPC_label.text = VD.assigned.alias;
//Sets the NPC container on
NPC_Container.SetActive(true);
}
}
//This uses the returned string[] from nodeData.comments to create the UIs for each comment
//It first cleans, then it instantiates new choices
public void SetChoices(string[] choices)
{
availableChoices = 0;
foreach (UnityEngine.UI.Button choice in maxPlayerChoices)
choice.gameObject.SetActive(false);
if (player_animateText)
{
//This coroutine animates the Player choices instead of displaying it all at once
TextAnimator = AnimatePlayerText(choices);
StartCoroutine(TextAnimator);
} else
{
for (int i = 0; i < choices.Length; i++)
{
maxPlayerChoices[i].transform.GetChild(0).GetComponent<Text>().text = choices[i]; //Assumes first child of button gameobject is text gameobject
maxPlayerChoices[i].gameObject.SetActive(true);
availableChoices++;
}
//Highlight the button. Used by Navi
if (useNavigation)
maxPlayerChoices[0].Select();
}
}
//Unsuscribe from everything, disable UI, and end dialogue
//Called automatically because we subscribed to the OnEnd event
void EndDialogue(VD.NodeData data)
{
VD.OnActionNode -= ActionHandler;
VD.OnNodeChange -= UpdateUI;
VD.OnEnd -= EndDialogue;
if (dialogueContainer != null)
dialogueContainer.SetActive(false);
VD.EndDialogue();
}
//To prevent errors
void OnDisable()
{
EndDialogue(null);
}
#endregion
#region DIALOGUE CONDITIONS
//DIALOGUE CONDITIONS --------------------------------------------
bool PreConditions(VIDE_Assign assigned)
{
var data = VD.nodeData;
if (VD.isActive)
{
if (!data.isPlayer)
{
}
else
{
}
} else
{
}
return false;
}
//Conditions we check after VD.Next was called but before we update the UI
void PostConditions(VD.NodeData data)
{
//Don't conduct extra variable actions if we are waiting on a paused action
if (data.pausedAction) return;
ReplaceWord(data);
if (!data.isPlayer) //For player nodes
{
//Checks for extraData that concerns font size (CrazyCap node 2)
if (data.extraData[data.commentIndex].Contains("fs"))
{
int fSize = 14;
string[] fontSize = data.extraData[data.commentIndex].Split(","[0]);
int.TryParse(fontSize[1], out fSize);
NPC_Text.fontSize = fSize;
}
else
{
NPC_Text.fontSize = 14;
}
} else
{
}
}
void ReplaceWord(VD.NodeData data)
{
if (data.comments[data.commentIndex].Contains("[NAME]"))
data.comments[data.commentIndex] = data.comments[data.commentIndex].Replace("[NAME]", VD.assigned.gameObject.name);
if (data.comments[data.commentIndex].Contains("[WEAPON]"))
data.comments[data.commentIndex] = data.comments[data.commentIndex].Replace("[WEAPON]", "sword");
}
#endregion
#region EVENTS AND HANDLERS
//Called when dialogue sare finished loading
void OnLoadedAction()
{
//Debug.Log("Finished loading all dialogues");
VD.OnLoaded -= OnLoadedAction;
}
//Another way to handle Action Nodes is to listen to the OnActionNode event, which sends the ID of the action node
void ActionHandler(int actionNodeID)
{
//Debug.Log("ACTION TRIGGERED: " + actionNodeID.ToString());
}
IEnumerator AnimatePlayerText(string[] choices)
{
animatingText = true;
for (int c = 0; c < choices.Length; c++)
{
maxPlayerChoices[c].gameObject.SetActive(true);
string[] words = choices[c].Split(' ');
Text txtref = maxPlayerChoices[c].transform.GetChild(0).GetComponent<Text>();
txtref.text = "";
for (int i = 0; i < words.Length; i++)
{
string word = words[i];
if (i != words.Length - 1) word += " ";
string previousText = txtref.text;
float lastHeight = txtref.preferredHeight;
txtref.text += word;
if (txtref.preferredHeight > lastHeight)
{
previousText += System.Environment.NewLine;
}
for (int j = 0; j < word.Length; j++)
{
txtref.text = previousText + word.Substring(0, j + 1);
yield return new WaitForSeconds(player_secsPerLetter);
}
}
availableChoices++;
txtref.text = choices[c];
yield return new WaitForSeconds(choiceInterval);
}
//Highlight the button. Used by Navi
if (useNavigation)
maxPlayerChoices[0].Select();
animatingText = false;
}
IEnumerator AnimateNPCText(string text)
{
animatingText = true;
string[] words = text.Split(' ');
for (int i = 0; i < words.Length; i++)
{
string word = words[i];
if (i != words.Length - 1) word += " ";
string previousText = NPC_Text.text;
float lastHeight = NPC_Text.preferredHeight;
NPC_Text.text += word;
if (NPC_Text.preferredHeight > lastHeight)
{
previousText += System.Environment.NewLine;
}
for (int j = 0; j < word.Length; j++)
{
NPC_Text.text = previousText + word.Substring(0, j + 1);
yield return new WaitForSeconds(NPC_secsPerLetter);
}
}
NPC_Text.text = text;
animatingText = false;
}
void CutTextAnim()
{
StopCoroutine(TextAnimator);
if (VD.nodeData.isPlayer)
{
availableChoices = 0;
for (int i = 0; i < VD.nodeData.comments.Length; i++)
{
maxPlayerChoices[i].transform.GetChild(0).GetComponent<Text>().text = VD.nodeData.comments[i]; //Assumes first child of button gameobject is text gameobject
maxPlayerChoices[i].gameObject.SetActive(true);
availableChoices++;
}
if (useNavigation)
maxPlayerChoices[0].Select();
}
else
{
NPC_Text.text = VD.nodeData.comments[VD.nodeData.commentIndex]; //Now just copy full text
}
animatingText = false;
}
#endregion
//Utility note: If you're on MonoDevelop. Go to Tools > Options > General and enable code folding.
//That way you can exapnd and collapse the regions and methods
}