-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInkTesting.cs
82 lines (64 loc) · 1.86 KB
/
InkTesting.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Ink.Runtime;
using UnityEngine.UI;
using TMPro;
public class InkTesting : MonoBehaviour
{
public TextAsset inkJson;
private Story story;
public TextMeshProUGUI textPrefab;
public Button buttonPrefab;
void Start()
{
story = new Story(inkJson.text);
refreshUI();
}
private void refreshUI()
{
eraseUI();
TextMeshProUGUI storyText = Instantiate(textPrefab);
string text = loadStoryChunk();
List<string> tags = story.currentTags;
if (tags.Count > 0)
{
text = tags[0] + " - " + text;
}
storyText.text = text;
storyText.transform.SetParent(this.transform, false);
foreach (Choice choice in story.currentChoices)
{
Button choiceButton = Instantiate(buttonPrefab);
choiceButton.transform.SetParent(this.transform, false); // set that button as a child of the canvas
TextMeshProUGUI choiceText = choiceButton.GetComponentInChildren<TextMeshProUGUI>(); // Get the component as a TMP text object
choiceText.text = choice.text; // set that component to the story text
choiceButton.onClick.AddListener(delegate
{
chooseStoryChoice(choice);
}
);
}
}
void eraseUI()
{
for(int i = 0; i < this.transform.childCount; i++)
{
Destroy(this.transform.GetChild(i).gameObject);
}
}
void chooseStoryChoice(Choice choice)
{
story.ChooseChoiceIndex(choice.index);
refreshUI();
}
string loadStoryChunk()
{
string text = "";
if (story.canContinue)
{
text = story.ContinueMaximally();
}
return text;
}
}